Module:Mmgtable2

From Roat Pkz
Revision as of 10:56, 5 May 2025 by Hefner (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

local MAX_INPUTS = 10
local MAX_OUTPUTS = 10

local function calculateTotal(args, prefix, max)
    local total = 0
    for i = 1, max do
        local num = tonumber(args[prefix .. i .. 'num']) or 0
        local value = tonumber(args[prefix .. i .. 'value']) or 0
        total = total + (num * value)
    end
    return total
end

function p.profit(frame)
    local args = frame:getParent().args
    local frameArgs = frame.args

    -- Merge frame.args into args in case of direct #invoke
    for k, v in pairs(frameArgs) do
        if v ~= '' then
            args[k] = v
        end
    end

    local inputs = calculateTotal(args, 'Input', MAX_INPUTS)
    local outputs = calculateTotal(args, 'Output', MAX_OUTPUTS)

    local profit = outputs - inputs
    return tostring(profit)
end

return p