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