Module:Infobox Recipe: Difference between revisions
no edit summary
(Switch to mw.html fluent builder; Use {{Currency cell}} to align currency) |
The Gaffer (talk | contribs) No edit summary |
||
(16 intermediate revisions by 2 users not shown) | |||
Line 1:
local currency = require('Module:Currency')
local hc = require('Module:Param Parse').has_content
local yn = require('Module:Yesno')
local editButton = require('Module:Edit button')
local function currency_cell(amount)
Line 9 ⟶ 12:
function p._main(frame)
local args = frame:getParent().args
--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 =
if args.output1 then
output1Value = mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1') or 0
end
--Calculate total value of output material(s)
local output1TotalValue = 0
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
output1TotalValue = 0
end
end
--Check if any of the raw mats provided are intermediate products, if they are, return their own raw materials
if showFullRecipe then
local
if next(
rawMaterials =
intermediateMaterials =
end
else
Line 45 ⟶ 56:
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)
Line 53 ⟶ 66:
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
Line 64 ⟶ 79:
return pageName
end
--Set SMW properties
for _,material in ipairs(argsMaterials) do
mw.smw.set({
["Uses item"] =
["Uses item_and_quantity"] = material.name .. ',' .. tostring(material.quantity)
})
end
if args.facility then
mw.smw.set({
Line 87 ⟶ 96:
end
--Creates a row suitable for the raw materials section of the infobox.
local function createRawMaterialRow(item)
local materialBuyPrice = getBuyPrice(item)
rawMaterialCost = rawMaterialCost + materialBuyPrice
return mw.html.create('tr')
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:wikitext(currency_cell(materialBuyPrice))
Line 106 ⟶ 119:
end
--Creates a row suitable for the intermediate materials section of the infobox.
local function createIntermediateMaterialRow(item)
local facility = getFacility(item.name)
return mw.html.create('tr')
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
:done()
:tag('td')
:
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = '
:wikitext('[[File:' .. facility .. '.png|30px|link=' .. facility .. ']] [[' .. facility .. ']]')
:done()
:done()
end
-- Recipe Table Head
Line 144 ⟶ 163:
:tag('td')
:attr{ colspan = '11' }
:wikitext(hc(args.facility) and ('[[
:done()
:done()
Line 164 ⟶ 184:
:attr{ colspan = '2' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.profession) and ('[[' .. args.profession .. ']]') or editButton("'
:done()
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.level) and (args.level) or editButton("'
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.exp) and (args.exp) or editButton("'
:done()
:done()
:tag('tr')
:tag('th')
Line 193 ⟶ 213:
:done()
for _, material in ipairs(rawMaterials) do
out:node(createRawMaterialRow(material))
end
out
:tag('tr')
Line 208 ⟶ 228:
:done()
-- Headers
if next(intermediateMaterials) ~= nil then
Line 227 ⟶ 247:
:done()
for _, material in ipairs(intermediateMaterials) do
out:node(createIntermediateMaterialRow(material))
end
end
-- Headers
out
:tag('tr')
:tag('th')
Line 249 ⟶ 270:
:done()
:done()
if args.output1 then
out
-- Values
:tag('tr')
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
Line 264 ⟶ 289:
:wikitext(currency_cell(output1TotalValue))
:done()
end
out
:tag('tr')
:tag('th')
Line 276 ⟶ 302:
return out
end
function p._extractRawMaterials(args)
Line 293 ⟶ 321:
end
--This function takes a table containing the raw materials params received from the calling template.
--For each of the raw materials, a query will be executed to see if it has the property 'Uses item'.
--If it does, that means that the 'raw material' is actually an 'intermediate material' as it is itself created.
--The materials required to create this intermediate material will be retrieved and added to the rawMaterials table.
--This allows for a full recipe to be shown where only intermediate ingredients were provided.
function p._getTrueRawMaterials(argsMaterials)
local intermediateMaterials = {}
local function _processMaterial(material, quantity)
if material then
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses item|?Uses item_and_quantity')
if result and result[1] and result[1]["Uses item"] then
-- The item is an intermediate material, add it to intermediateMaterials
table.insert(intermediateMaterials, { ["name"] = material, ["quantity"] = quantity })
-- Add the raw materials used by this intermediate material to rawMaterials
local usesItems = result[1]["Uses item"]
local quantities = result[1]["Uses item and quantity"]
if type(usesItems) == "table" then
for index, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local qty = 1
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local _, q = quantityString:match("(.-),(%d+)")
qty = tonumber(q) or 1
end
_processMaterial(pageName, qty)
end
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local qty = 1
if quantities and type(quantities) == "string" then
local _, q = quantities:match("(.-),(%d+)")
qty = tonumber(q) or 1
end
_processMaterial(pageName, qty)
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity })
end
end
end
-- Iterate over the initial argsMaterials
for _, item in pairs(argsMaterials) do
if item["name"] then
_processMaterial(item["name"], item["quantity"])
end
end
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials }
end
function p._reverseTable(t)
local reversed = {}
for i = #t, 1, -1 do
table.insert(reversed, t[i])
end
return reversed
end
return p
|