Module:Infobox Recipe: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(Link all images to the item/facility; Get rid of border between item image and name)
No edit summary
Line 17: Line 17:
--Get each of the rawmatX parameters from params and store their values in a new table
--Get each of the rawmatX parameters from params and store their values in a new table
local argsMaterials = p._extractRawMaterials(args)
local argsMaterials = p._extractRawMaterials(args)
--mw.logObject(argsMaterials)
--empty tables to hold materials
--empty tables to hold materials
local rawMaterials = {}
local rawMaterials = {}
Line 27: Line 28:
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1TotalValue = 0
local output1TotalValue = 0
mw.logObject(output1Value[1]['Value'])
if output1Value[1]['Value'] ~= nil then
if output1Value[1]['Value'] ~= nil then
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
Line 36: Line 36:
--Check if any of the raw mats provided are intermediate products, if they are, return their own raw materials
--Check if any of the raw mats provided are intermediate products, if they are, return their own raw materials
if showFullRecipe then
if showFullRecipe then
local trueRawMaterials = p._getTrueRawMaterials(argsMaterials)
local Materials = p._getTrueRawMaterials(argsMaterials)
if next(trueRawMaterials) ~= nil then
if next(Materials) ~= nil then
rawMaterials = trueRawMaterials
rawMaterials = Materials.rawMaterials
intermediateMaterials = argsMaterials
intermediateMaterials = Materials.intermediateMaterials
end
end
else
else
rawMaterials = argsMaterials
rawMaterials = argsMaterials
end
end

--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
local function getBuyPrice(material)
local function getBuyPrice(material)
Line 300: Line 301:


function p._getTrueRawMaterials(argsMaterials)
function p._getTrueRawMaterials(argsMaterials)
local queryResult = {}
local rawMaterials = {}
local intermediateMaterials = {}

for _, item in pairs(argsMaterials) do
for _, item in pairs(argsMaterials) do
if item["name"] then
if item["name"] then
Line 306: Line 309:


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
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 usesItems = result[1]["Uses item"]


-- If usesItems is a table (multiple items), iterate and add each to queryResult
if type(usesItems) == "table" then
if type(usesItems) == "table" then
for _, usedItem in ipairs(usesItems) do
for _, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1 })
end
end
else
else
-- If usesItems is a single item, add it directly
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1 })
end
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
end
end
end
end
end
end

return queryResult
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials }
end
end