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

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 7: Line 7:


-- Define raw material costs based on Semantic MediaWiki queries
-- Define raw material costs based on Semantic MediaWiki queries
local function getCost(queryResult)
-- local function getCost(queryResult)
if type(queryResult) == "table" and queryResult[1] then
-- if type(queryResult) == "table" and queryResult[1] then
return tonumber(queryResult[1]) or 0
-- return tonumber(queryResult[1]) or 0
end
-- end
return 0
-- return 0
end
-- end


local rawmat1cost = args.rawmat1 and mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
-- local rawmat1cost = args.rawmat1 and 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 mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat2 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
-- local rawmat2cost = args.rawmat2 and 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 mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
-- local rawmat3cost = args.rawmat3 and mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"


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


-- Calculate output cost total and profit
-- -- Calculate output cost total and profit
local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
-- local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
local profit = output1costTotal - totalCost
-- local profit = output1costTotal - totalCost


-- Generate the output table
-- -- Generate the output table
local out = {}
-- local out = {}
table.insert(out, '{| class="wikitable"')
-- table.insert(out, '{| class="wikitable"')
table.insert(out, '! colspan="4" | Requirements')
-- table.insert(out, '! colspan="4" | Requirements')
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '! colspan="2" | Facility')
-- table.insert(out, '! colspan="2" | Facility')
table.insert(out, '! colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
-- table.insert(out, '! colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '! colspan="2" | Profession')
-- table.insert(out, '! colspan="2" | Profession')
table.insert(out, '! Level')
-- table.insert(out, '! Level')
table.insert(out, '! XP')
-- table.insert(out, '! XP')
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '| colspan="2" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
-- table.insert(out, '| colspan="2" | ' .. (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.level or 'Unknown'))
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
-- table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '! colspan="2" | Raw Ingredient')
-- table.insert(out, '! colspan="2" | Raw Ingredient')
table.insert(out, '! Quantity')
-- table.insert(out, '! Quantity')
table.insert(out, '! Cost')
-- table.insert(out, '! Cost')
table.insert(out, '|-')
-- table.insert(out, '|-')


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


-- Add total raw cost
-- -- Add total raw cost
table.insert(out, '! colspan="3" | Total Raw cost')
-- table.insert(out, '! colspan="3" | Total Raw cost')
table.insert(out, '| style="text-align: right;" | ' .. totalCost)
-- table.insert(out, '| style="text-align: right;" | ' .. totalCost)
table.insert(out, '|-')
-- table.insert(out, '|-')


-- Add output data and profit
-- -- Add output data and profit
table.insert(out, '! colspan="2" | Output')
-- table.insert(out, '! colspan="2" | Output')
table.insert(out, '! Quantity')
-- table.insert(out, '! Quantity')
table.insert(out, '! Value')
-- table.insert(out, '! Value')
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
-- 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;" | ' .. (args.output1qty or 1))
table.insert(out, '| style="text-align: right;" | ' .. output1costTotal)
-- table.insert(out, '| style="text-align: right;" | ' .. output1costTotal)
table.insert(out, '|-')
-- table.insert(out, '|-')
table.insert(out, '! colspan="3" | Profit')
-- table.insert(out, '! colspan="3" | Profit')
table.insert(out, '| style="text-align: right;" | ' .. profit)
-- table.insert(out, '| style="text-align: right;" | ' .. profit)
table.insert(out, '|}')
-- table.insert(out, '|}')


-- Add debug information to the output as a simple paragraph
-- Add debug information to the output as a simple paragraph
local debugMessage = 'Debug Info:<br>' ..
local debugMessage = 'Debug Info:<br>' ..
'params: ' .. (params) .. '<br>' ..
'facility: ' .. (args.facility) .. '<br>' ..
'rawmat1query: ' .. (rawmat1query or 'nil') .. '<br>' ..
'profession: ' .. (args.profession) .. '<br>' ..
'rawmat1cost: ' .. tostring(rawmat1cost) .. '<br>' ..
'level: ' .. (args.level) .. '<br>' ..
'rawmat2query: ' .. (rawmat2query or 'nil') .. '<br>' ..
'exp: ' .. (args.exp) .. '<br>' ..
'rawmat2cost: ' .. tostring(rawmat2cost) .. '<br>' ..
'rawmat1: ' .. (args.rawmat1) .. '<br>' ..
'rawmat3query: ' .. (rawmat3query or 'nil') .. '<br>' ..
'rawmat1qty: ' .. (args.rawmat1qty) .. '<br>' ..
'rawmat3cost: ' .. tostring(rawmat3cost) .. '<br>' ..
'rawmat2: ' .. (args.rawmat2) .. '<br>' ..
'totalCost: ' .. tostring(totalCost) .. '<br>' ..
'rawmat2qty: ' .. (args.rawmat3qty) .. '<br>' ..
'output1query: ' .. (output1query or 'nil') .. '<br>' ..
'rawmat3: ' .. (args.rawmat3) .. '<br>' ..
'output1cost: ' .. tostring(output1cost) .. '<br>' ..
'rawmat3qty: ' .. (args.rawmat3qty) .. '<br>' ..
'output1costTotal: ' .. tostring(output1costTotal) .. '<br>' ..
'preparedmat1: ' .. (args.preparedmat1) .. '<br>' ..
'profit: ' .. tostring(profit)
'preparedmat1qty: ' .. (args.preparedmat1qty) .. '<br>' ..
'preparedmat1fac: ' .. (args.preparedmat1fac) .. '<br>' ..

'output1: ' .. (args.output1) .. '<br>' ..
'output1qty: ' .. (args.output1qty) .. '<br>' ..
table.insert(out, debugMessage)
table.insert(out, debugMessage)