Module:Infobox Recipe: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
The Gaffer (talk | contribs) No edit summary |
The Gaffer (talk | contribs) 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) |
|||
⚫ | |||
if |
if material then |
||
local result = mw.smw.ask('[[:+]][[' .. |
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"] = |
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 |
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 _, |
local _, q = quantityString:match("(.-),(%d+)") |
||
qty = tonumber(q) or 1 |
|||
end |
end |
||
_processMaterial(pageName, qty) |
|||
end |
end |
||
else |
else |
||
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
||
local |
local qty = 1 |
||
if quantities and type(quantities) == "string" then |
if quantities and type(quantities) == "string" then |
||
local _, |
local _, q = quantities:match("(.-),(%d+)") |
||
qty = tonumber(q) or 1 |
|||
end |
end |
||
_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"] = |
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity }) |
||
end |
end |
||
end |
end |
||
end |
end |
||
-- Iterate over the initial argsMaterials |
|||
⚫ | |||
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 |
||