Module:Mmgtable2: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
local p = {}
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)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local inputs = {}
     local totalInput = 0
     local outputs = {}
     local totalOutput = 0


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


         local outnum = tonumber(args["Output" .. i .. "num"]) or 0
    -- Loop outputs
         local outvalue = tonumber(args["Output" .. i .. "value"]) or 0
    for i = 1, 30 do
         table.insert(outputs, outnum * outvalue)
         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


     local inputTotal = 0
     local profit = totalOutput - totalInput
     for _, v in ipairs(inputs) do inputTotal = inputTotal + v end
     return math.floor(profit + 0.5) -- Rounded profit
 
    local outputTotal = 0
    for _, v in ipairs(outputs) do outputTotal = outputTotal + v end
 
    local profit = outputTotal - inputTotal
    return tostring(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