Guest User

Untitled

a guest
Mar 21st, 2013
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.44 KB | None | 0 0
  1.  
  2. -- only edit stuff below here
  3. ----------
  4.  
  5. local m_pets = "mypets" -- these four things must be the same on each persons addon
  6. local m_queue = "letsqueue"
  7. local m_pop = "poppinfresh"
  8. local m_no = "know"
  9.  
  10. local m_channel = "PARTY" -- channel to broadcast msg
  11. local m_target = nil -- only use this for m_channel == 'WHISPER' or 'CHANNEL' for chat messages
  12. local include_pet_names = true -- include pet names in the broadcast message, only really useful as a visual aid
  13.  
  14. local use_addon_msg = false -- using addon messages instead of chat messages
  15. local m_addon_prefix = "DIXBUTT" -- prefix to register
  16.  
  17. local max_decline_count = 5 -- if we have to decline a battle more than this number, just enter it to get the person out of the queue
  18. -- either manually forfiet or afk (afking prolly dodgy)
  19.  
  20. local a_cmd = "startomg" -- /command to start
  21.  
  22. ----------
  23. -- only edit stuff above here
  24.  
  25. local addon_name = ...
  26.  
  27. local popped = 0
  28. local popped_ok = false
  29.  
  30. local nextupdate = 0
  31.  
  32. local decline_count = 0
  33.  
  34. local enabled = false
  35.  
  36. local leader = false
  37.  
  38. local events_chat = {
  39.     "CHAT_MSG_ADDON",
  40.     "CHAT_MSG_CHANNEL",
  41.     "CHAT_MSG_EMOTE",
  42.     "CHAT_MSG_GUILD",
  43.     "CHAT_MSG_INSTANCE_CHAT",
  44.     "CHAT_MSG_INSTANCE_CHAT_LEADER",
  45.     "CHAT_MSG_OFFICER",
  46.     "CHAT_MSG_PARTY",
  47.     "CHAT_MSG_PARTY_LEADER",
  48.     "CHAT_MSG_RAID",
  49.     "CHAT_MSG_RAID_LEADER",
  50.     "CHAT_MSG_RAID_WARNING",
  51.     "CHAT_MSG_SAY",
  52.     "CHAT_MSG_WHISPER",
  53.     "CHAT_MSG_YELL",
  54. }
  55.  
  56. local function doprint(s)
  57.     DEFAULT_CHAT_FRAME:AddMessage(addon_name .. ": " .. s)
  58. end
  59.  
  60. local function isok()
  61.     if not enabled then return false end
  62.     if C_PetBattles.IsInBattle() then return false end -- if we're in a battle, don't bother
  63.     if not C_PetJournal.IsFindBattleEnabled() or not C_PetJournal.IsJournalUnlocked() then return false end -- if we can't even queue, dont bother
  64.     return true
  65. end
  66.  
  67. local function dosend(m)
  68.     if use_addon_msg then
  69.         if not IsAddonMessagePrefixRegistered(m_addon_prefix) then
  70.             if not RegisterAddonMessagePrefix(m_addon_prefix) then
  71.                 doprint("Error registering prefix: " .. m_addon_prefix)
  72.                 enabled = false
  73.             end
  74.         else
  75.             SendAddonMessage(m_addon_prefix, m, m_channel, m_target)
  76.         end
  77.     else
  78.         SendChatMessage(m, m_channel, nil, m_target)
  79.     end
  80. end
  81.  
  82. local function onceyoupop(us)
  83.     if GetTime() - popped < 2 then
  84.         nextupdate = GetTime() + 10
  85.         popped_ok = false
  86.         C_PetBattles.AcceptQueuedPVPMatch()
  87.     else
  88.         popped = GetTime()
  89.         popped_ok = true
  90.         if us then
  91.             dosend(m_pop)
  92.         end
  93.     end
  94. end
  95.  
  96. local z = CreateFrame("Button", nil, UIParent, "PetBattleActionButtonTemplate")
  97. z.Icon:SetTexture([[Interface\Icons\INV_Pet_PetTrap]])
  98. z:SetSize(200, 200)
  99. z:SetPoint("CENTER")
  100. z:SetScript("OnClick", function() C_PetBattles.ForfeitGame() end)
  101. z:Hide()
  102.  
  103. local function getpets()
  104.     local levels = ""
  105.     local names = ""
  106.     local count25 = 0
  107.     for i = 1, 3 do
  108.         local petID, ability1ID, ability2ID, ability3ID, locked = C_PetJournal.GetPetLoadOutInfo(i)
  109.         local speciesID, customName, level, xp, maxXp, displayID, isFavorite, name, icon, petType, creatureID, sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID(petID)
  110.         if level == 25 then
  111.             count25 = count25 + 1
  112.         end
  113.         levels = levels .. level .. " "
  114.         names = names .. name .. ", "
  115.     end
  116.     if count25 < 3 then
  117.         if include_pet_names and not use_addon_msg then -- dont include when using addon channel, sorta redundant
  118.             return strtrim(levels) .. " (" .. strtrim(names, " ,")
  119.         else
  120.             return strtrim(levels)
  121.         end
  122.     else
  123.         return nil
  124.     end
  125. end
  126.  
  127. local f = CreateFrame("Frame")
  128. f:RegisterEvent("PET_BATTLE_QUEUE_PROPOSE_MATCH")
  129. for _,v in pairs(events_chat) do -- all the events we should need for chat
  130.     f:RegisterEvent(v)
  131. end
  132. f:SetScript("OnEvent", function(self, event, ...)
  133.     if not isok() then return end
  134.     if event == "PET_BATTLE_QUEUE_PROPOSE_MATCH" then
  135.         onceyoupop(true)
  136.     elseif strfind(event, "CHAT_MSG_") then
  137.         local msg, sender
  138.         if event == "CHAT_MSG_ADDON" then
  139.             local prefix
  140.             prefix, msg, sender = ...
  141.             if prefix ~= m_addon_prefix then
  142.                 return
  143.             end
  144.         else
  145.             msg, sender = ...
  146.         end
  147.         if sender == UnitName('player') then return end
  148.         msg = strtrim(msg)
  149.         --
  150.         if msg == m_pop then
  151.             onceyoupop()
  152.         elseif strmatch(msg, m_pets .. " .*") then
  153.             if msg == m_pets .. " " .. m_no then
  154.                 enabled = false
  155.                 return
  156.             end
  157.             local levels = { strsplit(' ', strsub(msg, 6), 4) } -- creating table, prolly not a great idea
  158.             for i = 1, 3 do
  159.                 local j = tonumber(levels[i])
  160.                 local petID, ability1ID, ability2ID, ability3ID, locked = C_PetJournal.GetPetLoadOutInfo(i)
  161.                 local speciesID, customName, level, xp, maxXp, displayID, isFavorite, name, icon, petType, creatureID, sourceText, description, isWild, canBattle, tradable, unique = C_PetJournal.GetPetInfoByPetID(petID)
  162.                 for diff = 0, 2 do -- try to put in a pet as close to the level as possible
  163.                     if abs(level - j) > diff then
  164.                         local _, count = C_PetJournal.GetNumPets(PetJournal.isWild)
  165.                         for n = 1, count do -- iterate all our pets
  166.                             local e_petID, e_speciesID, e_isOwned, e_customName, e_level, e_favorite, e_isRevoked, e_name, e_icon, e_petType, e_creatureID, e_sourceText, e_description, e_isWildPet, e_canBattle = C_PetJournal.GetPetInfoByIndex(n, PetJournal.isWild)
  167.                             if e_canBattle and e_petID ~= petID and abs(e_level - j) <= diff then
  168.                                 C_PetJournal.SetPetLoadOutInfo(i, e_petID)
  169.                                 return
  170.                             end
  171.                         end
  172.                     end
  173.                 end
  174.                 if abs(level - j) > 2 then
  175.                     dosend(m_pets .. " " .. m_no)
  176.                     enabled = false
  177.                     return
  178.                 end
  179.             end
  180.             -- all pets have checked out
  181.             if GetTime() > nextupdate then
  182.                 popped_ok = false
  183.                 C_PetBattles.StartPVPMatchmaking()
  184.                 local pets = getpets()
  185.                 if pets then
  186.                     dosend(m_queue .. " " .. pets .. ")")
  187.                 end
  188.             end
  189.         elseif strmatch(msg, m_queue .. " .*") then
  190.             popped_ok = false
  191.             C_PetBattles.StartPVPMatchmaking()
  192.         end
  193.     end
  194. end)
  195. f:SetScript("OnUpdate", function(self, ...)
  196.     if not enabled then return end
  197.     if not PetJournal then PetJournal_LoadUI() end
  198.     C_PetJournal.ClearSearchFilter()
  199.     if C_PetBattles.IsInBattle() then
  200.         decline_count = 0
  201.         popped_ok = false
  202.         nextupdate = GetTime() + 10
  203.         z:Show()
  204.         return
  205.     else
  206.         z:Hide()
  207.     end
  208.     if C_PetBattles.GetPVPMatchmakingInfo() then
  209.         if popped_ok and GetTime() - popped > 2 then
  210.             decline_count = decline_count + 1
  211.             if max_decline_count > 0 and decline_count > max_decline_count then -- accept the pvp match just to get the person out of the queue, afk or manually abandon out
  212.                 C_PetBattles.AcceptQueuedPVPMatch()
  213.                 return
  214.             end
  215.             if C_PetBattles.GetPVPMatchmakingInfo() == "proposal" then
  216.                 C_PetBattles.DeclineQueuedPVPMatch()
  217.             else
  218.                 C_PetBattles.StopPVPMatchmaking()
  219.             end
  220.             popped_ok = false
  221.         end
  222.         return
  223.     end
  224.     if not isok() then return end
  225.     if leader and GetTime() > nextupdate then
  226.         nextupdate = GetTime() + 2
  227.         local pets = getpets()
  228.         if pets then
  229.             dosend(m_pets .. " " .. pets .. ")")
  230.         end
  231.     end
  232. end)
  233.  
  234. _G["SLASH_" .. addon_name .. "1"] = '/' .. a_cmd;
  235. SlashCmdList[addon_name] = function(...)
  236.     enabled = not enabled
  237.     if enabled and use_addon_msg and not IsAddonMessagePrefixRegistered(m_addon_prefix) then
  238.         if not RegisterAddonMessagePrefix(m_addon_prefix) then
  239.             doprint("Error registering prefix: " .. m_addon_prefix)
  240.             enabled = false
  241.         end
  242.     end
  243.     local msg = ...
  244.     leader = msg and strlen(msg) > 0
  245.     doprint((enabled and "enabled" or "disabled") .. ", leader: " .. tostring(leader))
  246. end
Advertisement
Add Comment
Please, Sign In to add comment