Editing Module:Infobox Recipe
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
require('strict') |
|||
require('Module:Mw.html extension') |
|||
local currency = require('Module:Currency') |
local currency = require('Module:Currency') |
||
local |
local parse = require('Module:Param Parse') |
||
local yn = require('Module:Yesno') |
local yn = require('Module:Yesno') |
||
local editButton = require('Module:Edit button') |
|||
local function currency_cell(amount) |
local function currency_cell(amount) |
||
Line 17: | Line 14: | ||
--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) |
||
--mw.logObject(argsMaterials) |
|||
--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 = |
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1') |
||
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 |
local output1TotalValue = 0 |
||
if |
if output1Value[1]['Value'] ~= nil then |
||
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1) |
|||
else |
|||
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1) |
|||
output1TotalValue = 0 |
|||
else |
|||
output1TotalValue = 0 |
|||
end |
|||
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 |
||
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 |
||
intermediateMaterials = |
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 |
||
Line 68: | Line 54: | ||
return 0 |
return 0 |
||
end |
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 |
--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 81: | Line 65: | ||
return pageName |
return pageName |
||
end |
end |
||
--Set SMW properties |
--Set SMW properties |
||
if args.rawmat1 then |
|||
for _,material in ipairs(argsMaterials) do |
|||
mw.smw.set({ |
|||
["Uses item"] = |
["Uses item"] = args.rawmat1, |
||
}) |
|||
["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 |
end |
||
if args.facility then |
if args.facility then |
||
mw.smw.set({ |
mw.smw.set({ |
||
Line 98: | Line 88: | ||
end |
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) |
|||
local materialBuyPrice = getBuyPrice(item) |
|||
--Creates a row suitable for the raw materials section of the infobox. |
|||
local function createRawMaterialRow(item) |
|||
local materialBuyPrice = getBuyPrice(item) * (item.quantity or 1) |
|||
rawMaterialCost = rawMaterialCost + materialBuyPrice |
rawMaterialCost = rawMaterialCost + materialBuyPrice |
||
return mw.html.create('tr') |
return mw.html.create('tr') |
||
Line 115: | Line 103: | ||
:tag('td') |
:tag('td') |
||
:css{ ['text-align'] = 'right' } |
:css{ ['text-align'] = 'right' } |
||
:wikitext( |
:wikitext(quantity or 1) |
||
:done() |
:done() |
||
:wikitext(currency_cell(materialBuyPrice)) |
:wikitext(currency_cell(materialBuyPrice)) |
||
Line 121: | Line 109: | ||
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 |
|||
local function createIntermediateMaterialRow(item, quantity) |
|||
local facility = getFacility(item) |
|||
--Creates a row suitable for the intermediate materials section of the infobox. |
|||
local function createIntermediateMaterialRow(item) |
|||
local facility = getFacility(item.name) |
|||
return mw.html.create('tr') |
return mw.html.create('tr') |
||
:tag('td') |
:tag('td') |
||
:css{ ['border-right'] = 'none' } |
:css{ ['border-right'] = 'none' } |
||
:wikitext('[[File:' .. item |
:wikitext('[[File:' .. item .. '.png|30px|link=' .. item .. ']]') |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:css{ ['border-left'] = 'none' } |
:css{ ['border-left'] = 'none' } |
||
:wikitext('[[' .. item |
:wikitext('[[' .. item .. ']]') |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:css{ ['text-align'] = 'right' } |
:css{ ['text-align'] = 'right' } |
||
:wikitext( |
:wikitext(quantity or 1) |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:attr{ colspan = '10' } |
:attr{ colspan = '10' } |
||
:css{ ['text-align'] = ' |
:css{ ['text-align'] = 'right' } |
||
:wikitext('[[File:' .. facility .. '.png|30px|link=' .. facility .. ']] [[' .. facility .. ']]') |
:wikitext('[[File:' .. facility .. '.png|30px|link=' .. facility .. ']] [[' .. facility .. ']]') |
||
:done() |
:done() |
||
:done() |
:done() |
||
end |
end |
||
-- Recipe Table Head |
-- Recipe Table Head |
||
Line 165: | Line 149: | ||
:tag('td') |
:tag('td') |
||
:attr{ colspan = '11' } |
:attr{ colspan = '11' } |
||
:wikitext |
:wikitext(args.facility and ('[[File:%s.png|link=%s|30px]] [[%s]]'):format(args.facility, args.facility, args.facility) or 'Unknown') |
||
:done() |
:done() |
||
:done() |
:done() |
||
Line 186: | Line 169: | ||
:attr{ colspan = '2' } |
:attr{ colspan = '2' } |
||
:css{ ['text-align'] = 'center' } |
:css{ ['text-align'] = 'center' } |
||
:wikitext |
:wikitext(args.profession and '[[' .. args.profession .. ']]' or 'Unknown') |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:css{ ['text-align'] = 'center' } |
:css{ ['text-align'] = 'center' } |
||
:wikitext |
:wikitext(args.level or 'Unknown') |
||
:done() |
:done() |
||
:tag('td') |
:tag('td') |
||
:attr{ colspan = '10' } |
:attr{ colspan = '10' } |
||
:css{ ['text-align'] = 'center' } |
:css{ ['text-align'] = 'center' } |
||
:wikitext |
:wikitext(args.exp or 'Unknown') |
||
:done() |
:done() |
||
:done() |
:done() |
||
-- Add raw materials |
-- Add raw materials |
||
-- Headers |
|||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 215: | Line 198: | ||
:done() |
:done() |
||
-- Values |
|||
for _, material in ipairs(rawMaterials) do |
for _, material in ipairs(rawMaterials) do |
||
out:node(createRawMaterialRow(material)) |
out:node(createRawMaterialRow(material)) |
||
end |
end |
||
-- Add total raw cost |
-- Add total raw cost |
||
out |
out |
||
:tag('tr') |
:tag('tr') |
||
Line 230: | Line 213: | ||
:done() |
:done() |
||
-- Add Intermediate steps if required |
-- Add Intermediate steps if required |
||
-- Headers |
-- Headers |
||
if next(intermediateMaterials) ~= nil then |
if next(intermediateMaterials) ~= nil then |
||
Line 249: | Line 232: | ||
:done() |
:done() |
||
-- Values |
|||
for _, material in ipairs(intermediateMaterials) do |
for _, material in ipairs(intermediateMaterials) do |
||
out:node(createIntermediateMaterialRow(material.name)) |
|||
out:node(createIntermediateMaterialRow(material)) |
|||
end |
end |
||
end |
end |
||
-- Add output data |
-- Add output data |
||
-- Headers |
|||
out |
out |
||
-- Headers |
|||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 272: | Line 254: | ||
:done() |
:done() |
||
:done() |
:done() |
||
:IF(args.output1) |
|||
-- Values |
|||
:tag('tr') |
|||
:tag('td') |
|||
:css{ ['border-right'] = 'none' } |
|||
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]') |
|||
:done() |
|||
: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() |
:done() |
||
: |
:tag('td') |
||
:css{ ['border-left'] = 'none' } |
|||
-- Add profit data |
|||
:wikitext(args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown') |
|||
:done() |
|||
:tag('td') |
|||
:css{ ['text-align'] = 'right' } |
|||
:wikitext(args.output1qty or 1) |
|||
:done() |
|||
:wikitext(currency_cell(output1TotalValue)) |
|||
:done() |
|||
-- Add profit data |
|||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 302: | Line 283: | ||
return out |
return out |
||
end |
end |
||
function p._extractRawMaterials(args) |
function p._extractRawMaterials(args) |
||
Line 321: | Line 300: | ||
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. |
|||
--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) |
function p._getTrueRawMaterials(argsMaterials) |
||
local rawMaterials = {} |
|||
local intermediateMaterials = {} |
|||
for _, item in pairs(argsMaterials) do |
|||
local function _processMaterial(material, quantity) |
|||
if item["name"] then |
|||
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item') |
|||
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 result and result[1] and result[1]["Uses item"] then |
|||
if type(usesItems) == "table" then |
|||
-- The item is an intermediate material, add it to intermediateMaterials |
|||
for index, usedItem in ipairs(usesItems) do |
|||
table.insert(intermediateMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] }) |
|||
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
|||
local qty = 1 |
|||
-- Add the raw materials used by this intermediate material to rawMaterials |
|||
if quantities and type(quantities) == "table" then |
|||
local usesItems = result[1]["Uses item"] |
|||
local quantityString = quantities[index] |
|||
local _, q = quantityString:match("(.-),(%d+)") |
|||
qty = tonumber(q) or 1 |
|||
end |
|||
_processMaterial(pageName, qty) |
|||
end |
|||
else |
|||
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
|||
local qty = 1 |
|||
if quantities and type(quantities) == "string" then |
|||
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 |
|||
if type(usesItems) == "table" then |
|||
-- Iterate over the initial argsMaterials |
|||
for _, usedItem in ipairs(usesItems) do |
|||
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
|||
if item["name"] then |
|||
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1 }) |
|||
end |
|||
else |
|||
end |
|||
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "") |
|||
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = 1 }) |
|||
end |
|||
else |
|||
-- The item is a raw material, add it to rawMaterials |
|||
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] }) |
|||
end |
|||
end |
|||
end |
|||
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials } |
|||
end |
end |
||
function p._reverseTable(t) |
|||
local reversed = {} |
|||
for i = #t, 1, -1 do |
|||
table.insert(reversed, t[i]) |
|||
end |
|||
return reversed |
|||
end |
|||
return p |
return p |