Module:Damage

From Elwiki
Revision as of 20:09, 1 April 2022 by Ritsu (talk | contribs)

Documentation for this module may be created at Module:Damage/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}
function string.starts(String,Start)
    return string.sub(String,1,string.len(Start))==Start
end

function spairs(t)
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end
    table.sort(keys)
    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

function var(name, dmg)
    return '{{#vardefine:'..name..'|'..dmg..'}}'
end

function p.list(frame)
    local args = getArgs(frame)

    function inArgs(key)
        if args[key] ~= nil then
            return true
        end
    end

    function heavy(dmg)
        return dmg * 1.44
    end

    function emp(dmg)
        if args.empowered == 'true' then
            return dmg * 1.2
        else
            return dmg * args.empowered
        end
    end

    function enh(dmg)
        return dmg * 0.8
    end

    local fvals = {
        total = 0,
        total_useful = 0,
        avg = 0,
        avg_useful = 0
    }
    local vars = {}

    function getTotal(arg, dmg, hits, fval)
        local i = 1
        for k,v in spairs(args) do
            if string.starts(k, arg) then
                fvals[fval] = fvals[fval] + args[dmg .. i] * args[hits .. i]
                i = i + 1
            end
        end
    end

    for k,v in spairs(args) do
        -- -- total damage only
        -- if string.starts(k, 'dmg') then
        --     total = total + args['dmg' .. i] * args['hits' .. i]
        --     i = i + 1
        -- -- check if useful provided. if so, output total damage with useful
        -- elseif string.starts(k, 'hits_useful') then
        --     total_useful = total_useful + args['dmg' .. i2] * args['hits_useful' .. i2]
        --     i2 = i2 + 1
        -- end
    end
    getTotal('dmg', 'dmg', 'hits', 'total')
    getTotal('hits_useful', 'dmg', 'hits_useful', 'total_useful')
    table.insert(vars, var('total_damage', fvals.total))
    if inArgs('useful_penalty') then
        table.insert(vars, var('total_damage_useful', fvals.total_useful * args.useful_penalty))
    end
    if inArgs('heavy') then
        table.insert(vars, var('total_damage_heavy', heavy(fvals.total)))
    end
    if inArgs('empowered') then
        table.insert(vars, var('total_damage_empowered', emp(fvals.total)))
    end
    if inArgs('enhanced') then
        table.insert(vars, var('total_damage_enhanced', enh(fvals.total)))
    end
    return frame:preprocess(table.concat(vars))
end

return p