Editing Module:Infobox Recipe

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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:
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 = {}
local p = {}


function p._main(frame)
function p._main(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local mw = require('mw')

local currency = require('Module:Currency').parse
--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 parse = require('Module:Param Parse')
local showFullRecipe = yn(args.showFull or 'no', false)
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.
--Get each of the rawmatX parameters from params and store their values in a new table
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)
local argsMaterials = p._extractRawMaterials(args)
mw.logObject(argsMaterials)
--empty tables to hold materials
--empty tables to hold materials
local rawMaterials = {}
local rawMaterials = {}
Line 25: Line 19:
--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 = 0
local output1Value = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1')
local output1TotalValue
if args.output1 then
if output1Value ~= nil then
output1Value = mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1') or 0
output1TotalValue = output1Value[1]['Value'] * (tonumber(args.output1qty) or 1)
else
output1TotalValue = 0
end
end
--Calculate total value of output material(s)
local output1TotalValue = 0
if 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
--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 trueRawMaterials = p._getTrueRawMaterials(argsMaterials)
if next(Materials) ~= nil then
if next(trueRawMaterials) ~= nil then
rawMaterials = Materials.rawMaterials
rawMaterials = trueRawMaterials
intermediateMaterials = p._reverseTable(Materials.intermediateMaterials)
intermediateMaterials = argsMaterials
end
end
else
else
rawMaterials = argsMaterials
rawMaterials = argsMaterials
end
end
mw.logObject(rawMaterials)



--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)
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




--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 74: Line 57:
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
for _,material in ipairs(argsMaterials) do
mw.smw.set({
mw.smw.set({
["Uses item"] = material.name,
["Uses item"] = args.rawmat1,
})
["Uses item_and_quantity"] = material.name .. ',' .. tostring(material.quantity)
end
})
if args.rawmat2 then
end
mw.smw.set({
["Uses item"] = args.rawmat2,
if args.facility then
})
end
if args.rawmat3 then
mw.smw.set({
["Uses item"] = args.rawmat3,
})
end
if args.facility then
mw.smw.set({
mw.smw.set({
["Uses facility"] = args.facility
["Uses facility"] = args.facility
Line 96: Line 85:
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)

--Creates a row suitable for the raw materials section of the infobox.
local function createRawMaterialRow(item)
local materialBuyPrice = getBuyPrice(item)
local materialBuyPrice = getBuyPrice(item)
rawMaterialCost = rawMaterialCost + materialBuyPrice
rawMaterialCost = rawMaterialCost + materialBuyPrice
local row = {}
return mw.html.create('tr')
table.insert(row, '| [[File:' .. item['name'] .. '.png|30px]]')
:tag('td')
table.insert(row, '| [[' .. item['name'] .. ']]')
:css{ ['border-right'] = 'none' }
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
table.insert(row, '| style="text-align: right;" | ' .. currency(materialBuyPrice))
:done()
table.insert(row, '|-')
:tag('td')
return row
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. item.name .. ']]')
:done()
:tag('td')
:css{ ['text-align'] = 'right' }
:wikitext(item.quantity or 1)
:done()
:wikitext(currency_cell(materialBuyPrice))
:done()
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 row = {}
--Creates a row suitable for the intermediate materials section of the infobox.
local function createIntermediateMaterialRow(item)
local facility = getFacility(item)
table.insert(row, '| [[File:' .. item .. '.png|30px]]')
local facility = getFacility(item.name)
table.insert(row, '| [[' .. item .. ']]')
return mw.html.create('tr')
table.insert(row, '| style="text-align: right;" | ' .. (quantity or 1))
:tag('td')
table.insert(row, '| style="text-align: right;" | [[File:' .. facility .. '.png|30px]] [[' .. facility .. ']]')
:css{ ['border-right'] = 'none' }
table.insert(row, '|-')
:wikitext('[[File:' .. item.name .. '.png|30px|link=' .. item.name .. ']]')
return row
:done()
end
: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
-- Recipe Table Head
local out = mw.html.create('table')
local out = {}
:addClass('wikitable')
table.insert(out, '{| class="wikitable"')
table.insert(out, '! colspan="4" | Requirements')
:tag('tr')
table.insert(out, '|-')
:tag('th')
table.insert(out, '! colspan="2" | Facility')
:attr{ colspan = '13' }
table.insert(out, '| colspan="2" | ' .. (args.facility and '[[' .. args.facility .. ']]' or 'Unknown'))
:wikitext('Requirements')
table.insert(out, '|-')
:done()
table.insert(out, '! colspan="2" | Profession')
:done()
table.insert(out, '! Level')
:tag('tr')
table.insert(out, '! XP')
:tag('th')
table.insert(out, '|-')
:attr{ colspan = '2' }
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. (args.profession and '[[' .. args.profession .. ']]' or 'Unknown'))
:wikitext('Facility')
table.insert(out, '| style="text-align: center;" | ' .. (args.level or 'Unknown'))
:done()
table.insert(out, '| style="text-align: center;" | ' .. (args.exp or 'Unknown'))
:tag('td')
table.insert(out, '|-')
:attr{ colspan = '11' }
: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
-- Add raw materials
-- Headers
-- Headers
table.insert(out, '! colspan="2" | Raw Ingredient')
:tag('tr')
table.insert(out, '! Quantity')
:tag('th')
table.insert(out, '! Cost')
:attr{ colspan = '2' }
table.insert(out, '|-')
:wikitext('Raw Ingredient')
:done()
:tag('th')
:wikitext('Quantity')
:done()
:tag('th')
:attr{ colspan = '10' }
:wikitext('Cost')
:done()
:done()


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


-- Add total raw cost
-- Add total raw cost
table.insert(out, '! colspan="3" | Total Raw cost')
out
table.insert(out, '| style="text-align: right;" | ' .. currency(rawMaterialCost))
:tag('tr')
table.insert(out, '|-')
:tag('th')
:attr{ colspan = '3' }
:wikitext('Total Raw cost')
:done()
:wikitext(currency_cell(rawMaterialCost))
:done()

-- 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')
out
table.insert(out, '! Quantity')
:tag('tr')
table.insert(out, '! Facility')
:tag('th')
table.insert(out, '|-')
:attr{ colspan = '2' }
:wikitext('Intermediate Ingredient')
-- Values
:done()
for _, material in ipairs(intermediateMaterials) do
:tag('th')
local row = createIntermediateMaterialRow(material['name'])
:wikitext('Quantity')
for _, value in ipairs(row) do
:done()
table.insert(out, value)
:tag('th')
end
:attr{ colspan = '10' }
end
:wikitext('Faciltity')
end
:done()
:done()

-- Values
for _, material in ipairs(intermediateMaterials) do
mw.logObject(material)
out:node(createIntermediateMaterialRow(material))
end
end


-- Add output data
-- Add output data
-- Headers
-- Headers
table.insert(out, '! colspan="2" | Output')
out
table.insert(out, '! Quantity')
:tag('tr')
table.insert(out, '! Value')
:tag('th')
table.insert(out, '|-')
:attr{ colspan = '2' }
:wikitext('Output')
-- Values
:done()
table.insert(out, '| [[File:' .. args.output1 .. '.png|30px]]')
:tag('th')
table.insert(out, '| ' .. (args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown'))
:wikitext('Quantity')
table.insert(out, '| style="text-align: right;" | ' .. (args.output1qty or 1))
:done()
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue))
:tag('th')
table.insert(out, '|-')
:attr{ colspan = '10' }
:wikitext('Value')
-- Add profit data
:done()
table.insert(out, '! colspan="3" | Profit')
:done()
table.insert(out, '| style="text-align: right;" | ' .. currency(output1TotalValue - rawMaterialCost))
if args.output1 then
table.insert(out, '|}')
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 .. ']]' 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
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




--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 queryResult = {}
for _, item in pairs(argsMaterials) do
local intermediateMaterials = {}
if item["name"] then

local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item')
local function _processMaterial(material, quantity)
if material then
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses item|?Uses item_and_quantity')
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"] = material, ["quantity"] = quantity })
-- Add the raw materials used by this intermediate material to rawMaterials
local usesItems = result[1]["Uses item"]
local usesItems = result[1]["Uses item"]
local quantities = result[1]["Uses item and quantity"]


-- If usesItems is a table (multiple items), iterate and add each to queryResult
if type(usesItems) == "table" then
if type(usesItems) == "table" then
for index, 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("^:", "")
local qty = 1
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local _, q = quantityString:match("(.-),(%d+)")
qty = tonumber(q) or 1
end
_processMaterial(pageName, qty)
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("^:", "")
local qty = 1
table.insert(queryResult, { ["name"] = pageName, ["quantity"] = 1 })
if quantities and type(quantities) == "string" then
local _, q = quantities:match("(.-),(%d+)")
qty = tonumber(q) or 1
end
_processMaterial(pageName, qty)
end
end
else
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity })
end
end
end
end
end
end
return queryResult

-- 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
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
Please note that all contributions to Brighter Shores Wiki are considered to be released under the CC BY-NC-SA 3.0 (see Brighter Shores:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

This page is a member of a hidden category: