Module:Infobox Recipe: Difference between revisions

1,503 bytes added ,  Yesterday at 13:58
no edit summary
No edit summary
No edit summary
Line 37:
if showFullRecipe then
local Materials = p._getTrueRawMaterials(argsMaterials)
if next(Materials) ~= nil then
rawMaterials = Materials.rawMaterials
Line 45 ⟶ 44:
rawMaterials = argsMaterials
end
mw.logObject(rawMaterials)
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0
Line 67 ⟶ 68:
 
--Set SMW properties
for _, usedItemmaterial in ipairs(usesItemsargsMaterials) do
if args.rawmat1 then
mw.smw.set({
["Uses item"] = argsmaterial.rawmat1name,
["Uses item_and_quantity"] = material.name .. ',' .. tostring(material.quantity)
})
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
Line 88 ⟶ 80:
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
Line 103 ⟶ 95:
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:wikitext(currency_cell(materialBuyPrice))
Line 109 ⟶ 101:
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')
Line 123 ⟶ 115:
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:tag('td')
Line 200 ⟶ 192:
-- Values
for _, material in ipairs(rawMaterials) do
mw.logObject(material)
out:node(createRawMaterialRow(material))
end
Line 300 ⟶ 293:
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.
-- Add the rawThe materials usedrequired byto 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 rawMaterials = {}
local intermediateMaterials = {}
 
for _, item in pairs(argsMaterials) do
if item["name"] then
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item|?Uses item_and_quantity')
--mw.logObject(result)
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"] = item["name"], ["quantity"] = item["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
if result and result[1] and result[1]["Uses item"] then
for index, usedItem in ipairs(usesItems) do
-- The item is an intermediate material, add it to intermediateMaterials
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(intermediateMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
local quantity = 1
if quantities and type(quantities) == "table" then
-- Add the raw materials used by this intermediate material to rawMaterials
local quantityString = quantities[index]
local usesItems = result[1]["Uses item"]
local _, qty = quantityString:match("(.-),(%d+)")
 
quantity = tonumber(qty) or 1
if type(usesItems) == "table" then
mw.logObject(tonumber(qty))
for _, usedItem in ipairs(usesItems) do
end
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1quantity })
end
end
else
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantity = 1
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1 })
if quantities and type(quantities) == "string" then
end
local _, qty = quantities:match("(.-),(%d+)")
else
quantity = tonumber(qty) or 1
-- The item is a raw material, add it to rawMaterials
end
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1quantity })
end
end
end
else
end
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
mw.logObject(item)
end
end
end
 
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials }
end
 
927

edits