Advertisement
Guest User

management.lua, rev 4

a guest
Aug 9th, 2013
43
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 ChangeView()
  79.             GuildFrameTab2:Click()
  80.             GuildRoster_SetView("guildStatus")
  81.             -- Currently not performed in _SetView
  82.             UIDropDownMenu_SetSelectedValue(GuildRosterViewDropdown, "guildStatus")
  83.         end
  84.         GuildFrame:HookScript("OnShow", ChangeView)
  85.         GuildFrame:HookScript("OnHide", function()
  86.             ChangeView()
  87.             Management:GUILD_ROSTER_UPDATE()
  88.         end)
  89.  
  90.         self:UnregisterEvent("ADDON_LOADED")
  91.     end
  92. end
  93.  
  94. ------------------------------------------------------------------------
  95. --  List of calendar events changed.
  96. ------------------------------------------------------------------------
  97.  
  98. function Management:CALENDAR_UPDATE_EVENT_LIST()
  99.     self:ManageCalendar()
  100. end
  101.  
  102. ------------------------------------------------------------------------
  103. --  Guild roster changed.
  104. ------------------------------------------------------------------------
  105.  
  106. function Management:GUILD_ROSTER_UPDATE()
  107.     if GuildFrame and GuildFrame:IsShown() then
  108.         return
  109.     end
  110.     -- Are we in the right guild? Do we have any permissions?
  111.     if GetGuildInfo("player") == "We Did It" and (CanGuildPromote() or CanGuildRemove()) then
  112.         -- Should we defer to the other manager?
  113.         if herChars then
  114.             -- Is the other manager online?
  115.             local chars = myToons[caelLib.playerRealm]
  116.             for i = 1, GetNumGuildMembers() do
  117.                 local name = GetGuildRosterInfo(i)
  118.                 if chars[name] then
  119.                     -- Other manager is online. Let them handle it.
  120.                     return
  121.                 end
  122.             end
  123.         end
  124.  
  125.         -- Pass "true" to do it for real, eg. self:ManageGuild(true)
  126.         self:ManageGuild()
  127.     end
  128. end
  129.  
  130. ------------------------------------------------------------------------
  131. --  Actual calendar management logic.
  132. ------------------------------------------------------------------------
  133.  
  134. function Management:ManageCalendar()
  135.     for month = 0, 12 do
  136.         for day = 1, 31 do
  137.             for eventIndex = 1, CalendarGetNumDayEvents(month, day) do
  138.                 local _, _, _, calendarType, _, _, _, _, _, invitedBy = CalendarGetDayEvent(month, day, eventIndex)
  139.                 if calendarType == "PLAYER" and CalendarContextInviteIsPending(month, day, eventIndex)
  140.                     if caelLib.isGuild(invitedBy) then -- Check this first so you don't have to check it twice.
  141.                         CalendarContextInviteAvailable(month, day, eventIndex)
  142.                     elseif invitedBy == "" then -- or (invitedBy ~= caelLib.playerName and not caelLib.isFriend(invitedBy) and not not caelLib.isInGroup(invitedBy)) then
  143.                         CalendarContextInviteRemove(month, day, eventIndex)
  144.                     end
  145.                 end
  146.             end
  147.         end
  148.     end
  149. end
  150.  
  151. ------------------------------------------------------------------------
  152. --  Actual guild management logic, all wrapped up in a do-end block to
  153. --  limit scoping.
  154. ------------------------------------------------------------------------
  155.  
  156. do
  157.     local ignoreList = {
  158.         -- Use a hash table here so ignoreList[name] actually works.
  159.         ["Byleths"] = true,
  160.         ["Prøsper"] = true,
  161.         ["Trickaz"] = true,
  162.         ["Trøyà"] = true,
  163.         ["Winnø"] = true,
  164.     }
  165.  
  166.     local testMode, lastAction, lastName = true
  167.  
  168.     local group = Management:CreateAnimationGroup()
  169.     group.anim = ManageGuild:CreateAnimation()
  170.     group.anim:SetDuration(1)
  171.     group.anim:SetOrder(1)
  172.     group.parent = Management
  173.  
  174.     group:SetScript("OnFinished", function(self, forced)
  175.         if forced or (GuildFrame and GuildFrame:IsShown()) then
  176.             return
  177.         end
  178.  
  179.         local didAction
  180.         for playerIndex = 1, GetNumGuildMembers() do
  181.             local name, _, rankIndex, level, _, _, _, _, _, _, classFileName = GetGuildRosterInfo(playerIndex)
  182.             -- Make it consistent with values for SetGuildMemberRank() for sanity's sake.
  183.             rankIndex = rankIndex + 1
  184.  
  185.             if rankIndex > 3 then
  186.                 if CanGuildPromote() then
  187.                     local targetRank
  188.                     if level >= 1 and level <= 79 and rankIndex ~= 7 then
  189.                         targetRank = 7
  190.                     elseif level >= 80 and level <= 84 and rankIndex ~= 6 then
  191.                         targetRank = 6
  192.                     elseif level >= 85 and level <= 89 and rankIndex ~= 5 then
  193.                         targetRank = 5
  194.                     elseif level == 90 and rankIndex ~= 4 then
  195.                         targetRank = 4
  196.                     end
  197.                     if targetRank then
  198.                         if testMode then
  199.                             print("PROMOTE", name, targetRank)
  200.                         else
  201.                             if lastAction == "PROMOTE" and lastName == name then
  202.                                 return print("Action failed:", lastAction, lastName, targetRank)
  203.                             end
  204.                             didAction, lastAction, lastName = true, "PROMOTE", name
  205.                             SetGuildMemberRank(playerIndex, targetRank)
  206.                         end
  207.                         break
  208.                     end
  209.                 end
  210.  
  211.                 if not ignoreList[name] and CanGuildRemove() then
  212.                     local year, month, day = GetGuildRosterLastOnline(playerIndex)
  213.                     if year and (
  214.                         (rankIndex == 7
  215.                         -- level 1 for more than 1 day.
  216.                             and (day >= 1 and level == 1)
  217.                         -- level 55 DK for more than 2 days.
  218.                             or (day >= 2 and level == 55 and classFileName == "DEATHKNIGHT")
  219.                         -- level 2-5 and DK level 56-58 for more than 3 days.
  220.                             or (day >= 3 and ((level > 1 and level <= 5) or (level > 55 and level <= 58 and classFileName == "DEATHKNIGHT")))
  221.                         -- level 6-79 for more than 14 days.
  222.                             or (day >= 14 and level > 5 and level <= 79)
  223.                         )
  224.                         -- level 80-84 for more than 21 days.
  225.                         or (rankIndex == 6 and day >= 21)
  226.                         -- level 85-90 for more than 28 days.
  227.                         or ((rankIndex == 4 or rankIndex == 5) and day >= 28)
  228.                     ) then
  229.                         if testMode then
  230.                             print("REMOVE", name)
  231.                         else
  232.                             if lastAction == "REMOVE" and lastName == name then
  233.                                 -- Avoid infinite loops if the action is failing.
  234.                                 return print("Action failed:", lastAction, lastName)
  235.                             end
  236.                             didAction, lastAction, lastName = true, "REMOVE", name
  237.                             GuildUninvite(name)
  238.                         end
  239.                         break
  240.                     end
  241.                 end
  242.             end
  243.         end
  244.  
  245.         if didAction then
  246.             -- Did something, queue the next action.
  247.             self:Play()
  248.         else
  249.             -- Did nothing, we're done.
  250.             Management:RegisterEvent("GUILD_ROSTER_UPDATE")
  251.         end
  252.     end)
  253.  
  254.     function Management:ManageGuild(doItForReal)
  255.         self:UnregisterEvent("GUILD_ROSTER_UPDATE")
  256.  
  257.         SortGuildRoster("online")
  258.         SetGuildRosterShowOffline(true)
  259.  
  260.         if group:IsPlaying() then
  261.             group:Finish()
  262.         end
  263.  
  264.         testMode = not doItForReal
  265.         group:Play()
  266.     end
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement