<commit_before>local L = EPGPGlobalStrings

local mod = EPGP:NewModule("EPGP_Backend", "AceEvent-2.0")

local function OnStaticPopupHide()
	if ChatFrameEditBox:IsShown() then
		ChatFrameEditBox:SetFocus()
	end
	getglobal(this:GetName().."EditBox"):SetText("")
end

local function GuildIterator(obj, i)
  local name = GetGuildRosterInfo(i)
  -- Handle dummies
  if obj.cache:IsDummy(name) then
    name = EPGP.db.profile.dummies[name]
  end
  if not name then return end
  return i+1, name
end

local function RaidIterator(obj, i)
  if not UnitInRaid("player") then return end
  local name = GetRaidRosterInfo(i)
  if not name then return end
  return i+1, name
end

local function GuildOnlineIterator(obj, i)
  local name, online
  repeat
    name, _, _, _, _, _, _, _, online = GetGuildRosterInfo(i)
    i = i + 1
  until not name or online
  -- Handle dummies
  if obj.cache:IsDummy(name) then
    name = EPGP.db.profile.dummies[name]
  end

  if not name then return end
  return i, name
end

local ITERATORS = {
  ["GUILD"] = GuildIterator,
  ["RAID"] = RaidIterator,
  ["GUILD_ONLINE_LABEL"] = GuildOnlineIterator,
}

local LISTING_IDS = {
  "GUILD",
  "RAID",
  "GUILD_ONLINE_LABEL",
}

function mod:GetListingIDs()
  return LISTING_IDS
end

function mod:OnInitialize()
  self.cache = EPGP:GetModule("EPGP_Cache")
  StaticPopupDialogs["EPGP_RESET_EPGP"] = {
    text = L["Reset all EP and GP to 0 and make officer notes readable by all?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:ResetEPGP()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  StaticPopupDialogs["EPGP_DECAY_EPGP"] = {
    text = L["Decay EP and GP by %d%%?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:DecayEPGP()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  StaticPopupDialogs["EPGP_RESTORE_NOTES"] = {
    text = L["Restore public and officer notes from the last backup?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:RestoreNotes()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  self.popup_modify_epgp_data = {}
  StaticPopupDialogs["EPGP_MODIFY_EPGP"] = {
    text = "%s",
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnShow = function()
      local editBox = getglobal(this:GetName().."EditBox")
      editBox:SetFocus()
    end,
    OnHide = OnStaticPopupHide,
    OnAccept = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        data.func(mod, data.member, number)
      end
    end,
    EditBoxOnEnterPressed = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        data.func(mod, data.member, number)
        this:GetParent():Hide()
      end
    end,
    EditBoxOnTextChanged = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local button1 = getglobal(this:GetParent():GetName().."Button1")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        button1:Enable()
      else
        button1:Disable()
      end
    end,
    EditBoxOnEscapePressed = function()
      this:GetParent():Hide()
    end,
    hideOnEscape = 1,
    hasEditBox = 1,
    whileDead = 1,
  }
  self.popup_unzoned_members_data = {}
  StaticPopupDialogs["EPGP_UNZONED_MEMBERS_POPUP"] = {
    text = L["Do you want to include members not in %s in the award? (%s)"],
    button1 = YES,
    button2 = NO,
    timeout = 0,
    OnAccept = function()
      local data = self.popup_unzoned_members_data
      data.func(mod, data.list_name, data.points, {})
    end,
    OnCancel = function()
      local data = self.popup_unzoned_members_data
      data.func(mod, data.list_name, data.points, data.exclude_map)
    end,
    whileDead = 1,
  }
end

function mod:OnEnable()
  self:RegisterEvent("RAID_ROSTER_UPDATE")
  self:RegisterEvent("EPGP_CACHE_UPDATE")
  self:RegisterEvent("EPGP_STOP_RECURRING_EP_AWARDS")
end

function mod:RAID_ROSTER_UPDATE()
  if not UnitInRaid("player") then
    self:TriggerEvent("EPGP_STOP_RECURRING_EP_AWARDS")
  end
end

function mod:EPGP_CACHE_UPDATE()
  local guild_name = GetGuildInfo("player")
  if guild_name ~= EPGP:GetProfile() then EPGP:SetProfile(guild_name) end
end

function mod:CanLogRaids()
  return CanEditOfficerNote()
end

function mod:CanChangeRules()
  return IsGuildLeader() or (self:CanLogRaids() and EPGP.db.profile.flat_credentials)
end

function mod:Report(fmt, ...)
  if EPGP.db.profile.report_channel ~= "NONE" then
    local msg = string.format(fmt, ...)
    local str = "EPGP:"
    for _,s in pairs({strsplit(" ", msg)}) do
      if #str + #s >= 255 then
	      SendChatMessage(str, EPGP.db.profile.report_channel)
	      str = "EPGP:"
      end
      str = str .. " " .. s
    end
    SendChatMessage(str, EPGP.db.profile.report_channel)
  end
end

function mod:ResetEPGP()
  -- First delete all officer notes
  for i = 1, GetNumGuildMembers(true) do
    GuildRosterSetOfficerNote(i, "")
  end
  -- Now set zero values
  for i = 1, GetNumGuildMembers(true) do
    local name = GetGuildRosterInfo(i)
    self.cache:SetMemberEPGP(name, 0, 0)
  end
  self.cache:SaveRoster()
  -- Make officer notes readable by all ranks
  for i = 1,GuildControlGetNumRanks() do
    GuildControlSetRank(i)
    GuildControlSetRankFlag(11, true)
    GuildControlSaveRank(GuildControlGetRankName(i))
  end
  self:Report(L["All EP/GP are reset and officer notes are made readable by all."])<commit_msg>Small fix on multiline output.<commit_after>local L = EPGPGlobalStrings

local mod = EPGP:NewModule("EPGP_Backend", "AceEvent-2.0")

local function OnStaticPopupHide()
	if ChatFrameEditBox:IsShown() then
		ChatFrameEditBox:SetFocus()
	end
	getglobal(this:GetName().."EditBox"):SetText("")
end

local function GuildIterator(obj, i)
  local name = GetGuildRosterInfo(i)
  -- Handle dummies
  if obj.cache:IsDummy(name) then
    name = EPGP.db.profile.dummies[name]
  end
  if not name then return end
  return i+1, name
end

local function RaidIterator(obj, i)
  if not UnitInRaid("player") then return end
  local name = GetRaidRosterInfo(i)
  if not name then return end
  return i+1, name
end

local function GuildOnlineIterator(obj, i)
  local name, online
  repeat
    name, _, _, _, _, _, _, _, online = GetGuildRosterInfo(i)
    i = i + 1
  until not name or online
  -- Handle dummies
  if obj.cache:IsDummy(name) then
    name = EPGP.db.profile.dummies[name]
  end

  if not name then return end
  return i, name
end

local ITERATORS = {
  ["GUILD"] = GuildIterator,
  ["RAID"] = RaidIterator,
  ["GUILD_ONLINE_LABEL"] = GuildOnlineIterator,
}

local LISTING_IDS = {
  "GUILD",
  "RAID",
  "GUILD_ONLINE_LABEL",
}

function mod:GetListingIDs()
  return LISTING_IDS
end

function mod:OnInitialize()
  self.cache = EPGP:GetModule("EPGP_Cache")
  StaticPopupDialogs["EPGP_RESET_EPGP"] = {
    text = L["Reset all EP and GP to 0 and make officer notes readable by all?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:ResetEPGP()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  StaticPopupDialogs["EPGP_DECAY_EPGP"] = {
    text = L["Decay EP and GP by %d%%?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:DecayEPGP()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  StaticPopupDialogs["EPGP_RESTORE_NOTES"] = {
    text = L["Restore public and officer notes from the last backup?"],
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnAccept = function()
      mod:RestoreNotes()
    end,
    hideOnEscape = 1,
    whileDead = 1,
  }
  self.popup_modify_epgp_data = {}
  StaticPopupDialogs["EPGP_MODIFY_EPGP"] = {
    text = "%s",
    button1 = ACCEPT,
    button2 = CANCEL,
    timeout = 0,
    OnShow = function()
      local editBox = getglobal(this:GetName().."EditBox")
      editBox:SetFocus()
    end,
    OnHide = OnStaticPopupHide,
    OnAccept = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        data.func(mod, data.member, number)
      end
    end,
    EditBoxOnEnterPressed = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        data.func(mod, data.member, number)
        this:GetParent():Hide()
      end
    end,
    EditBoxOnTextChanged = function()
      local data = self.popup_modify_epgp_data
      local editBox = getglobal(this:GetParent():GetName().."EditBox")
      local button1 = getglobal(this:GetParent():GetName().."Button1")
      local number = editBox:GetNumber()
      if not data.valid_func or data.valid_func(number) then
        button1:Enable()
      else
        button1:Disable()
      end
    end,
    EditBoxOnEscapePressed = function()
      this:GetParent():Hide()
    end,
    hideOnEscape = 1,
    hasEditBox = 1,
    whileDead = 1,
  }
  self.popup_unzoned_members_data = {}
  StaticPopupDialogs["EPGP_UNZONED_MEMBERS_POPUP"] = {
    text = L["Do you want to include members not in %s in the award? (%s)"],
    button1 = YES,
    button2 = NO,
    timeout = 0,
    OnAccept = function()
      local data = self.popup_unzoned_members_data
      data.func(mod, data.list_name, data.points, {})
    end,
    OnCancel = function()
      local data = self.popup_unzoned_members_data
      data.func(mod, data.list_name, data.points, data.exclude_map)
    end,
    whileDead = 1,
  }
end

function mod:OnEnable()
  self:RegisterEvent("RAID_ROSTER_UPDATE")
  self:RegisterEvent("EPGP_CACHE_UPDATE")
  self:RegisterEvent("EPGP_STOP_RECURRING_EP_AWARDS")
end

function mod:RAID_ROSTER_UPDATE()
  if not UnitInRaid("player") then
    self:TriggerEvent("EPGP_STOP_RECURRING_EP_AWARDS")
  end
end

function mod:EPGP_CACHE_UPDATE()
  local guild_name = GetGuildInfo("player")
  if guild_name ~= EPGP:GetProfile() then EPGP:SetProfile(guild_name) end
end

function mod:CanLogRaids()
  return CanEditOfficerNote()
end

function mod:CanChangeRules()
  return IsGuildLeader() or (self:CanLogRaids() and EPGP.db.profile.flat_credentials)
end

function mod:Report(fmt, ...)
  if EPGP.db.profile.report_channel ~= "NONE" then
    local msg = string.format(fmt, ...)
    local str = "EPGP:"
    for _,s in pairs({strsplit(" ", msg)}) do
      if #str + #s >= 250 then
	      SendChatMessage(str, EPGP.db.profile.report_channel)
	      str = "EPGP:"
      end
      str = str .. " " .. s
    end
    SendChatMessage(str, EPGP.db.profile.report_channel)
  end
end

function mod:ResetEPGP()
  -- First delete all officer notes
  for i = 1, GetNumGuildMembers(true) do
    GuildRosterSetOfficerNote(i, "")
  end
  -- Now set zero values
  for i = 1, GetNumGuildMembers(true) do
    local name = GetGuildRosterInfo(i)
    self.cache:SetMemberEPGP(name, 0, 0)
  end
  self.cache:SaveRoster()
  -- Make officer notes readable by all ranks
  for i = 1,GuildControlGetNumRanks() do
    GuildControlSetRank(i)
    GuildControlSetRankFlag(11, true)
    GuildControlSaveRank(GuildControlGetRankName(i))
  end
  self:Report(L["All EP/GP are reset and officer notes are made readable by all."])
{'fraction_non_alphanumeric': 0.11208865578208273, 'fraction_numerical': 0.007613569071990525, 'mean_word_length': 3.4244011976047903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '853448', 'n_tokens_mistral': 4225, 'n_tokens_neox': 4183, 'n_words': 963}