Reply
 
Thread Tools Display Modes
Old 03-11-13, 09:14 PM   #1
Transformis
A Defias Bandit
Join Date: Mar 2013
Posts: 2
Help expanding an Addon

I went looking for an addon that could detect when someone was spam-clicking group food items. I found one but it was outdated. I edited it to work with Banquet of the Wok, tested it and it works. Now I'd like to expand it to monitor all the new MoP banquets in a single addon. Here's what I'm working with so far.
Code:
local Food_Hog_Wok = CreateFrame("Frame", "Food_Hog", UIParent)
Food_Hog_Wok:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)
Food_Hog_Wok:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

local alert, started, players = "%s is spam-clicking the Banquet of the Wok! (%s)"

function Food_Hog_Wok:Print(msg)
	DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99Food_Hog|r: " .. msg)
end

function Food_Hog_Wok:UNIT_AURA(unit)
	if GetTime() - started > 180 then
		self:UnregisterEvent("UNIT_AURA")
		return
	end
	if UnitAura(unit, "Food") then
		local name, expiration = UnitName(unit), select(7, UnitAura(unit, "Food"))
		if players[name] and players[name].expiration ~= expiration and expiration - players[name].expiration < 20 then
			players[name].count = players[name].count + 1
			if players[name].count > 2 then
				self:Print(alert:format(name, players[name].count))
			end
		end
		players[name] = players[name] or { count = 0 }
		players[name].expiration = expiration
	end
end

function Food_Hog_Wok:COMBAT_LOG_EVENT_UNFILTERED(_, event, _,_,_,_,_,_,_,_,_, spellID)
	if event == "SPELL_CAST_START" and spellID == 126495 then
		started, players = GetTime(), {}
		self:RegisterEvent("UNIT_AURA")
		self:Print("Monitoring the Banquet of the Wok!")
	end
end
Transformis is offline   Reply With Quote
Old 03-11-13, 10:11 PM   #2
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,673
I'd do something like this:
Code:
local alert = "%s is hogging the %s! (%d)"

local foods = {
	[126503] = true, -- Banquet of the Brew
	[126504] = true, -- Great Banquet of the Brew
	[126492] = true, -- Banquet of the Grill
	[126494] = true, -- Great Banquet of the Grill
	[126501] = true, -- Banquet of the Oven
	[126502] = true, -- Great Banquet of the Oven
	[126497] = true, -- Banquet of the Pot
	[126498] = true, -- Great Banquet of the Pot
	[126499] = true, -- Banquet of the Steamer
	[126500] = true, -- Great Banquet of the Steamer
	[126595] = true, -- Banquet of the Wok
	[126596] = true, -- Great Banquet of the Wok
	[104958] = true, -- Pandaren Banquet
	[105193] = true, -- Great Pandaren Banquet
}

local counts = {}
local endTimes = {}

local started = 0

local f = CreateFrame("Frame", "FoodHog", UIParent)
f:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
f:SetScript("OnEvent", function(self, event, unit, spellName, _, _, spellID)
	if event == "UNIT_SPELLCAST_SUCCEEDED" then
		if foods[spellID] then
			started = GetTime()
			self:RegisterEvent("UNIT_AURA")
			print("|cff33ff99FoodHog:|r Monitoring", spellName)
		end
	else
		if GetTime() - started > 180 then
			return self:UnregisterEvent("UNIT_AURA")
		end
		local _, _, _, _, _, _, endTime = UnitAura(unit, "Food")
		if endTime then
			local name, server = UnitName(unit)
			if server and server ~= "" then
				name = name.."-"..server
			end
			local t = endTimes[name]
			if t and t ~= endTime and endTime - t < 20 then
				counts[name] = 1 + (counts[name] or 0)
				if counts[name] > 2 then
					print("|cff33ff99FoodHog:|r", format(alert, name, counts[name]))
				end
			end
			endTimes[name] = endTime
		end
	end
end)
UNIT_SPELLCAST_SUCCEEDED fires a lot less often that COMBAT_LOG_EVENT_UNFILTERED, and gives you the same info for your purposes, so you should use that.

Currently it doesn't track different banquets separately, so if someone clicks multiple banquets repeatedly, it will warn about them, but this seems fine.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Old 03-11-13, 11:14 PM   #3
Transformis
A Defias Bandit
Join Date: Mar 2013
Posts: 2
Wow, thanks!

This looks a lot simpler than the original.
Transformis is offline   Reply With Quote
Old 03-12-13, 01:56 AM   #4
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
Should

Lua Code:
  1. if server and server ~= "" then

be

Lua Code:
  1. if name and server ~= "" then

?
Clamsoda is offline   Reply With Quote
Old 03-12-13, 05:01 AM   #5
Phanx
A Pyroguard Emberseer
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 3,673
No. You'll always have a valid unit token from the UNIT_AURA event, so you'll always get a name from UnitName. You just need to check if the server name from UnitName is both (a) non-nil and (b) not an empty string, so you can append it to the name for cross-realm players.
__________________
Author/maintainer of Grid, PhanxChat, ShieldsUp, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
Phanx is offline   Reply With Quote
Old 03-12-13, 10:25 AM   #6
Clamsoda
A Cobalt Mageweaver
Join Date: Nov 2011
Posts: 233
Oh I see, I hadn't though about the difference between nil, and an empty string. Thank you very much for the explanation.
Clamsoda is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » Help expanding an Addon

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off