Editing Module:Sandbox/User:The Gaffer/Modules/Infobox Recipe

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
-- Lua module to replicate the functionality of the Infobox Recipe template
local p = {}
local p = {}


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


-- Define raw material costs based on Semantic MediaWiki queries
-- get the buy price from the query result
local function getCost(queryResult)
-- local function getCost(queryResult)
if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
-- if type(queryResult) == "table" and queryResult[1] then
return tonumber(queryResult[1]['Shop buy price']) 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"
-- 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 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 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 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 getCost(mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1')) or 0


-- Calculate total cost
-- Calculate total cost and output cost
local totalCost = (rawmat1cost * (tonumber(args.rawmat1qty) or 1)) +
-- local totalCost = (tonumber(rawmat1cost) or 0) * (tonumber(args.rawmat1qty) or 1) +
(rawmat2cost * (tonumber(args.rawmat2qty) or 1)) +
-- (tonumber(rawmat2cost) or 0) * (tonumber(args.rawmat2qty) or 1) +
(rawmat3cost * (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"
-- 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


-- -- Calculate output cost total and profit
-- Recipe Table Head
-- local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
local out = {}
-- local profit = output1costTotal - totalCost
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, '|-')


-- -- Generate the output table
-- Values
if args.rawmat1 then
-- local out = {}
table.insert(out, '| [[File:' .. args.rawmat1 .. '.png|30px]]')
-- table.insert(out, '{| class="wikitable"')
table.insert(out, '| [[' .. args.rawmat1 .. ']]')
-- table.insert(out, '! colspan="4" | Requirements')
table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat1qty or 1))
-- table.insert(out, '|-')
table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat1cost))
-- table.insert(out, '! colspan="2" | Facility')
table.insert(out, '|-')
-- table.insert(out, '! colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
-- table.insert(out, '|-')
end
-- table.insert(out, '! colspan="2" | Profession')
if args.rawmat2 then
table.insert(out, '| [[File:' .. args.rawmat2 .. '.png|30px]]')
-- table.insert(out, '! Level')
table.insert(out, '| [[' .. args.rawmat2 .. ']]')
-- table.insert(out, '! XP')
table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat2qty or 1))
-- table.insert(out, '|-')
table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat2cost))
-- table.insert(out, '| colspan="2" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
table.insert(out, '|-')
-- table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
-- table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
end
if args.rawmat3 then
-- table.insert(out, '|-')
table.insert(out, '| [[File:' .. args.rawmat3 .. '.png|30px]]')
-- table.insert(out, '! colspan="2" | Raw Ingredient')
table.insert(out, '| [[' .. args.rawmat3 .. ']]')
-- table.insert(out, '! Quantity')
table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat3qty or 1))
-- table.insert(out, '! Cost')
table.insert(out, '| style="text-align: right;" | ' .. currency(rawmat3cost))
-- table.insert(out, '|-')
table.insert(out, '|-')
end


-- Add total raw cost
-- -- Add each raw material to the table
-- if args.rawmat1 then
table.insert(out, '! colspan="3" | Total Raw cost')
table.insert(out, '| style="text-align: right;" | ' .. currency(totalCost))
-- table.insert(out, '| [[File:' .. args.rawmat1 .. '.png|30px]]')
table.insert(out, '|-')
-- table.insert(out, '| [[' .. args.rawmat1 .. ']]')
-- table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat1qty or 1))
-- table.insert(out, '| style="text-align: right;" | ' .. rawmat1cost)
-- Add Intermediate steps if required
-- Headers
-- table.insert(out, '|-')
-- end
table.insert(out, '! colspan="2" | Intermediate Ingredient')
-- if args.rawmat2 then
table.insert(out, '! Quantity')
table.insert(out, '! Facility')
-- table.insert(out, '| [[File:' .. args.rawmat2 .. '.png|30px]]')
table.insert(out, '|-')
-- table.insert(out, '| [[' .. args.rawmat2 .. ']]')
-- table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat2qty or 1))
-- table.insert(out, '| style="text-align: right;" | ' .. rawmat2cost)
-- Values
-- table.insert(out, '|-')
if args.preparedmat1 then
-- end
table.insert(out, '| [[File:' .. args.preparedmat1 .. '.png|30px]]')
table.insert(out, '| [[' .. args.preparedmat1 .. ']]')
-- if args.rawmat3 then
table.insert(out, '| style="text-align: right;" | ' .. (args.preparedmat1qty or 1))
-- table.insert(out, '| [[File:' .. args.rawmat3 .. '.png|30px]]')
table.insert(out, '| style="text-align: right;" | [[File:' .. args.preparedmat1fac .. '.png|30px]] [[' .. args.preparedmat1fac .. ']]')
-- table.insert(out, '| [[' .. args.rawmat3 .. ']]')
table.insert(out, '|-')
-- table.insert(out, '| style="text-align: right;" | ' .. (args.rawmat3qty or 1))
-- table.insert(out, '| style="text-align: right;" | ' .. rawmat3cost)
end
-- table.insert(out, '|-')
if args.preparedmat2 then
-- end
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
-- -- Add total raw cost
-- table.insert(out, '! colspan="3" | Total Raw cost')
-- Headers
table.insert(out, '! colspan="2" | Output')
-- table.insert(out, '| style="text-align: right;" | ' .. totalCost)
table.insert(out, '! Quantity')
-- table.insert(out, '|-')

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

table.insert(out, '|}')
-- Add debug information to the output as a simple paragraph
local debugMessage = 'Debug Info:<br>' ..
'facility: ' .. (args.facility) .. '<br>' ..
'profession: ' .. (args.profession) .. '<br>' ..
'level: ' .. (args.level) .. '<br>' ..
'exp: ' .. (args.exp) .. '<br>' ..
'rawmat1: ' .. (args.rawmat1) .. '<br>' ..
'rawmat1qty: ' .. (args.rawmat1qty) .. '<br>' ..
'rawmat2: ' .. (args.rawmat2) .. '<br>' ..
'rawmat2qty: ' .. (args.rawmat3qty) .. '<br>' ..
'rawmat3: ' .. (args.rawmat3) .. '<br>' ..
'rawmat3qty: ' .. (args.rawmat3qty) .. '<br>' ..
'preparedmat1: ' .. (args.preparedmat1) .. '<br>' ..
'preparedmat1qty: ' .. (args.preparedmat1qty) .. '<br>' ..
'preparedmat1fac: ' .. (args.preparedmat1fac) .. '<br>' ..
'output1: ' .. (args.output1) .. '<br>' ..
'output1qty: ' .. (args.output1qty) .. '<br>' ..
table.insert(out, debugMessage)


return table.concat(out, '\n')
return table.concat(out, '\n')
Please note that all contributions to Brighter Shores Wiki are considered to be released under the CC BY-NC-SA 3.0 (see Brighter Shores:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template