Module:Damage

From Elwiki
Revision as of 19:18, 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 tableContains(set, key)
    return set[key] ~= nil
end

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

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

    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 total = 0
    local i = 1
    local vars = {}
    for k,v in spairs(args) do
        if string.starts(k, 'dmg') then
            total = total + args['dmg' .. i] * args['hits' .. i]
            i = i + 1
            table.insert(vars, '{{#vardefine:total_damage|'..total..'}}')
        end
    end
    if tableContains(args, 'heavy') then
        table.insert(vars, var('total_damage_heavy', heavy(total)))
    end
    if tableContains(args, 'empowered') then
        table.insert(vars, var('total_damage_empowered', emp(total)))
    end
    if tableContains(args, 'enh') then
        table.insert(vars, var('total_damage_enhanced', enh(total)))
    end
    return frame:preprocess(table.concat(vars))
end

return p