Module:Infobox Recipe: Difference between revisions
The Gaffer (talk | contribs) No edit summary |
(Switch to mw.html fluent builder; Use {{Currency cell}} to align currency) |
||
Line 1: | Line 1: | ||
local currency = require('Module:Currency') |
|||
local function currency_cell(amount) |
|||
return currency._cell(amount, { html = 'yes' }) |
|||
end |
|||
local p = {} |
local p = {} |
||
function p._main(frame) |
function p._main(frame) |
||
local args = frame:getParent().args |
local args = frame:getParent().args |
||
local mw = require('mw') |
|||
local currency = require('Module:Currency').parse |
|||
local parse = require('Module:Param Parse') |
local parse = require('Module:Param Parse') |
||
local yn = require('Module:Yesno') |
local yn = require('Module:Yesno') |
||
Line 87: | Line 91: | ||
local materialBuyPrice = getBuyPrice(item) |
local materialBuyPrice = getBuyPrice(item) |
||
rawMaterialCost = rawMaterialCost + materialBuyPrice |
rawMaterialCost = rawMaterialCost + materialBuyPrice |
||
return mw.html.create('tr') |
|||
local row = {} |
|||
:tag('td') |
|||
table.insert(row, '| [[File:' .. item['name'] .. '.png|30px]]') |
|||
:wikitext('[[File:' .. item.name .. '.png|30px]]') |
|||
:done() |
|||
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1)) |
|||
:tag('td') |
|||
table.insert(row, '| style="text-align: right;" | ' .. currency(materialBuyPrice)) |
|||
:wikitext('[[' .. item.name .. ']]') |
|||
table.insert(row, '|-') |
|||
:done() |
|||
return row |
|||
:tag('td') |
|||
:css{ ['text-align'] = 'right' } |
|||
:wikitext(quantity or 1) |
|||
:done() |
|||
:wikitext(currency_cell(materialBuyPrice)) |
|||
:done() |
|||
end |
end |
||
--Creates a row suitable for the intermediate materials section of the infobox. Quantity is optional, if no value is provided it will default to 1 |
--Creates a row suitable for the intermediate materials section of the infobox. Quantity is optional, if no value is provided it will default to 1 |
||
local function createIntermediateMaterialRow(item, quantity) |
local function createIntermediateMaterialRow(item, quantity) |
||
local row = {} |
|||
local facility = getFacility(item) |
local facility = getFacility(item) |
||
return mw.html.create('tr') |
|||
table.insert(row, '| [[File:' .. item .. '.png|30px]]') |
|||
:tag('td') |
|||
table.insert(row, '| [[' .. item .. ']]') |
|||
:wikitext('[[File:' .. item .. '.png|30px]]') |
|||
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1)) |
|||
:done() |
|||
table.insert(row, '| style="text-align: right;" | [[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]') |
|||
:tag('td') |
|||
:wikitext('[[' .. item .. ']]') |
|||
return row |
|||
:done() |
|||
:tag('td') |
|||
:css{ ['text-align'] = 'right' } |
|||
:wikitext(quantity or 1) |
|||
:done() |
|||
:tag('td') |
|||
:attr{ colspan = '10' } |
|||
:css{ ['text-align'] = 'right' } |
|||
:wikitext('[[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]') |
|||
:done() |
|||
:done() |
|||
end |
end |
||
-- Recipe Table Head |
-- Recipe Table Head |
||
local out = |
local out = mw.html.create('table') |
||
:addClass('wikitable') |
|||
:tag('tr') |
|||
table.insert(out, '! colspan="4" | Requirements') |
|||
:tag('th') |
|||
table.insert(out, '|-') |
|||
:attr{ colspan = '13' } |
|||
:wikitext('Requirements') |
|||
table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown')) |
|||
:done() |
|||
table.insert(out, '|-') |
|||
:done() |
|||
table.insert(out, '! colspan="2" | Profession') |
|||
:tag('tr') |
|||
table.insert(out, '! Level') |
|||
:tag('th') |
|||
table.insert(out, '! XP') |
|||
:attr{ colspan = '2' } |
|||
table.insert(out, '|-') |
|||
:wikitext('Facility') |
|||
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown')) |
|||
:done() |
|||
table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown')) |
|||
:tag('td') |
|||
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown')) |
|||
:attr{ colspan = '11' } |
|||
table.insert(out, '|-') |
|||
:wikitext(args.facility and '[[' .. args.facility .. ']]' or 'Unknown') |
|||
:done() |
|||
:done() |
|||
:tag('tr') |
|||
:tag('th') |
|||
:attr{ colspan = '2' } |
|||
:wikitext('Profession') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Level') |
|||
:done() |
|||
:tag('th') |
|||
:attr{ colspan = '10' } |
|||
:wikitext('XP') |
|||
:done() |
|||
:done() |
|||
:tag('tr') |
|||
:tag('td') |
|||
:attr{ colspan = '2' } |
|||
:css{ ['text-align'] = 'center' } |
|||
:wikitext(args.profession and '[[' .. args.profession .. ']]' or 'Unknown') |
|||
:done() |
|||
:tag('td') |
|||
:css{ ['text-align'] = 'center' } |
|||
:wikitext(args.level or 'Unknown') |
|||
:done() |
|||
:tag('td') |
|||
:attr{ colspan = '10' } |
|||
:css{ ['text-align'] = 'center' } |
|||
:wikitext(args.exp or 'Unknown') |
|||
:done() |
|||
:done() |
|||
-- Add raw materials |
-- Add raw materials |
||
-- Headers |
|||
:tag('tr') |
|||
table.insert(out, '! colspan="2" | Raw Ingredient') |
|||
:tag('th') |
|||
table.insert(out, '! Quantity') |
|||
:attr{ colspan = '2' } |
|||
table.insert(out, '! Cost') |
|||
:wikitext('Raw Ingredient') |
|||
table.insert(out, '|-') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Quantity') |
|||
:done() |
|||
:tag('th') |
|||
:attr{ colspan = '10' } |
|||
:wikitext('Cost') |
|||
:done() |
|||
:done() |
|||
-- Values |
|||
for _, material in ipairs(rawMaterials) do |
for _, material in ipairs(rawMaterials) do |
||
out:node(createRawMaterialRow(material)) |
|||
for _, value in ipairs(row) do |
|||
table.insert(out, value) |
|||
end |
|||
end |
end |
||
-- Add total raw cost |
-- Add total raw cost |
||
out |
|||
table.insert(out, '! colspan="3" | Total Raw cost') |
|||
:tag('tr') |
|||
table.insert(out, '| style="text-align: right;" | ' .. currency(rawMaterialCost)) |
|||
:tag('th') |
|||
table.insert(out, '|-') |
|||
:attr{ colspan = '3' } |
|||
:wikitext('Total Raw cost') |
|||
:done() |
|||
:wikitext(currency_cell(rawMaterialCost)) |
|||
:done() |
|||
-- Add Intermediate steps if required |
-- Add Intermediate steps if required |
||
-- Headers |
-- Headers |
||
if next(intermediateMaterials) ~= nil then |
if next(intermediateMaterials) ~= nil then |
||
-- Headers |
-- Headers |
||
out |
|||
table.insert(out, '! colspan="2" | Intermediate Ingredient') |
|||
:tag('tr') |
|||
table.insert(out, '! Quantity') |
|||
:tag('th') |
|||
table.insert(out, '! Facility') |
|||
:attr{ colspan = '2' } |
|||
table.insert(out, '|-') |
|||
:wikitext('Intermediate Ingredient') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Quantity') |
|||
:done() |
|||
:tag('th') |
|||
:attr{ colspan = '10' } |
|||
:wikitext('Faciltity') |
|||
:done() |
|||
:done() |
|||
-- Values |
|||
for _, material in ipairs(intermediateMaterials) do |
for _, material in ipairs(intermediateMaterials) do |
||
out:node(createIntermediateMaterialRow(material.name)) |
|||
for _, value in ipairs(row) do |
|||
table.insert(out, value) |
|||
end |
|||
end |
end |
||
end |
end |
||
-- Add output data |
-- Add output data |
||
out |
|||
-- Headers |
|||
-- Headers |
|||
table.insert(out, '! colspan="2" | Output') |
|||
:tag('tr') |
|||
table.insert(out, '! Quantity') |
|||
:tag('th') |
|||
table.insert(out, '! Value') |
|||
:attr{ colspan = '2' } |
|||
table.insert(out, '|-') |
|||
:wikitext('Output') |
|||
:done() |
|||
:tag('th') |
|||
:wikitext('Quantity') |
|||
:done() |
|||
:tag('th') |
|||
:attr{ colspan = '10' } |
|||
:wikitext('Value') |
|||
:done() |
|||
:done() |
|||
-- Values |
|||
:tag('tr') |
|||
table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]') |
|||
:tag('td') |
|||
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown')) |
|||
:wikitext('[[File:' .. args.output1 .. '.png|30px]]') |
|||
table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1)) |
|||
:done() |
|||
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue)) |
|||
:tag('td') |
|||
table.insert(out, '|-') |
|||
:wikitext(args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown') |
|||
:done() |
|||
:tag('td') |
|||
:css{ ['text-align'] = 'right' } |
|||
:wikitext(args.output1qty or 1) |
|||
:done() |
|||
:wikitext(currency_cell(output1TotalValue)) |
|||
:done() |
|||
-- Add profit data |
|||
:tag('tr') |
|||
table.insert(out, '! colspan="3" | Profit') |
|||
:tag('th') |
|||
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue - rawMaterialCost)) |
|||
:attr{ colspan = '3' } |
|||
table.insert(out, '|}') |
|||
:wikitext('Profit') |
|||
:done() |
|||
:wikitext(currency_cell(output1TotalValue - rawMaterialCost)) |
|||
:done() |
|||
return |
return out |
||
end |
end |
||
Revision as of 20:56, 20 November 2024
The Infobox Recipe template is used to show the details of a recipe step in item articles, such as a potion mixture or adding a cooking ingredient. It can also be used to display the experience gained for item creation.
Parameters
Parameter | Format | Usage | Mandatory? |
---|---|---|---|
facility
|
Link to facility | The name of the facility that is required to create the item; e.g. facility = Standard Potion Station will display
|
Yes |
profession
|
profession name | The profession in which experience is gained, or which has a level requirement, for creating the item | Yes |
level
|
Integer | The level required in the profession to create the item. | Yes |
exp
|
number | The amount of experience gained for performing the action. | Yes |
rawmat1
|
Exact name of the item | The primary item used for performing the craft. e.g. Brown Kelp for Brown Kelp
|
Yes |
rawmat1qty
|
Integer | The amount of the material used in the process.
This parameter can be removed if it would be 1 |
No |
rawmat2
|
Exact name of the item | The secondary item used for performing the craft. e.g. Brown Kelp for Brown Kelp
|
No |
rawmat2qty
|
Integer | The amount of the material used in the process.
This parameter can be removed if it would be 1 |
No |
rawmat3
|
Exact name of the item | The tertiary item used for performing the craft. e.g. Brown Kelp for Brown Kelp
|
No |
rawmat3qty
|
Integer | The amount of the material used in the process.
This parameter can be removed if it would be 1 |
No |
rawmat4
|
Exact name of the item | The fourth item used for performing the craft. e.g. Brown Kelp for Brown Kelp
|
No |
rawmat4qty
|
Integer | The amount of the material used in the process.
This parameter can be removed if it would be 1 |
No |
output1
|
Exact name of the item | The item created by performing the craft. e.g. Brown Kelp for Brown Kelp
|
Yes |
output1qty
|
Integer | The amount of the output created in the process.
This parameter can be removed if it would be 1 |
No |
showFull
|
boolean | Flag to display all steps of a recipe or only the step specified in the params | No |
Examples
Show Single Step Recipe
This is an example that uses values from the 10% Potion Healing recipe. Notice that this only shows information of the steps for which the information has been provided.
The showFull
parameter can be omitted entirely if this is the desired outcome as the default value is false
{{Infobox Recipe |facility = Standard Potion Station |profession = Alchemist |level = 0 |exp = 63 |rawmat1 = 10% Preparation Healing |rawmat2 = Bottle |output1 = 10% Potion Healing |output1qty = 1 }}
Requirements | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Facility | Standard Potion Station | |||||||||||
Profession | Level | XP | ||||||||||
Alchemist | 0 | 63 | ||||||||||
Raw Ingredient | Quantity | Cost | ||||||||||
10% Preparation Healing | 1 | 0 | ||||||||||
Bottle | 1 | 20 | ||||||||||
Total Raw cost | 20 | |||||||||||
Output | Quantity | Value | ||||||||||
10% Potion Healing | 1 | 450 | ||||||||||
Profit | 430 |
Show Full Recipe
This is an example that uses values from the 10% Potion Healing recipe. Notice that the parameters that have been passed to this example do not include 'Brown Kelp'
This is displayed because the showFull
parameter has been set to true
. This causes the Infobox Module to query smw data for each of the provided
raw materials to check if they have raw materials of their own. If they do, they are brought in to the recipe.
{{Infobox Recipe |facility = Standard Potion Station |profession = Alchemist |level = 0 |exp = 63 |rawmat1 = 10% Preparation Healing |rawmat2 = Bottle |output1 = 10% Potion Healing |output1qty = 1 |showFull = true }}
Requirements | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Facility | Standard Potion Station | |||||||||||
Profession | Level | XP | ||||||||||
Alchemist | 0 | 63 | ||||||||||
Raw Ingredient | Quantity | Cost | ||||||||||
Brown Kelp | 1 | 500 | ||||||||||
Total Raw cost | 500 | |||||||||||
Intermediate Ingredient | Quantity | Faciltity | ||||||||||
10% Preparation Healing | 1 | Reagent Preparation Station | ||||||||||
Bottle | 1 | File:.png [[]] | ||||||||||
Output | Quantity | Value | ||||||||||
10% Potion Healing | 1 | 450 | ||||||||||
Profit | -50 |
Blank infobox
{{Infobox Recipe |facility = |profession = |level = |exp = |rawmat1 = |rawmat1qty = |rawmat2 = |rawmat2qty = |rawmat3 = |rawmat3qty = |output1 = |output1qty =
local currency = require('Module:Currency')
local function currency_cell(amount)
return currency._cell(amount, { html = 'yes' })
end
local p = {}
function p._main(frame)
local args = frame:getParent().args
local parse = require('Module:Param Parse')
local yn = require('Module:Yesno')
--If set to true, will check if any of the items passed as a rawmaterialparam are actually intermediate materials. Will then change the display order of the infobox.
local showFullRecipe = yn(args.showFull or 'no', false)
--Get each of the rawmatX parameters from params and store their values in a new table
local argsMaterials = p._extractRawMaterials(args)
--empty tables to hold materials
local rawMaterials = {}
local intermediateMaterials = {}
--counter for total price of raw materials
local rawMaterialCost = 0
--get the value for the output product
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1TotalValue = 0
mw.logObject(output1Value[1]['Value'])
if output1Value[1]['Value'] ~= nil then
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
else
output1TotalValue = 0
end
--Check if any of the raw mats provided are intermediate products, if they are, return their own raw materials
if showFullRecipe then
local trueRawMaterials = p._getTrueRawMaterials(argsMaterials)
if next(trueRawMaterials) ~= nil then
rawMaterials = trueRawMaterials
intermediateMaterials = argsMaterials
end
else
rawMaterials = argsMaterials
end
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0
local function getBuyPrice(material)
local queryResult = mw.smw.ask('[[:+]][[Sold item::' .. material['name'] .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or 0
if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
return tonumber(queryResult[1]['Shop buy price']) or 0
end
return 0
end
--If one of the raw materials provided as a param has been identified as an intermediate material then get the facility it is created at to display in the intermediate Ingredient row
local function getFacility(material)
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses facility|limit=1') or 'unknown'
local pageName = ''
if result and result[1] and result[1]["Uses facility"] then
local usesFac = result[1]["Uses facility"]
pageName = usesFac:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
end
return pageName
end
--Set SMW properties
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
if args.facility then
mw.smw.set({
["Uses facility"] = args.facility
})
end
--Creates a row suitable for the raw materials section of the infobox. Quantity is optional, if no value is provided it will default to 1
local function createRawMaterialRow(item, quantity)
local materialBuyPrice = getBuyPrice(item)
rawMaterialCost = rawMaterialCost + materialBuyPrice
return mw.html.create('tr')
:tag('td')
:wikitext('[[File:' .. item.name .. '.png|30px]]')
:done()
:tag('td')
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(quantity or 1)
:done()
:wikitext(currency_cell(materialBuyPrice))
:done()
end
--Creates a row suitable for the intermediate materials section of the infobox. Quantity is optional, if no value is provided it will default to 1
local function createIntermediateMaterialRow(item, quantity)
local facility = getFacility(item)
return mw.html.create('tr')
:tag('td')
:wikitext('[[File:' .. item .. '.png|30px]]')
:done()
:tag('td')
:wikitext('[[' .. item .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(quantity or 1)
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'right' }
:wikitext('[[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]')
:done()
:done()
end
-- Recipe Table Head
local out = mw.html.create('table')
:addClass('wikitable')
:tag('tr')
:tag('th')
:attr{ colspan = '13' }
:wikitext('Requirements')
:done()
:done()
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Facility')
:done()
:tag('td')
:attr{ colspan = '11' }
:wikitext(args.facility and '[[' .. args.facility .. ']]' or 'Unknown')
:done()
:done()
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Profession')
:done()
:tag('th')
:wikitext('Level')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('XP')
:done()
:done()
:tag('tr')
:tag('td')
:attr{ colspan = '2' }
:css{ ['text-align'] = 'center' }
:wikitext(args.profession and '[[' .. args.profession .. ']]' or 'Unknown')
:done()
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(args.level or 'Unknown')
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'center' }
:wikitext(args.exp or 'Unknown')
:done()
:done()
-- Add raw materials
-- Headers
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Raw Ingredient')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Cost')
:done()
:done()
-- Values
for _, material in ipairs(rawMaterials) do
out:node(createRawMaterialRow(material))
end
-- Add total raw cost
out
:tag('tr')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Total Raw cost')
:done()
:wikitext(currency_cell(rawMaterialCost))
:done()
-- Add Intermediate steps if required
-- Headers
if next(intermediateMaterials) ~= nil then
-- Headers
out
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Intermediate Ingredient')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Faciltity')
:done()
:done()
-- Values
for _, material in ipairs(intermediateMaterials) do
out:node(createIntermediateMaterialRow(material.name))
end
end
-- Add output data
out
-- Headers
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Output')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Value')
:done()
:done()
-- Values
:tag('tr')
:tag('td')
:wikitext('[[File:' .. args.output1 .. '.png|30px]]')
:done()
:tag('td')
:wikitext(args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(args.output1qty or 1)
:done()
:wikitext(currency_cell(output1TotalValue))
:done()
-- Add profit data
:tag('tr')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Profit')
:done()
:wikitext(currency_cell(output1TotalValue - rawMaterialCost))
:done()
return out
end
function p._extractRawMaterials(args)
local rawMaterials = {}
for i = 1, 9 do
local materialKey = "rawmat" .. i
local quantityKey = "rawmat" .. i .. "qty"
local materialValue = args[materialKey]
if materialValue and materialValue ~= "" then
local quantityValue = tonumber(args[quantityKey]) or 1
table.insert(rawMaterials, { name = materialValue, quantity = quantityValue })
end
end
return rawMaterials
end
function p._getTrueRawMaterials(argsMaterials)
local queryResult = {}
for _, item in pairs(argsMaterials) do
if item["name"] then
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item')
if result and result[1] and result[1]["Uses item"] then
local usesItems = result[1]["Uses item"]
-- If usesItems is a table (multiple items), iterate and add each to queryResult
if type(usesItems) == "table" then
for _, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
end
else
-- If usesItems is a single item, add it directly
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
end
end
end
end
return queryResult
end
return p