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

no edit summary
No edit summary
No edit summary
Line 7:
local currency = require('Module:Currency').parse
 
-- Defineget rawthe materialbuy costsprice basedfrom onthe Semanticquery MediaWiki queriesresult
local function getCost(queryResult)
if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
Line 15:
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 and output cost
local totalCost = (rawmat1cost * (tonumber(args.rawmat1qty) or 1)) +
(rawmat2cost * (tonumber(args.rawmat2qty) or 1)) +
(rawmat3cost * (tonumber(args.rawmat3qty) or 1))
local output1cost = args.output1 and getCost(mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')) or 0
 
-- Calculate output cost total and profit
local output1costTotaloutput1cost = output1costargs.output1 *and mw.smw.ask(tonumber('[[:+]][[' .. args.output1qty)output1 or.. ']]|?Value|limit=1')
local output1costTotal = output1cost[1]['Value'] * (tonumber(args.output1qty) or 1)
local profit = output1costTotal - totalCost
 
-- GenerateRecipe theTable outputHead table
local out = {}
table.insert(out, '{| class="wikitable"')
Line 35 ⟶ 36:
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')
Line 41 ⟶ 42:
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')
Line 50 ⟶ 54:
table.insert(out, '|-')
 
-- Values
-- Add each raw material to the table
if args.rawmat1 then
table.insert(out, '| [[File:' .. args.rawmat1 .. '.png|30px]]')
Line 75 ⟶ 79:
-- Add total raw cost
table.insert(out, '! colspan="3" | Total Raw cost')
table.insert(out, '| style="text-align: right;" | ' .. currency(totalCost))
table.insert(out, '|-')
Line 87 ⟶ 91:
-- Values
if args.preparedmat1 then
table.insert(out, '| [[File:' .. args.preparedmat1 .. '.png|30px]] [[' .. args.preparedmat1 .. ']]')
table.insert(out, '| [[' .. args.preparedmat1 .. ']]')
table.insert(out, '| style="text-align: right;" | ' .. (args.preparedmat1qty or 1))
table.insert(out, '| style="text-align: right;" | [[File:' .. args.preparedmat1fac .. '.png|30px]] [[' .. args.preparedmat1fac .. ']]')
table.insert(out, debugMessage'|-')
end
if args.preparedmat2 then
table.insert(out, '| [[File:' .. args.preparedmat2 .. '.png|30px]]')
table.insert(out, '| [[' .. args.preparedmat2 .. ']]')
table.insert(out, '| style="text-align: right;" | ' .. (args.preparedmat2qty or 1))
table.insert(out, '| style="text-align: right;" | [[File:' .. args.preparedmat2fac .. '.png|30px]] [[' .. args.preparedmat2fac .. ']]')
table.insert(out, '|-')
end
if args.preparedmat3 then
table.insert(out, '| [[File:' .. args.preparedmat3 .. '.png|30px]]')
table.insert(out, '| [[' .. args.preparedmat3 .. ']]')
table.insert(out, '| style="text-align: right;" | ' .. (args.preparedmat3qty or 1))
table.insert(out, '| style="text-align: right;" | [[File:' .. args.preparedmat3fac .. '.png|30px]] [[' .. args.preparedmat3fac .. ']]')
table.insert(out, '|-')
end
 
-- Add output data and profit
-- 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, '|}')
 
-- Add debug information to the output as a simple paragraph
local debugMessage = 'Debug Info:<br>' ..
'rawmat1query: ' .. (rawmat1query or 'nil') .. '<br>' ..
'rawmat1cost: ' .. tostring(rawmat1cost) .. '<br>' ..
'rawmat2query: ' .. (rawmat2query or 'nil') .. '<br>' ..
'rawmat2cost: ' .. tostring(rawmat2cost) .. '<br>' ..
'rawmat3query: ' .. (rawmat3query or 'nil') .. '<br>' ..
'rawmat3cost: ' .. tostring(rawmat3cost) .. '<br>' ..
'totalCost: ' .. tostring(totalCost) .. '<br>' ..
'output1query: ' .. (output1query or 'nil') .. '<br>' ..
'output1cost: ' .. tostring(output1cost) .. '<br>' ..
'output1costTotal: ' .. tostring(output1costTotal) .. '<br>' ..
'profit: ' .. tostring(profit)
 
table.insert(out, debugMessage)
 
return table.concat(out, '\n')
526

edits