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 37: | Line 37: | ||
if showFullRecipe then |
if showFullRecipe then |
||
local Materials = p._getTrueRawMaterials(argsMaterials) |
local Materials = p._getTrueRawMaterials(argsMaterials) |
||
if next(Materials) ~= nil then |
if next(Materials) ~= nil then |
||
rawMaterials = Materials.rawMaterials |
rawMaterials = Materials.rawMaterials |
||
Line 45: | Line 44: | ||
rawMaterials = argsMaterials |
rawMaterials = argsMaterials |
||
end |
end |
||
mw.logObject(rawMaterials) |
|||
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0 |
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0 |
||
Line 67: | Line 68: | ||
--Set SMW properties |
--Set SMW properties |
||
⚫ | |||
if args.rawmat1 then |
|||
mw.smw.set({ |
mw.smw.set({ |
||
["Uses item"] = |
["Uses item"] = material.name, |
||
["Uses item_and_quantity"] = material.name .. ',' .. tostring(material.quantity) |
|||
}) |
|||
⚫ | |||
if args.rawmat2 then |
|||
mw.smw.set({ |
|||
["Uses item"] = args.rawmat2, |
|||
}) |
|||
end |
|||
if args.rawmat3 then |
|||
mw.smw.set({ |
|||
["Uses item"] = args.rawmat3, |
|||
}) |
}) |
||
end |
end |
||
Line 88: | Line 80: | ||
end |
end |
||
--Creates a row suitable for the raw materials section of the infobox. |
--Creates a row suitable for the raw materials section of the infobox. |
||
local function createRawMaterialRow(item |
local function createRawMaterialRow(item) |
||
local materialBuyPrice = getBuyPrice(item) |
local materialBuyPrice = getBuyPrice(item) |
||
rawMaterialCost = rawMaterialCost + materialBuyPrice |
rawMaterialCost = rawMaterialCost + materialBuyPrice |
||
Line 103: | Line 95: | ||
:tag('td') |
:tag('td') |
||
:css{ ['text-align'] = 'right' } |
:css{ ['text-align'] = 'right' } |
||
:wikitext(quantity or 1) |
:wikitext(item.quantity or 1) |
||
:done() |
:done() |
||
:wikitext(currency_cell(materialBuyPrice)) |
:wikitext(currency_cell(materialBuyPrice)) |
||
Line 109: | Line 101: | ||
end |
end |
||
--Creates a row suitable for the intermediate materials section of the infobox. |
--Creates a row suitable for the intermediate materials section of the infobox. |
||
local function createIntermediateMaterialRow(item |
local function createIntermediateMaterialRow(item) |
||
local facility = getFacility(item) |
local facility = getFacility(item) |
||
return mw.html.create('tr') |
return mw.html.create('tr') |
||
Line 123: | Line 115: | ||
:tag('td') |
:tag('td') |
||
:css{ ['text-align'] = 'right' } |
:css{ ['text-align'] = 'right' } |
||
:wikitext(quantity or 1) |
:wikitext(item.quantity or 1) |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
Line 200: | Line 192: | ||
-- Values |
-- Values |
||
for _, material in ipairs(rawMaterials) do |
for _, material in ipairs(rawMaterials) do |
||
mw.logObject(material) |
|||
out:node(createRawMaterialRow(material)) |
out:node(createRawMaterialRow(material)) |
||
end |
end |
||
Line 300: | Line 293: | ||
end |
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. |
|||
⚫ | |||
--This allows for a full recipe to be shown where only intermediate ingredients were provided. |
|||
function p._getTrueRawMaterials(argsMaterials) |
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) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- Add the raw materials used by this intermediate material to rawMaterials |
|||
⚫ | |||
local quantities = result[1]["Uses item and quantity"] |
|||
⚫ | |||
⚫ | |||
for index, usedItem in ipairs(usesItems) do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
local quantity = 1 |
|||
if quantities and type(quantities) == "table" then |
|||
⚫ | |||
local quantityString = quantities[index] |
|||
⚫ | |||
local _, qty = quantityString:match("(.-),(%d+)") |
|||
quantity = tonumber(qty) or 1 |
|||
⚫ | |||
mw.logObject(tonumber(qty)) |
|||
⚫ | |||
end |
|||
⚫ | |||
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity }) |
|||
end |
|||
end |
|||
else |
|||
else |
|||
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
|||
local quantity = 1 |
|||
⚫ | |||
if quantities and type(quantities) == "string" then |
|||
end |
|||
local _, qty = quantities:match("(.-),(%d+)") |
|||
else |
|||
quantity = tonumber(qty) or 1 |
|||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
end |
|||
end |
|||
end |
|||
else |
|||
end |
|||
⚫ | |||
⚫ | |||
mw.logObject(item) |
|||
end |
|||
end |
|||
⚫ | |||
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials } |
|||
end |
end |
||