Module:Infobox Recipe: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 38: Line 38:
if next(Materials) ~= nil then
if next(Materials) ~= nil then
rawMaterials = Materials.rawMaterials
rawMaterials = Materials.rawMaterials
intermediateMaterials = Materials.intermediateMaterials
intermediateMaterials = p._reverseTable(Materials.intermediateMaterials)
end
end
else
else
Line 223: Line 223:
-- Values
-- Values
for _, material in ipairs(intermediateMaterials) do
for _, material in ipairs(intermediateMaterials) do
mw.logObject(material)
out:node(createIntermediateMaterialRow(material))
out:node(createIntermediateMaterialRow(material))
end
end
Line 298: Line 299:
local intermediateMaterials = {}
local intermediateMaterials = {}


local function _processMaterial(material, quantity)
for _, item in pairs(argsMaterials) do
if item["name"] then
if material then
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item|?Uses item_and_quantity')
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses item|?Uses item_and_quantity')
if result and result[1] and result[1]["Uses item"] then
if result and result[1] and result[1]["Uses item"] then
-- The item is an intermediate material, add it to intermediateMaterials
-- The item is an intermediate material, add it to intermediateMaterials
table.insert(intermediateMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
table.insert(intermediateMaterials, { ["name"] = material, ["quantity"] = quantity })
-- Add the raw materials used by this intermediate material to rawMaterials
-- Add the raw materials used by this intermediate material to rawMaterials
Line 312: Line 313:
for index, usedItem in ipairs(usesItems) do
for index, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantity = 1
local qty = 1
if quantities and type(quantities) == "table" then
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local quantityString = quantities[index]
local _, qty = quantityString:match("(.-),(%d+)")
local _, q = quantityString:match("(.-),(%d+)")
quantity = tonumber(qty) or 1
qty = tonumber(q) or 1
end
end
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity })
_processMaterial(pageName, qty)
end
end
else
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantity = 1
local qty = 1
if quantities and type(quantities) == "string" then
if quantities and type(quantities) == "string" then
local _, qty = quantities:match("(.-),(%d+)")
local _, q = quantities:match("(.-),(%d+)")
quantity = tonumber(qty) or 1
qty = tonumber(q) or 1
end
end
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity })
_processMaterial(pageName, qty)
end
end
else
else
-- The item is a raw material, add it to rawMaterials
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity })
end
end
end
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 }
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
end