Module:Damage: Difference between revisions

From Elwiki
No edit summary
No edit summary
Line 43: Line 43:
     end
     end


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


     function enh(dmg)
     function enhanced(dmg)
         return dmg * 0.8
         return dmg * 0.8
     end
     end
Line 57: Line 57:
     local fvals = {}
     local fvals = {}
     local vars = {}
     local vars = {}
    local traits = {
        'heavy', 'empowered', 'enhanced'
    }


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


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


     if inArgsStarts('avg_hits') then
     if inArgsStarts('avg_hits') then
         getTotal('avg_hits', 'dmg', 'avg_hits', 'average')
         getTotal('dmg', 'avg_hits', 'average')
         table.insert(vars, var('average_damage', fvals.average))
         table.insert(vars, var('average_damage', fvals.average))
     end
     end
Line 84: Line 87:
     -- traits
     -- traits
     if inArgs('useful_penalty') then
     if inArgs('useful_penalty') then
         getTotal('hits_useful', 'dmg', 'hits_useful', 'total_useful')
         getTotal('dmg', 'hits_useful', 'total_useful')
         table.insert(vars, var('total_damage_useful', fvals.total_useful * args.useful_penalty))
         table.insert(vars, var('total_damage_useful', fvals.total_useful * args.useful_penalty))
     end
     end
     if inArgs('heavy') then
 
         table.insert(vars, var('total_damage_heavy', heavy(fvals.total)))
     for k,v in spairs(fvals) do
        for kt,vt in spairs(traits) do
            if inArgs(vt) then
                table.insert(vars, var(k..'_damage_'..vt), vt(fvals.k))
            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', empowered(fvals.total)))
        -- end
        -- if inArgs('enhanced') then
        --    table.insert(vars, var('total_damage_enhanced', enhanced(fvals.total)))
        -- end
     end
     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))
     return frame:preprocess(table.concat(vars))
end
end


return p
return p

Revision as of 20:33, 1 April 2022

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 = {}
    local traits = {
        'heavy', 'empowered', 'enhanced'
    }

    function getTotal(dmg, hits, fval)
        local i = 1
        fvals[fval] = 0
        for k,v in spairs(hits) 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', 'hits', 'total')
    table.insert(vars, var('total_damage', fvals.total))

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

    -- traits
    if inArgs('useful_penalty') then
        getTotal('dmg', 'hits_useful', 'total_useful')
        table.insert(vars, var('total_damage_useful', fvals.total_useful * args.useful_penalty))
    end

    for k,v in spairs(fvals) do
        for kt,vt in spairs(traits) do
            if inArgs(vt) then
                table.insert(vars, var(k..'_damage_'..vt), vt(fvals.k))
            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', empowered(fvals.total)))
        -- end
        -- if inArgs('enhanced') then
        --     table.insert(vars, var('total_damage_enhanced', enhanced(fvals.total)))
        -- end
    end

    return frame:preprocess(table.concat(vars))
end

return p