Module:Infobox Recipe: Difference between revisions

no edit summary
No edit summary
No edit summary
 
(12 intermediate revisions by the same user not shown)
Line 1:
local currency = require('Module:Currency')
local parsehc = require('Module:Param Parse').has_content
local yn = require('Module:Yesno')
local editButton = require('Module:Edit button')
 
local function currency_cell(amount)
Line 14 ⟶ 15:
--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
if type(output1Value[1]['Value']) ~== nil"table" then
output1TotalValue =if output1Value[1]['Value'] *~= (tonumber(args.output1qty)nil or 1)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
Line 38 ⟶ 50:
if next(Materials) ~= nil then
rawMaterials = Materials.rawMaterials
intermediateMaterials = p._reverseTable(Materials.intermediateMaterials)
end
else
rawMaterials = argsMaterials
end
 
 
--Simple query to get the shop buy price for the provided material, if no buy price is available returns 0
Line 52 ⟶ 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 63 ⟶ 79:
return pageName
end
 
 
 
--Set SMW properties
Line 71 ⟶ 89:
})
end
if args.facility then
mw.smw.set({
Line 76 ⟶ 95:
})
end
 
 
 
--Creates a row suitable for the raw materials section of the infobox.
Line 97 ⟶ 118:
:done()
end
 
 
 
--Creates a row suitable for the intermediate materials section of the infobox.
Line 104 ⟶ 127:
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
:done()
:tag('td')
Line 116 ⟶ 139:
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'rightleft' }
:wikitext('[[File:' .. facility .. '.png|30px|link=' .. facility .. ']] [[' .. facility .. ']]')
:done()
:done()
end
 
 
 
-- Recipe Table Head
Line 138 ⟶ 163:
:tag('td')
:attr{ colspan = '11' }
:wikitext(hc(args.facility) and ('[[File:%s.png|link=%s|30px]] [[%s]]'):format(args.facility, args.facility, args.facility) or editButton("'Unknown''?''' (edit)"))
 
:done()
:done()
Line 158 ⟶ 184:
:attr{ colspan = '2' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.profession) and ('[[' .. args.profession .. ']]') or editButton("'Unknown''?''' (edit)"))
:done()
:tag('td')
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.level) and (args.level) or editButton("'Unknown''?''' (edit)"))
:done()
:tag('td')
:attr{ colspan = '10' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.exp) and (args.exp) or editButton("'Unknown''?''' (edit)"))
:done()
:done()
 
-- Add raw materials
-- Headers
:tag('tr')
:tag('th')
Line 187 ⟶ 213:
:done()
 
-- Values
for _, material in ipairs(rawMaterials) do
out:node(createRawMaterialRow(material))
end
 
-- Add total raw cost
out
:tag('tr')
Line 202 ⟶ 228:
:done()
 
-- Add Intermediate steps if required
-- Headers
if next(intermediateMaterials) ~= nil then
Line 221 ⟶ 247:
:done()
 
-- Values
for _, material in ipairs(intermediateMaterials) do
mw.logObject(material)
out:node(createIntermediateMaterialRow(material))
end
end
 
-- Add output data
-- Headers
out
-- Headers
:tag('tr')
:tag('th')
Line 243 ⟶ 270:
:done()
:done()
if args.output1 then
 
-- Values
out
-- Values
:tag('tr')
:tag('td')
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
:css{ ['border-left'] = 'none' }
:wikitext(args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown')
:done()
:tag('td')
Line 260 ⟶ 289:
:wikitext(currency_cell(output1TotalValue))
:done()
end
 
-- Add profit data
out
:tag('tr')
:tag('th')
Line 272 ⟶ 302:
return out
end
 
 
 
function p._extractRawMaterials(args)
Line 288 ⟶ 320:
return rawMaterials
end
 
 
 
--This function takes a table containing the raw materials params received from the calling template.
Line 298 ⟶ 332:
local intermediateMaterials = {}
 
local function _processMaterial(material, quantity)
for _, item in pairs(argsMaterials) do
if item["name"]material then
local result = mw.smw.ask('[[:+]][[' .. item["name"]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"] = item["name"]material, ["quantity"] = item["quantity"] })
-- Add the raw materials used by this intermediate material to rawMaterials
Line 312 ⟶ 346:
for index, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantityqty = 1
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local _, qtyq = quantityString:match("(.-),(%d+)")
quantityqty = tonumber(qtyq) or 1
end
table.insert_processMaterial(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity }qty)
end
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local quantityqty = 1
if quantities and type(quantities) == "string" then
local _, qtyq = quantities:match("(.-),(%d+)")
quantityqty = tonumber(qtyq) or 1
end
table.insert_processMaterial(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity }qty)
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = item["name"]material, ["quantity"] = item["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