Module:Infobox Recipe: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 38:
if next(Materials) ~= nil then
rawMaterials = Materials.rawMaterials
intermediateMaterials = p._reverseTable(Materials.intermediateMaterials)
end
else
Line 223:
-- Values
for _, material in ipairs(intermediateMaterials) do
mw.logObject(material)
out:node(createIntermediateMaterialRow(material))
end
Line 298 ⟶ 299:
local intermediateMaterials = {}
 
local function _processMaterial(material, quantity)
for _, item in pairs(argsMaterials) do
if item["name"]material then
local result = mw.smw.ask('[[:+]][[' .. item["name"]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"] = item["name"]material, ["quantity"] = item["quantity"] })
-- Add the raw materials used by this intermediate material to rawMaterials
Line 312 ⟶ 313:
for index, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantityqty = 1
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local _, qtyq = quantityString:match("(.-),(%d+)")
quantityqty = tonumber(qtyq) or 1
end
table.insert_processMaterial(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity }qty)
end
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantityqty = 1
if quantities and type(quantities) == "string" then
local _, qtyq = quantities:match("(.-),(%d+)")
quantityqty = tonumber(qtyq) or 1
end
table.insert_processMaterial(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity }qty)
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = item["name"]material, ["quantity"] = item["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
 
956

edits