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

no edit summary
No edit summary
No edit summary
 
(32 intermediate revisions by the same user not shown)
Line 1:
-- Lua module to replicate the functionality of the Infobox Recipe template
local p = {}
 
function p.main_main(frame)
local args = frame:getParent().args
local mw = require('mw')
local currency = require('Module:Currency').parse
 
-- get the buy price from the query result
-- Define raw material costs based on Semantic MediaWiki queries
-- 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 mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
-- local rawmat2costrawmat1cost = args.rawmat2rawmat1 and getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat2rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1')) or "0"
-- local rawmat3costrawmat2cost = args.rawmat3rawmat2 and getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3rawmat2 .. ']]|?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 = (tonumber(rawmat1cost) or 0) * (tonumber(args.rawmat1qty) or 1)) +
-- (tonumber(rawmat2cost) or 0) * (tonumber(args.rawmat2qty) 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"
-- 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
 
-- Recipe Table Head
-- -- Calculate output cost total and profit
-- local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
-- local profit = output1costTotal - totalCost
 
-- -- Generate the output table
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, '|-')
-- table.insert(out, '! colspan="2" | Raw Ingredient')
-- table.insert(out,Add '!raw Quantity')materials
-- table.insert(out, '! Cost')Headers
-- table.insert(out, '! colspan="2" |- Raw Ingredient')
table.insert(out, '! Quantity')
 
table.insert(out, '! Cost')
-- -- Add each raw material to the table
table.insert(out, '|-')
-- 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;" | ' .. 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;" | ' .. 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;" | ' .. rawmat3cost)
-- table.insert(out, '|-')
-- end
 
-- -- Add total raw costValues
if args.rawmat1 then
-- table.insert(out, '! colspan="3" | Total Raw cost')
-- table.insert(out, '| style="text-align[[File:' right;".. | 'args.rawmat1 .. totalCost'.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 output datatotal andraw profitcost
-- table.insert(out, '! colspan="23" | OutputTotal Raw cost')
-- table.insert(out, '!| style="text-align: right;" | Quantity' .. currency(totalCost))
-- table.insert(out, '! Value|-')
-- table.insert(out, '|-')
-- Add Intermediate steps if required
-- table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
-- Headers
-- table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
-- table.insert(out, '|! stylecolspan="text-align: right;2" | Intermediate Ingredient' .. output1costTotal)
-- table.insert(out, '|-! Quantity')
-- table.insert(out, '! colspan="3" | ProfitFacility')
-- table.insert(out, '| style="text-align: right;" | ' .. profit)
-- table.insert(out, '|}')
-- Values
if args.preparedmat1 then
table.insert(out, '| [[File:' .. args.preparedmat1 .. '.png|30px]]')
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, '|-')
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 debug information to-- theAdd output as a simple paragraphdata
-- Headers
local debugMessage = 'Debug Info:<br>' ..
table.insert(out, '! colspan="2" | Output')
'facility: ' .. (args.facility) .. '<br>' ..
table.insert(out, '! Quantity')
'profession: ' .. (args.profession) .. '<br>' ..
table.insert(out, '! Value')
'level: ' .. (args.level) .. '<br>' ..
table.insert(out, '|-')
'exp: ' .. (args.exp) .. '<br>' ..
'rawmat1: ' .. (args.rawmat1) .. '<br>' ..
-- Values
'rawmat1qty: ' .. (args.rawmat1qty) .. '<br>' ..
table.insert(out, '| 'rawmat2[[File: ' .. (args.rawmat2)output1 .. '<br>.png|30px]]' ..)
table.insert(out, '| ' .. 'rawmat2qty:(args.output1 and '[[' .. (args.rawmat3qty)output1 .. '<br>]]' ..or 'Unknown'))
table.insert(out, '| style="text-align: right;" 'rawmat3:| ' .. (args.rawmat3)output1qty ..or '<br>' ..1))
table.insert(out, '| style="text-align: right;" | ' .. currency(output1costTotal))
'rawmat3qty: ' .. (args.rawmat3qty) .. '<br>' ..
table.insert(out, '|-')
'preparedmat1: ' .. (args.preparedmat1) .. '<br>' ..
'preparedmat1qty: ' .. (args.preparedmat1qty) .. '<br>' ..
-- Add profit data
'preparedmat1fac: ' .. (args.preparedmat1fac) .. '<br>' ..
table.insert(out, '! colspan="3" | Profit')
'output1: ' .. (args.output1) .. '<br>' ..
table.insert(out, '| style="text-align: right;" | ' .. currency(profit))
'output1qty: ' .. (args.output1qty) .. '<br>' ..
table.insert(out, '|}')
table.insert(out, debugMessage)
 
return table.concat(out, '\n')
526

edits