Module:Sandbox/User:The Gaffer/Modules/Infobox Single Step Recipe: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 5: Line 5:
local mw = require('mw')
local mw = require('mw')
local currency = require('Module:Currency').parse
local currency = require('Module:Currency').parse
local parse = require('Module:Param Parse')


-- get the buy price from the query result
-- get the buy price from the query result
Line 26: Line 27:
-- Calculate output cost total and profit
-- Calculate output cost total and profit
local output1cost = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1cost = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1costTotal = output1cost[1]['Value'] * (tonumber(args.output1qty) or 1)
local outputcost1Total
if (type(outputcost1) == "table" and outputcost1[1]['Value']) then
outputcost1Total = output1cost[1]['Value'] * (tonumber(args.output1qty) or 1)
elseif (type(outputcost1) == "number") then
outputcost1Total = output1cost * (tonumber(args.output1qty) or 1)
else
outputcost1Total = 0
end
local profit = output1costTotal - totalCost
local profit = output1costTotal - totalCost

if args.rawmat1 then
mw.smw.set({
["Uses item"] = args.rawmat1,
})
end
if args.rawmat2 then
mw.smw.set({
["Uses item"] = args.rawmat2,
})
end
if args.rawmat3 then
mw.smw.set({
["Uses item"] = args.rawmat3,
})
end



-- Recipe Table Head
-- Recipe Table Head

Latest revision as of 22:54, 15 November 2024

Documentation for this module may be created at Module:Sandbox/User:The Gaffer/Modules/Infobox Single Step Recipe/doc

local p = {}

function p._main(frame)
    local args = frame:getParent().args
    local mw = require('mw')
    local currency = require('Module:Currency').parse
    local parse = require('Module:Param Parse')

     -- get the buy price from the query result
    local function getCost(queryResult)
        if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
            return tonumber(queryResult[1]['Shop buy price']) or 0
        end
        return 0
    end

	-- Get buy prices of raw materials
    local rawmat1cost = args.rawmat1 and getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1')) or 0
    local rawmat2cost = args.rawmat2 and getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat2 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1')) or 0
    local rawmat3cost = args.rawmat3 and getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1')) or 0

    -- Calculate total cost
    local totalCost = (rawmat1cost * (tonumber(args.rawmat1qty) or 1)) +
                      (rawmat2cost * (tonumber(args.rawmat2qty) or 1)) +
                      (rawmat3cost * (tonumber(args.rawmat3qty) or 1))
    
    -- Calculate output cost total and profit
    local output1cost = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
    local output1costTotal = output1cost[1]['Value'] * (tonumber(args.output1qty) or 1)
    local profit = output1costTotal - totalCost

	 if args.rawmat1 then
        mw.smw.set({
            ["Uses item"] = args.rawmat1,
        })
    end
    if args.rawmat2 then
        mw.smw.set({
            ["Uses item"] = args.rawmat2,
        })
    end
    if args.rawmat3 then
        mw.smw.set({
            ["Uses item"] = args.rawmat3,
        })
    end


    -- Recipe Table Head 
    local out = {}
    table.insert(out, '{| class="wikitable"')
    table.insert(out, '! colspan="4" | Requirements')
    table.insert(out, '|-')
    table.insert(out, '! colspan="2" | Facility')
    table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
    table.insert(out, '|-')
    table.insert(out, '! colspan="2" | Profession')
    table.insert(out, '! Level')
    table.insert(out, '! XP')
    table.insert(out, '|-')
    table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
    table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
    table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
    table.insert(out, '|-')
    
    -- Add raw materials
    -- Headers
    table.insert(out, '! colspan="2" | Raw Ingredient')
    table.insert(out, '! Quantity')
    table.insert(out, '! Cost')
    table.insert(out, '|-')

    -- Values
    if args.rawmat1 then
        table.insert(out, '| [[File:' .. args.rawmat1 .. '.png|30px]]')
        table.insert(out, '| [[' .. args.rawmat1 .. ']]')
        table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat1qty or 1))
        table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat1cost))
        table.insert(out, '|-')
    end
    if args.rawmat2 then
        table.insert(out, '| [[File:' .. args.rawmat2 .. '.png|30px]]')
        table.insert(out, '| [[' .. args.rawmat2 .. ']]')
        table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat2qty or 1))
        table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat2cost))
        table.insert(out, '|-')
    end
    if args.rawmat3 then
        table.insert(out, '| [[File:' .. args.rawmat3 .. '.png|30px]]')
        table.insert(out, '| [[' .. args.rawmat3 .. ']]')
        table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat3qty or 1))
        table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat3cost))
        table.insert(out, '|-')
    end

    -- Add total raw cost
    table.insert(out, '! colspan="3" | Total Raw cost')
    table.insert(out, '| style="text-align: right;" | ' .. currency(totalCost))
    table.insert(out, '|-')
    
    -- Add output data
    -- Headers
    table.insert(out, '! colspan="2" | Output')
    table.insert(out, '! Quantity')
    table.insert(out, '! Value')
    table.insert(out, '|-')
    
    -- Values
    table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]')
    table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
    table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
    table.insert(out, '| style="text-align: right;" | ' .. currency(output1costTotal))
    table.insert(out, '|-')
    
    -- Add profit data
    table.insert(out, '! colspan="3" | Profit')
    table.insert(out, '| style="text-align: right;" | ' .. currency(profit))
    table.insert(out, '|}')

    return table.concat(out, '\n')
end

return p