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
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Maximum allowed entries
local function getItemValue(name)
local MAX_INPUTS = 75
    -- Use mw.loadData if needed, or fetch values another way
local MAX_OUTPUTS = 75
    return tonumber(mw.ext.VariablesLua.var{name}) or 0
end


-- Main function to return only the profit as a number
function p.main(frame)
function p.profit(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
    local totalInput = 0
    local totalOutput = 0


     local inputs = calculateTotal(args, 'Input', MAX_INPUTS)
     -- Loop inputs
    local outputs = calculateTotal(args, 'Output', MAX_OUTPUTS)
     for i = 1, 20 do
 
         local name = args["Input" .. i]
    local profit = outputs - inputs
         local num = tonumber(args["Input" .. i .. "num"]) or 0
    return tostring(profit)
         local isPerHour = args["Input" .. i .. "isph"] == "y"
end
        if name then
 
            local value = getItemValue(name)
-- Calculates total value from prefixed args (Input/Output)
            if isPerHour then
function calculateTotal(args, prefix, max)
                num = num / (tonumber(args.kph) or 1)
    local total = 0
            end
 
            totalInput = totalInput + (value * num)
     for i = 1, max do
        end
         local name = args[prefix .. i]
    end
        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)
    -- Loop outputs
    for i = 1, 30 do
        local name = args["Output" .. i]
        local num = tonumber(args["Output" .. i .. "num"]) or 0
         if name then
            local value = getItemValue(name)
            totalOutput = totalOutput + (value * num)
        end
     end
     end


     return total
    local profit = totalOutput - totalInput
     return math.floor(profit + 0.5) -- Rounded profit
end
end


return p
return p

Latest revision as of 11:01, 5 May 2025

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

local p = {}

local function getItemValue(name)
    -- Use mw.loadData if needed, or fetch values another way
    return tonumber(mw.ext.VariablesLua.var{name}) or 0
end

function p.main(frame)
    local args = frame:getParent().args
    local totalInput = 0
    local totalOutput = 0

    -- Loop inputs
    for i = 1, 20 do
        local name = args["Input" .. i]
        local num = tonumber(args["Input" .. i .. "num"]) or 0
        local isPerHour = args["Input" .. i .. "isph"] == "y"
        if name then
            local value = getItemValue(name)
            if isPerHour then
                num = num / (tonumber(args.kph) or 1)
            end
            totalInput = totalInput + (value * num)
        end
    end

    -- Loop outputs
    for i = 1, 30 do
        local name = args["Output" .. i]
        local num = tonumber(args["Output" .. i .. "num"]) or 0
        if name then
            local value = getItemValue(name)
            totalOutput = totalOutput + (value * num)
        end
    end

    local profit = totalOutput - totalInput
    return math.floor(profit + 0.5) -- Rounded profit
end

return p