Module:Mmgtable2: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
(Replaced content with "local p = {} -- Maximum allowed entries local MAX_INPUTS = 75 local MAX_OUTPUTS = 75 -- Main function to return only the profit as a number function p.profit(frame) local args = frame:getParent().args local inputs = calculateTotal(args, 'Input', MAX_INPUTS) local outputs = calculateTotal(args, 'Output', MAX_OUTPUTS) local profit = outputs - inputs return tostring(profit) end -- Calculates total value from prefixed args (Input/Output) function...")
Tag: Replaced
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Maximum allowed entries
local MAX_INPUTS = 10
local MAX_INPUTS = 75
local MAX_OUTPUTS = 10
local MAX_OUTPUTS = 75
 
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


-- Main function to return only the profit as a number
function p.profit(frame)
function p.profit(frame)
     local args = frame:getParent().args
     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 inputs = calculateTotal(args, 'Input', MAX_INPUTS)
Line 14: Line 30:
     local profit = outputs - inputs
     local profit = outputs - inputs
     return tostring(profit)
     return tostring(profit)
end
-- Calculates total value from prefixed args (Input/Output)
function calculateTotal(args, prefix, max)
    local total = 0
    for i = 1, max do
        local name = args[prefix .. i]
        if not name or name == '' then break end
        local qty = tonumber(args[prefix .. i .. 'num']) or 1
        local value = tonumber(args[prefix .. i .. 'value']) or 0
        total = total + (qty * value)
    end
    return total
end
end


return p
return p

Revision as of 10:56, 5 May 2025

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