Module:Infobox Recipe: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
m (Spaces to tabs)
Line 2: Line 2:


function p._main(frame)
function p._main(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local mw = require('mw')
local mw = require('mw')
local currency = require('Module:Currency').parse
local currency = require('Module:Currency').parse
local parse = require('Module:Param Parse')
local parse = require('Module:Param Parse')
local yn = require('Module:Yesno')
local yn = require('Module:Yesno')

--If set to true, will check if any of the items passed as a rawmaterialparam are actually intermediate materials. Will then change the display order of the infobox.
--If set to true, will check if any of the items passed as a rawmaterialparam are actually intermediate materials. Will then change the display order of the infobox.
local showFullRecipe = yn(args.showFull or 'no', false)
local showFullRecipe = yn(args.showFull or 'no', false)

--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)
--empty tables to hold materials
--empty tables to hold materials
local rawMaterials = {}
local rawMaterials = {}
local intermediateMaterials = {}
local intermediateMaterials = {}

--counter for total price of raw materials
--counter for total price of raw materials
local rawMaterialCost = 0
local rawMaterialCost = 0

--get the value for the output product
--get the value for the output product
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')
Line 29: Line 29:
output1TotalValue = 0
output1TotalValue = 0
end
end

--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
Line 45: Line 45:
local queryResult = mw.smw.ask('[[:+]][[Sold item::' .. material['name'] .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or 0
local queryResult = mw.smw.ask('[[:+]][[Sold item::' .. material['name'] .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or 0
if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
if type(queryResult) == "table" and queryResult[1]['Shop buy price'] then
return tonumber(queryResult[1]['Shop buy price']) or 0
return tonumber(queryResult[1]['Shop buy price']) or 0
end
end
return 0
return 0
end
end


Line 55: Line 55:
local pageName = ''
local pageName = ''
if result and result[1] and result[1]["Uses facility"] then
if result and result[1] and result[1]["Uses facility"] then
local usesFac = result[1]["Uses facility"]
local usesFac = result[1]["Uses facility"]
pageName = usesFac:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
pageName = usesFac:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
end
end
return pageName
return pageName
end
end


--Set SMW properties
--Set SMW properties
if args.rawmat1 then
if args.rawmat1 then
mw.smw.set({
mw.smw.set({
["Uses item"] = args.rawmat1,
["Uses item"] = args.rawmat1,
})
})
end
end
if args.rawmat2 then
if args.rawmat2 then
mw.smw.set({
mw.smw.set({
["Uses item"] = args.rawmat2,
["Uses item"] = args.rawmat2,
})
})
end
end
if args.rawmat3 then
if args.rawmat3 then
mw.smw.set({
mw.smw.set({
["Uses item"] = args.rawmat3,
["Uses item"] = args.rawmat3,
})
})
end
end
if args.facility then
if args.facility then
mw.smw.set({
mw.smw.set({
["Uses facility"] = args.facility
["Uses facility"] = args.facility
Line 89: Line 89:
local row = {}
local row = {}
table.insert(row, '| [[File:' .. item['name'] .. '.png|30px]]')
table.insert(row, '| [[File:' .. item['name'] .. '.png|30px]]')
table.insert(row, '| [[' .. item['name'] .. ']]')
table.insert(row, '| [[' .. item['name'] .. ']]')
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
table.insert(row, '| style="text-align: right;" | ' .. currency(materialBuyPrice))
table.insert(row, '| style="text-align: right;" | ' .. currency(materialBuyPrice))
table.insert(row, '|-')
table.insert(row, '|-')
return row
return row
end
end

--Creates a row suitable for the intermediate materials section of the infobox. Quantity is optional, if no value is provided it will default to 1
--Creates a row suitable for the intermediate materials section of the infobox. Quantity is optional, if no value is provided it will default to 1
local function createIntermediateMaterialRow(item, quantity)
local function createIntermediateMaterialRow(item, quantity)
Line 101: Line 101:
local facility = getFacility(item)
local facility = getFacility(item)
table.insert(row, '| [[File:' .. item .. '.png|30px]]')
table.insert(row, '| [[File:' .. item .. '.png|30px]]')
table.insert(row, '| [[' .. item .. ']]')
table.insert(row, '| [[' .. item .. ']]')
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
table.insert(row, '| style="text-align: right;" | [[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]')
table.insert(row, '| style="text-align: right;" | [[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]')
table.insert(row, '|-')
table.insert(row, '|-')
return row
return row
end
end

-- Recipe Table Head
-- Recipe Table Head
local out = {}
local out = {}
table.insert(out, '{| class="wikitable"')
table.insert(out, '{| class="wikitable"')
table.insert(out, '! colspan="4" | Requirements')
table.insert(out, '! colspan="4" | Requirements')
table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '! colspan="2" | Facility')
table.insert(out, '! colspan="2" | Facility')
table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '! colspan="2" | Profession')
table.insert(out, '! colspan="2" | Profession')
table.insert(out, '! Level')
table.insert(out, '! Level')
table.insert(out, '! XP')
table.insert(out, '! XP')
table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
table.insert(out, '|-')
table.insert(out, '|-')

-- Add raw materials
-- Add raw materials
-- Headers
-- Headers
table.insert(out, '! colspan="2" | Raw Ingredient')
table.insert(out, '! colspan="2" | Raw Ingredient')
table.insert(out, '! Quantity')
table.insert(out, '! Quantity')
table.insert(out, '! Cost')
table.insert(out, '! Cost')
table.insert(out, '|-')
table.insert(out, '|-')


-- Values
-- Values
for _, material in ipairs(rawMaterials) do
for _, material in ipairs(rawMaterials) do
local row = createRawMaterialRow(material)
local row = createRawMaterialRow(material)
for _, value in ipairs(row) do
for _, value in ipairs(row) do
table.insert(out, value)
table.insert(out, value)
end
end
end
end


-- Add total raw cost
-- Add total raw cost
table.insert(out, '! colspan="3" | Total Raw cost')
table.insert(out, '! colspan="3" | Total Raw cost')
table.insert(out, '| style="text-align: right;" | ' .. currency(rawMaterialCost))
table.insert(out, '| style="text-align: right;" | ' .. currency(rawMaterialCost))
table.insert(out, '|-')
table.insert(out, '|-')

-- Add Intermediate steps if required
-- Add Intermediate steps if required
-- Headers
-- Headers
if next(intermediateMaterials) ~= nil then
if next(intermediateMaterials) ~= nil then
-- Headers
-- Headers
table.insert(out, '! colspan="2" | Intermediate Ingredient')
table.insert(out, '! colspan="2" | Intermediate Ingredient')
table.insert(out, '! Quantity')
table.insert(out, '! Quantity')
table.insert(out, '! Facility')
table.insert(out, '! Facility')
table.insert(out, '|-')
table.insert(out, '|-')

-- Values
-- Values
for _, material in ipairs(intermediateMaterials) do
for _, material in ipairs(intermediateMaterials) do
local row = createIntermediateMaterialRow(material['name'])
local row = createIntermediateMaterialRow(material['name'])
for _, value in ipairs(row) do
for _, value in ipairs(row) do
table.insert(out, value)
table.insert(out, value)
end
end
end
end
end
end


-- Add output data
-- Add output data
-- Headers
-- Headers
table.insert(out, '! colspan="2" | Output')
table.insert(out, '! colspan="2" | Output')
table.insert(out, '! Quantity')
table.insert(out, '! Quantity')
table.insert(out, '! Value')
table.insert(out, '! Value')
table.insert(out, '|-')
table.insert(out, '|-')
-- Values
table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]')
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue))
table.insert(out, '|-')
-- Add profit data
table.insert(out, '! colspan="3" | Profit')
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue - rawMaterialCost))
table.insert(out, '|}')


-- Values
return table.concat(out, '\n')
table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]')
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue))
table.insert(out, '|-')

-- Add profit data
table.insert(out, '! colspan="3" | Profit')
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue - rawMaterialCost))
table.insert(out, '|}')

return table.concat(out, '\n')
end
end


function p._extractRawMaterials(args)
function p._extractRawMaterials(args)
local rawMaterials = {}
local rawMaterials = {}


for i = 1, 9 do
for i = 1, 9 do
local materialKey = "rawmat" .. i
local materialKey = "rawmat" .. i
local quantityKey = "rawmat" .. i .. "qty"
local quantityKey = "rawmat" .. i .. "qty"


local materialValue = args[materialKey]
local materialValue = args[materialKey]
if materialValue and materialValue ~= "" then
if materialValue and materialValue ~= "" then
local quantityValue = tonumber(args[quantityKey]) or 1
local quantityValue = tonumber(args[quantityKey]) or 1
table.insert(rawMaterials, { name = materialValue, quantity = quantityValue })
table.insert(rawMaterials, { name = materialValue, quantity = quantityValue })
end
end
end
end
return rawMaterials
return rawMaterials
end
end


function p._getTrueRawMaterials(argsMaterials)
function p._getTrueRawMaterials(argsMaterials)
local queryResult = {}
local queryResult = {}
for _, item in pairs(argsMaterials) do
for _, item in pairs(argsMaterials) do
if item["name"] then
if item["name"] then
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item')
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item')
if result and result[1] and result[1]["Uses item"] then
local usesItems = result[1]["Uses item"]


if result and result[1] and result[1]["Uses item"] then
-- If usesItems is a table (multiple items), iterate and add each to queryResult
if type(usesItems) == "table" then
local usesItems = result[1]["Uses item"]

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