1. #1

    Help with WA LUA

    Hi everyone,

    I'm trying to adapt some code from 2013 (originally posted by Archimand)

    Code:
    function ()
        local now = GetTime();
        WA_STATS_LAST_UP = WA_STATS_LAST_UP or now - 1;
        if now >= WA_STATS_LAST_UP + 1 then
            WA_STATS_LAST_UP = now;
            local mas = GetMasteryEffect();                         
            local has = UnitSpellHaste("player");                     
            local crit= GetSpellCritChance(6);
            local Tmas = 0 or (Tmas + mas)/count;
            local Thas = 0 or (Thas + has)/count;
            local Tcrit = 0 or (Tcrit + crit)/count;
            local count = count + 1;
            WA_STATS_RETURN = string.format([[
    Mastery: %.1f
    Haste: %.1f
    Crit: %.1f
    Total Mastery: %.1f
    Total Haste: %.1f
    Total Crit: %.1f]], 
                mas or 0, 
                has or 0, 
                crit or 0,
                Tmas or 0,
                Thas or 0,
                Tcrit or 0
            );
        end
        
        return  WA_STATS_RETURN or "<no data>";
    end
    Basically, the code originally showed current stats for mastery, haste, and crit. I wanted to adapt it to show me average stats over a fight. My idea was to sum each stat every 1 second and then divide it by the count.

    But I'm looking for help to reset the summation after combat ends, and to only start it when combat is engaged.

    Current code doesn't work due to an error
    Locals: errorMessage = "[string "--[[ Error in ' Stats' ]]return function ()..."]:12: attempt to perform arithmetic on global 'count' (a nil value)"
    DisplayMessageInternal = <function> defined @Interface\SharedXML\SharedBasicControls.lua:191
    MESSAGE_TYPE_ERROR = 0
    Please do one thing on the forums, read the whole post first, form an opinion, and then state whether you agree or disagree.

  2. #2
    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.
    Originally Posted by Zarhym (Blue Tracker)
    this thread is a waste of internet

  3. #3
    Thanks Kanegasi,

    I found out some of your solutions, but your use of UnitAffectingCombat and the reset is going to be helpful.

    I'm playing around with trying to get the stats to show on line, instead of two (i.e. Mastery: "Average mastery", "Current Mastery") but i like playing around and learning the code.
    Please do one thing on the forums, read the whole post first, form an opinion, and then state whether you agree or disagree.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •