Module:Damage

From Elwiki
Revision as of 20:47, 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 inArgsStarts(key)
        for k,v in spairs(args) do
            if string.starts(k, key) then
                return true
            end
        end
    end

    function heavy(dmg)
        return dmg * 1.44
    end

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

    function enhanced(dmg)
        return dmg * 0.8
    end

    local fvals = {}
    local vars = {}

    function getTotal(arg, dmg, hits, fval)
        local i = 1
        fvals[fval] = 0
        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
        -- dosomething
    end

    -- total/avg
    getTotal('dmg', 'dmg', 'hits', 'total')
    table.insert(vars, var('total_damage', fvals.total))

    if inArgsStarts('avg_hits') then
        getTotal('avg_hits', 'dmg', 'avg_hits', 'average')
        table.insert(vars, var('average_damage', fvals.average))
    end

    -- traits
    if inArgs('useful_penalty') then
        getTotal('hits_useful', 'dmg', 'hits_useful', 'total_useful')
        table.insert(vars, var('total_damage_useful', fvals.total_useful * args.useful_penalty))
    end
    local traits = {
        heavy = 1.44,
        empowered = 1.2,
        enhanced = 0.8
    }
    for k,v in spairs(fvals) do
        for kt,vt in spairs(traits) do
            if inArgs(kt) then
                local out = ''
                if kt == 'heavy' then
                    out = heavy(v)
                elseif kt == 'empowered' then
                    out = empowered(v)
                elseif kt == 'enhanced' then
                    out = enhanced(v)
                end
                table.insert(vars, var(k..'_damage_'..kt, out))
            end
        end
    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