Advertisement
Guest User

management.lua rewritten, v2

a guest
Aug 8th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[    $Id: management.lua 3447 2013-08-02 09:29:57Z sdkyron@gmail.com $       ]]
  2.  
  3. local _, caelCore = ...
  4.  
  5. ------------------------------------------------------------------------
  6. --  List of characters to run the addon for.
  7. -- I cleaned up the table format, as I couldn't see any code that
  8. -- depended on having so many extra table levels.
  9. ------------------------------------------------------------------------
  10.  
  11. local myToons = {
  12.     ["ServerName"] = {
  13.         ["CharacterA"]  = "DEATHKNIGHT",
  14.         ["CharacterB"] = "DRUID",
  15.         ["CharacterC"] = "HUNTER",
  16.         ["CharacterD"]  = "MAGE",
  17.         ["CharacterE"] = "MONK",
  18.         ["CharacterF"] = "WARLOCK",
  19.         ["CharacterG"]  = "WARRIOR",
  20.     }
  21. }
  22.  
  23. local herToons = {
  24.     ["ServerName"] = {
  25.         ["CharacterH"] = "DRUID",
  26.         ["CharacterI"] = "HUNTER",
  27.         ["CharacterJ"] = "MONK",
  28.         ["CharacterK"] = "PRIEST",
  29.         ["CharacterL"] = "ROGUE",
  30.         ["CharacterM"] = "SHAMAN",
  31.         ["CharacterN"] = "WARRIOR",
  32.     }
  33. }
  34.  
  35. local playerName = UnitName("player")
  36.  
  37. -- Don't make these global.
  38. local myChars = myToons[caelLib.playerRealm] and myToons[caelLib.playerRealm][playerName]
  39. local herChars = herToons[caelLib.playerRealm] and herToons[caelLib.playerRealm][playerName]
  40.  
  41. if not (myChars or herChars) then return end
  42.  
  43. ------------------------------------------------------------------------
  44. --  Basic initialization stuff.
  45. ------------------------------------------------------------------------
  46.  
  47. local Management = caelCore.createModule("Management")
  48. caelCore.management = Management
  49.  
  50. -- Avoid giant if-else chains by passing off events to individual functions.
  51. Management:SetScript("OnEvent", function(self, event, ...) return self[event](self, ...) end)
  52. Management:RegisterEvent("PLAYER_LOGIN")
  53.  
  54. function Management:PLAYER_LOGIN()
  55.     self:UnregisterEvent("PLAYER_LOGIN")
  56.  
  57.     -- If the GuildUI is already loaded, do it now, otherwise wait.
  58.     if IsAddOnLoaded("Blizzard_GuildUI") then
  59.         self:ADDON_LOADED("Blizzard_GuildUI")
  60.     else
  61.         self:RegisterEvent("ADDON_LOADED")
  62.     end
  63.  
  64.     self:RegisterEvent("CALENDAR_UPDATE_EVENT_LIST")
  65.  
  66.     self:RegisterEvent("GUILD_ROSTER_UPDATE")
  67.     GuildRoster() -- Forces a GUILD_ROSTER_UPDATE to fire soon.
  68. end
  69.  
  70. ------------------------------------------------------------------------
  71. -- Let the GuildUI load normally.
  72. ------------------------------------------------------------------------
  73.  
  74. function Management:ADDON_LOADED(addon)
  75.     if addon == "Blizzard_GuildUI" then
  76.         GuildRosterShowOfflineButton:Disable()
  77.  
  78.         local function GuildFrameHook(GuildFrame)
  79.             GuildFrameTab2:Click()
  80.             GuildRoster_SetView("guildStatus")
  81.             -- Currently not performed in _SetView
  82.             UIDropDownMenu_SetSelectedValue(GuildRosterViewDropdown, "guildStatus")
  83.         end
  84.         GuildFrame:HookScript("OnShow", GuildFrameHook)
  85.         GuildFrame:HookScript("OnHide", GuildFrameHook)
  86.  
  87.         self:UnregisterEvent("ADDON_LOADED")
  88.     end
  89. end
  90.  
  91. ------------------------------------------------------------------------
  92. --  List of calendar events changed.
  93. ------------------------------------------------------------------------
  94.  
  95. function Management:CALENDAR_UPDATE_EVENT_LIST()
  96.     self:ManageCalendar()
  97. end
  98.  
  99. ------------------------------------------------------------------------
  100. --  Guild roster changed.
  101. ------------------------------------------------------------------------
  102.  
  103. function Management:GUILD_ROSTER_UPDATE()
  104.     -- Are we in the right guild? Do we have any permissions?
  105.     if GetGuildInfo("player") == "We Did It" and (CanGuildPromote() or CanGuildRemove()) then
  106.         -- Should we defer to the other manager?
  107.         if herChars then
  108.             -- Is the other manager online?
  109.             local chars = myToons[caelLib.playerRealm]
  110.             for i = 1, GetNumGuildMembers() do
  111.                 local name = GetGuildRosterInfo(i)
  112.                 if chars[name] then
  113.                     -- Other manager is online. Let them handle it.
  114.                     return
  115.                 end
  116.             end
  117.         end
  118.  
  119.         -- Pass "true" to do it for real, eg. self:ManageGuild(true)
  120.         self:ManageGuild()
  121.     end
  122. end
  123.  
  124. ------------------------------------------------------------------------
  125. --  Actual calendar management logic.
  126. ------------------------------------------------------------------------
  127.  
  128. function Management:ManageCalendar()
  129.     for month = 0, 12 do
  130.         for day = 1, 31 do
  131.             for eventIndex = 1, CalendarGetNumDayEvents(month, day) do
  132.                 local _, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(month, day, eventIndex)
  133.                 if calendarType == "PLAYER" and CalendarContextInviteIsPending(month, day, eventIndex)
  134.                     if caelLib.isGuild(invitedBy) then -- Check this first so you don't have to check it twice.
  135.                         CalendarContextInviteAvailable(month, day, eventIndex)
  136.                     elseif invitedBy == "" then -- or (invitedBy ~= caelLib.playerName and not caelLib.isFriend(invitedBy) and not not caelLib.isInGroup(invitedBy)) then
  137.                         CalendarContextInviteRemove(month, day, eventIndex)
  138.                     end
  139.                 end
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. ------------------------------------------------------------------------
  146. --  Actual guild management logic, all wrapped up in a do-end block to
  147. -- limit scoping.
  148. ------------------------------------------------------------------------
  149.  
  150. do
  151.     local ignoreList = {
  152.         -- Use a hash table here so ignoreList[name] actually works.
  153.         ["Byleths"] = true,
  154.         ["Prøsper"] = true,
  155.         ["Trickaz"] = true,
  156.         ["Trøyà"] = true,
  157.         ["Winnø"] = true,
  158.     }
  159.  
  160.     local testMode, lastAction, lastName = true
  161.  
  162.     local group = Management:CreateAnimationGroup()
  163.     group.anim = ManageGuild:CreateAnimation()
  164.     group.anim:SetDuration(1)
  165.     group.anim:SetOrder(1)
  166.     group.parent = Management
  167.  
  168.     group:SetScript("OnFinished", function(self, forced)
  169.         if forced then
  170.             return
  171.         end
  172.  
  173.         local didAction
  174.         for playerIndex = 1, GetNumGuildMembers() do
  175.             local name, _, rankIndex, level, _, _, _, _, _, _, classFileName = GetGuildRosterInfo(playerIndex)
  176.             -- Make it consistent with values for SetGuildMemberRank() for sanity's sake.
  177.             rankIndex = rankIndex + 1
  178.  
  179.             if rankIndex > 3 then
  180.                 if CanGuildPromote() then
  181.                     local targetRank
  182.                     if level >= 1 and level <= 79 and rankIndex ~= 7 then
  183.                         targetRank = 7
  184.                     elseif level >= 80 and level <= 84 and rankIndex ~= 6 then
  185.                         targetRank = 6
  186.                     elseif level >= 85 and level <= 89 and rankIndex ~= 5 then
  187.                         targetRank = 5
  188.                     elseif level == 90 and rankIndex ~= 4 then
  189.                         targetRank = 4
  190.                     end
  191.                     if targetRank then
  192.                         if testMode then
  193.                             print("PROMOTE", name, targetRank)
  194.                         else
  195.                             if lastAction == "PROMOTE" and lastName == name then
  196.                                 return print("Action failed:", lastAction, lastName, targetRank)
  197.                             end
  198.                             didAction, lastAction, lastName = true, "PROMOTE", name
  199.                             SetGuildMemberRank(playerIndex, targetRank)
  200.                         end
  201.                         break
  202.                     end
  203.                 end
  204.  
  205.                 if not ignoreList[name] and CanGuildRemove() then
  206.                     local year, month, day = GetGuildRosterLastOnline(playerIndex)
  207.                     if year and (
  208.                         (rankIndex == 7
  209.                         -- level 1 for more than 1 day.
  210.                             and (day >= 1 and level == 1)
  211.                         -- level 55 DK for more than 2 days.
  212.                             or (day >= 2 and level == 55 and classFileName == "DEATHKNIGHT")
  213.                         -- level 2-5 and DK level 56-58 for more than 3 days.
  214.                             or (day >= 3 and ((level > 1 and level <= 5) or (level > 55 and level <= 58 and classFileName == "DEATHKNIGHT")))
  215.                         -- level 6-79 for more than 14 days.
  216.                             or (day >= 14 and level > 5 and level <= 79)
  217.                         )
  218.                         -- level 80-84 for more than 21 days.
  219.                         or (rankIndex == 6 and day >= 21)
  220.                         -- level 85-90 for more than 28 days.
  221.                         or ((rankIndex == 4 or rankIndex == 5) and day >= 28)
  222.                     ) then
  223.                         if testMode then
  224.                             print("REMOVE", name)
  225.                         else
  226.                             if lastAction == "REMOVE" and lastName == name then
  227.                                 -- Avoid infinite loops if the action is failing.
  228.                                 return print("Action failed:", lastAction, lastName)
  229.                             end
  230.                             didAction, lastAction, lastName = true, "REMOVE", name
  231.                             GuildUninvite(name)
  232.                         end
  233.                         break
  234.                     end
  235.                 end
  236.             end
  237.         end
  238.  
  239.         if didAction then
  240.             -- Did something, queue the next action.
  241.             self:Play()
  242.         else
  243.             -- Did nothing, we're done.
  244.             Management:RegisterEvent("GUILD_ROSTER_UPDATE")
  245.         end
  246.     end)
  247.  
  248.     function Management:ManageGuild(doItForReal)
  249.         self:UnregisterEvent("GUILD_ROSTER_UPDATE")
  250.  
  251.         SortGuildRoster("online")
  252.         SetGuildRosterShowOffline(true)
  253.  
  254.         if group:IsPlaying() then
  255.             group:Finish()
  256.         end
  257.  
  258.         testMode = not not doItForReal -- force boolean
  259.         group:Play()
  260.     end
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement