WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Help expanding an Addon (https://www.wowinterface.com/forums/showthread.php?t=46036)

Transformis 03-11-13 09:14 PM

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


Phanx 03-11-13 10:11 PM

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.

Transformis 03-11-13 11:14 PM

Wow, thanks!

This looks a lot simpler than the original.

Clamsoda 03-12-13 01:56 AM

Should

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

be

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

?

Phanx 03-12-13 05:01 AM

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.

Clamsoda 03-12-13 10:25 AM

Oh I see, I hadn't though about the difference between nil, and an empty string. Thank you very much for the explanation.


All times are GMT -6. The time now is 04:21 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI