The error basically tells you what happened. The variable "count" does not exist when you use it, because you declare it after you try to use it. You also never set it to actually be a number. Even if you did, it's not saved anywhere outside the function (globally) and will never hold a count between function uses. This function saves the last time in the WA_STATS_LAST_UP variable, you need to save the count the same way. Your totals also have to be saved.
Also, the way you have the math now, you're simply adding the average with the current, but trying to divide it by an ever increasing count. The displayed averages would just keep getting smaller and smaller. You have to save the totals, then do the math in a separate variable.
Code:
function()
if not UnitAffectingCombat("player") then -- check if not in combat
WA_STATS_RESET=true -- set reset trigger for next combat
return WA_STATS_RETURN or "<no data>" -- keep displaying stats from last combat
end
if WA_STATS_RESET then -- first frame of combat, reset everything
WA_STATS_RESET=nil
WA_STATS_LAST=0
WA_STATS_COUNT=0
WA_STATS_TOTAL_MASTERY=0
WA_STATS_TOTAL_HASTE=0
WA_STATS_TOTAL_CRIT=0
end
local now=time() -- current time in total seconds since epoch (Jan 01 1970) (also called unix timestamp)
-- example: 1518586343 is Feb 14 2018 @ 5:32:23am (UTC)
if now>WA_STATS_LAST then -- if first time or one second since last time
local mastery=GetMasteryEffect()
local haste=UnitSpellHaste("player")
local crit=GetSpellCritChance(6)
if not (mastery and haste and crit) then -- just in case game doesn't give us any of the stats, prevent screwing up data
return WA_STATS_RETURN or "<no data>" -- still display what we already have
end
WA_STATS_LAST=now
WA_STATS_COUNT=WA_STATS_COUNT+1
-- add current stats to total
WA_STATS_TOTAL_MASTERY=WA_STATS_TOTAL_MASTERY+mastery
WA_STATS_TOTAL_HASTE=WA_STATS_TOTAL_HASTE+haste
WA_STATS_TOTAL_CRIT=WA_STATS_TOTAL_CRIT+crit
-- calculate averages
local averagemastery=WA_STATS_TOTAL_MASTERY/WA_STATS_COUNT
local averagehaste=WA_STATS_TOTAL_HASTE/WA_STATS_COUNT
local averagecrit=WA_STATS_TOTAL_CRIT/WA_STATS_COUNT
-- display new values
WA_STATS_RETURN=string.format([[Mastery: %.1f
Haste: %.1f
Crit: %.1f
Total Mastery: %.1f
Total Haste: %.1f
Total Crit: %.1f]],
mastery,
haste,
crit,
averagemastery,
averagehaste,
averagecrit
)
end
return WA_STATS_RETURN or "<no data>"
end
This should do what you want. After combat ends, the WeakAura will continue showing what you ended with last combat, then reset as soon as the next combat starts. I left comments in the code, they'll be easier to read in WeakAura's color coding instead of here.
I threw this together without testing, let me know if it works.