Module:Infobox Recipe: Difference between revisions

3,286 bytes added ,  Yesterday at 22:23
no edit summary
m (Spaces to tabs)
No edit summary
 
(18 intermediate revisions by 2 users not shown)
Line 1:
local currency = require('Module:Currency')
local hc = require('Module:Param Parse').has_content
local yn = require('Module:Yesno')
local editButton = require('Module:Edit button')
 
local function currency_cell(amount)
return currency._cell(amount, { html = 'yes' })
end
 
local p = {}
 
function p._main(frame)
local args = frame:getParent().args
local mw = require('mw')
local currency = require('Module:Currency').parse
local parse = require('Module:Param Parse')
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.
local showFullRecipe = yn(args.showFull or 'no', false)
 
 
--Get each of the rawmatX parameters from params and store their values in a new table
local argsMaterials = p._extractRawMaterials(args)
--empty tables to hold materials
local rawMaterials = {}
local intermediateMaterials = {}
 
--counter for total price of raw materials
local rawMaterialCost = 0
 
--get the value for the output product
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')0
if args.output1 then
output1Value = mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1') or 0
end
--Calculate total value of output material(s)
local output1TotalValue = 0
mw.logObjectif type(output1Value) == "table" then
if output1Value[1]['Value'] ~= nil then
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
else
output1TotalValue = 0
end
end
 
 
 
--Check if any of the raw mats provided are intermediate products, if they are, return their own raw materials
if showFullRecipe then
local trueRawMaterialsMaterials = p._getTrueRawMaterials(argsMaterials)
if next(trueRawMaterialsMaterials) ~= nil then
rawMaterials = trueRawMaterialsMaterials.rawMaterials
intermediateMaterials = argsMaterialsp._reverseTable(Materials.intermediateMaterials)
end
else
Line 41 ⟶ 56:
end
 
 
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0
local function getBuyPrice(material)
Line 49 ⟶ 66:
return 0
end
 
 
 
--If one of the raw materials provided as a param has been identified as an intermediate material then get the facility it is created at to display in the intermediate Ingredient row
Line 60 ⟶ 79:
return pageName
end
 
 
 
--Set SMW properties
for _,material in ipairs(argsMaterials) do
if args.rawmat1 then
mw.smw.set({
["Uses item"] = argsmaterial.rawmat1name,
["Uses item_and_quantity"] = material.name .. ',' .. tostring(material.quantity)
})
end
if args.rawmat2 then
mw.smw.set({
["Uses item"] = args.rawmat2,
})
end
if args.rawmat3 then
mw.smw.set({
["Uses item"] = args.rawmat3,
})
end
if args.facility then
mw.smw.set({
Line 83 ⟶ 96:
end
 
 
--Creates a row suitable for the raw materials section of the infobox. Quantity is optional, if no value is provided it will default to 1
 
local function createRawMaterialRow(item, quantity)
--Creates a row suitable for the raw materials section of the infobox.
local function createRawMaterialRow(item)
local materialBuyPrice = getBuyPrice(item)
rawMaterialCost = rawMaterialCost + materialBuyPrice
return mw.html.create('tr')
local row = {}
:tag('td')
table.insert(row, '| [[File:' .. item['name'] .. '.png|30px]]')
table.insert(row, :css{ '| [[' .. item['nameborder-right'] ..= ']]none') }
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
:done()
table.insert(row, '| style="text-align: right;" | ' .. currency(materialBuyPrice))
table.insert :tag(row, '|-td')
:css{ ['border-left'] = 'none' }
return row
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:wikitext(currency_cell(materialBuyPrice))
:done()
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
 
local function createIntermediateMaterialRow(item, quantity)
--Creates a row suitable for the intermediate materials section of the infobox.
local row = {}
local facilityfunction = getFacilitycreateIntermediateMaterialRow(item)
local facility = getFacility(item.name)
table.insert(row, '| [[File:' .. item .. '.png|30px]]')
return mw.html.create('tr')
table.insert(row, '| [[' .. item .. ']]')
:tag('td')
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
:css{ ['border-right'] = 'none' }
table.insert(row, '| style="text-align: right;" | [[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]')
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
table.insert(row, '|-')
:done()
return row
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'left' }
:wikitext('[[File:' .. facility .. '.png|30px|link=' .. facility .. ']] [[' .. facility .. ']]')
:done()
:done()
end
 
 
 
-- Recipe Table Head
local out = {}mw.html.create('table')
table.insert :addClass(out, '{| class="wikitable"')
:tag('tr')
table.insert(out, '! colspan="4" | Requirements')
:tag('th')
table.insert(out, '|-')
table.insert(out, '! :attr{ colspan ="2" | Facility')13' }
:wikitext('Requirements')
table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
:done()
table.insert(out, '|-')
:done()
table.insert(out, '! colspan="2" | Profession')
:tag('tr')
table.insert(out, '! Level')
:tag('th')
table.insert(out, '! XP')
:attr{ colspan = '2' }
table.insert(out, '|-')
:wikitext('Facility')
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
:done()
table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
:tag('td')
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
:attr{ colspan = '11' }
table.insert(out, '|-')
:wikitext(hc(args.facility) and ('[[File:%s.png|link=%s|30px]] [[%s]]'):format(args.facility, args.facility, args.facility) or editButton("'''?''' (edit)"))
 
:done()
:done()
:tag('tr')
:tag('th')
:attr{ colspan = '2' }
:wikitext('Profession')
:done()
:tag('th')
:wikitext('Level')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('XP')
:done()
:done()
:tag('tr')
:tag('td')
:attr{ colspan = '2' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.profession) and ('[[' .. args.profession .. ']]') or editButton("'''?''' (edit)"))
:done()
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.level) and (args.level) or editButton("'''?''' (edit)"))
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.exp) and (args.exp) or editButton("'''?''' (edit)"))
:done()
:done()
 
-- Add raw materials
-- Headers
:tag('tr')
table.insert(out, '! colspan="2" | Raw Ingredient')
:tag('th')
table.insert(out, '! Quantity')
:attr{ colspan = '2' }
table.insert(out, '! Cost')
:wikitext('Raw Ingredient')
table.insert(out, '|-')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Cost')
:done()
:done()
 
-- Values
for _, material in ipairs(rawMaterials) do
local row = out:node(createRawMaterialRow(material))
for _, value in ipairs(row) do
table.insert(out, value)
end
end
 
-- Add total raw cost
out
table.insert(out, '! colspan="3" | Total Raw cost')
:tag('tr')
table.insert(out, '| style="text-align: right;" | ' .. currency(rawMaterialCost))
:tag('th')
table.insert(out, '|-')
:attr{ colspan = '3' }
:wikitext('Total Raw cost')
:done()
:wikitext(currency_cell(rawMaterialCost))
:done()
 
-- Add Intermediate steps if required
Line 149 ⟶ 232:
if next(intermediateMaterials) ~= nil then
-- Headers
out
table.insert(out, '! colspan="2" | Intermediate Ingredient')
:tag('tr')
table.insert(out, '! Quantity')
:tag('th')
table.insert(out, '! Facility')
:attr{ colspan = '2' }
table.insert(out, '|-')
:wikitext('Intermediate Ingredient')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Faciltity')
:done()
:done()
 
-- Values
for _, material in ipairs(intermediateMaterials) do
local row = createIntermediateMaterialRowmw.logObject(material['name'])
out:node(createIntermediateMaterialRow(material))
for _, value in ipairs(row) do
table.insert(out, value)
end
end
end
Line 165 ⟶ 256:
-- Add output data
-- Headers
out
table.insert(out, '! colspan="2" | Output')
:tag('tr')
table.insert(out, '! Quantity')
:tag('th')
table.insert(out, '! Value')
:attr{ colspan = '2' }
table.insert(out, '|-')
:wikitext('Output')
 
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Value')
:done()
:done()
if args.output1 then
out
-- Values
:tag('tr')
table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]')
:tag('td')
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
:css{ ['border-right'] = 'none' }
table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]' or 'Unknown')
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue))
:done()
table.insert(out, '|-')
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(args.output1qty or 1)
:done()
:wikitext(currency_cell(output1TotalValue))
:done()
end
-- Add profit data
out
:tag('tr')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Profit')
:done()
:wikitext(currency_cell(output1TotalValue - rawMaterialCost))
:done()
 
return 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
 
 
 
function p._extractRawMaterials(args)
Line 201 ⟶ 321:
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.
--The materials required to create this intermediate material will be retrieved and added to the rawMaterials table.
--This allows for a full recipe to be shown where only intermediate ingredients were provided.
function p._getTrueRawMaterials(argsMaterials)
local queryResultrawMaterials = {}
local intermediateMaterials = {}
for _, item in pairs(argsMaterials) do
if item["name"] then
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item')
 
local function _processMaterial(material, quantity)
if result and result[1] and result[1]["Uses item"] then
if material then
local usesItems = result[1]["Uses item"]
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses item|?Uses item_and_quantity')
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"] = material, ["quantity"] = quantity })
-- Add the raw materials used by this intermediate material to rawMaterials
local usesItems = result[1]["Uses item"]
local quantities = result[1]["Uses item and quantity"]
 
if type(usesItems) == "table" then
-- If usesItems is a table (multiple items), iterate and add each to queryResult
for index, usedItem in ipairs(usesItems) do
if type(usesItems) == "table" then
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
for _, usedItem in ipairs(usesItems) do
local qty = 1
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
if quantities and type(quantities) == "table" then
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
local quantityString = quantities[index]
end
local _, q = quantityString:match("(.-),(%d+)")
else
qty = tonumber(q) or 1
-- If usesItems is a single item, add it directly
end
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
_processMaterial(pageName, qty)
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
end
end
else
end
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
end
local qty = 1
end
if quantities and type(quantities) == "string" then
return queryResult
local _, q = quantities:match("(.-),(%d+)")
qty = tonumber(q) or 1
end
_processMaterial(pageName, qty)
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity })
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 }
end
 
 
 
function p._reverseTable(t)
local reversed = {}
for i = #t, 1, -1 do
table.insert(reversed, t[i])
end
return reversed
end
 
 
 
return p
839

edits