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 currency = require('Module:Currency')
local hc = require('Module:Param Parse').has_content
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 15: 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 = 0
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 type(output1Value) == "table" then
if output1Value[1]['Value'] ~= nil then
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
Line 50: Line 39:
if next(Materials) ~= nil then
if next(Materials) ~= nil then
rawMaterials = Materials.rawMaterials
rawMaterials = Materials.rawMaterials
intermediateMaterials = p._reverseTable(Materials.intermediateMaterials)
intermediateMaterials = Materials.intermediateMaterials
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
Line 66: Line 55:
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 79: Line 66:
return pageName
return pageName
end
end




--Set SMW properties
--Set SMW properties
Line 89: Line 74:
})
})
end
end
if args.facility then
if args.facility then
mw.smw.set({
mw.smw.set({
Line 95: Line 79:
})
})
end
end




--Creates a row suitable for the raw materials section of the infobox.
--Creates a row suitable for the raw materials section of the infobox.
Line 118: Line 100:
:done()
:done()
end
end




--Creates a row suitable for the intermediate materials section of the infobox.
--Creates a row suitable for the intermediate materials section of the infobox.
local function createIntermediateMaterialRow(item)
local function createIntermediateMaterialRow(item)
local facility = getFacility(item.name)
local facility = getFacility(item)
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.name .. '.png|30px|link=' .. item.name .. ']]')
:wikitext('[[File:' .. item .. '.png|30px|link=' .. item .. ']]')
:done()
:done()
:tag('td')
:tag('td')
:css{ ['border-left'] = 'none' }
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. item.name .. ']]')
:wikitext('[[' .. item .. ']]')
:done()
:done()
:tag('td')
:tag('td')
Line 139: Line 119:
:tag('td')
:tag('td')
:attr{ colspan = '10' }
:attr{ colspan = '10' }
:css{ ['text-align'] = 'left' }
: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 163: Line 141:
:tag('td')
:tag('td')
:attr{ colspan = '11' }
: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)"))
: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 184: Line 161:
:attr{ colspan = '2' }
:attr{ colspan = '2' }
:css{ ['text-align'] = 'center' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.profession) and ('[[' .. args.profession .. ']]') or editButton("'''?''' (edit)"))
:wikitext(args.profession and '[[' .. args.profession .. ']]' or 'Unknown')
:done()
:done()
:tag('td')
:tag('td')
:css{ ['text-align'] = 'center' }
:css{ ['text-align'] = 'center' }
:wikitext(hc(args.level) and (args.level) or editButton("'''?''' (edit)"))
: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(hc(args.exp) and (args.exp) or editButton("'''?''' (edit)"))
:wikitext(args.exp or 'Unknown')
:done()
:done()
:done()
:done()


-- Add raw materials
-- Add raw materials
-- Headers
-- Headers
:tag('tr')
:tag('tr')
:tag('th')
:tag('th')
Line 213: Line 190:
:done()
:done()


-- Values
-- Values
for _, material in ipairs(rawMaterials) do
for _, material in ipairs(rawMaterials) do
mw.logObject(material)
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 228: Line 206:
: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 247: Line 225:
:done()
:done()


-- Values
-- Values
for _, material in ipairs(intermediateMaterials) do
for _, material in ipairs(intermediateMaterials) do
mw.logObject(material)
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 270: Line 247:
:done()
:done()
:done()
:done()

if args.output1 then
-- Values
out
-- Values
:tag('tr')
:tag('tr')
:tag('td')
:tag('td')
:css{ ['border-right'] = 'none' }
:css{ ['border-right'] = 'none' }
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]' or 'Unknown')
:wikitext('[[File:' .. args.output1 .. '.png|30px|link=' .. args.output1 .. ']]')
:done()
:done()
:tag('td')
:tag('td')
:css{ ['border-left'] = 'none' }
:css{ ['border-left'] = 'none' }
:wikitext('[[' .. args.output1 .. ']]' or 'Unknown')
:wikitext(args.output1 and '[[' .. args.output1 .. ']]' or 'Unknown')
:done()
:done()
:tag('td')
:tag('td')
Line 289: Line 264:
:wikitext(currency_cell(output1TotalValue))
:wikitext(currency_cell(output1TotalValue))
:done()
:done()

end
-- Add profit data
-- Add profit data
out
:tag('tr')
:tag('tr')
:tag('th')
:tag('th')
Line 302: Line 276:
return out
return out
end
end




function p._extractRawMaterials(args)
function p._extractRawMaterials(args)
Line 320: Line 292:
return rawMaterials
return rawMaterials
end
end




--This function takes a table containing the raw materials params received from the calling template.
--This function takes a table containing the raw materials params received from the calling template.
Line 332: Line 302:
local intermediateMaterials = {}
local intermediateMaterials = {}


for _, item in pairs(argsMaterials) do
local function _processMaterial(material, quantity)
if material then
if item["name"] then
local result = mw.smw.ask('[[:+]][[' .. material .. ']]|?Uses item|?Uses item_and_quantity')
local result = mw.smw.ask('[[:+]][[' .. item["name"] .. ']]|?Uses item|?Uses item_and_quantity')
--mw.logObject(result)
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
-- The item is an intermediate material, add it to intermediateMaterials
table.insert(intermediateMaterials, { ["name"] = material, ["quantity"] = quantity })
table.insert(intermediateMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
-- Add the raw materials used by this intermediate material to rawMaterials
-- Add the raw materials used by this intermediate material to rawMaterials
Line 346: Line 317:
for index, usedItem in ipairs(usesItems) do
for index, usedItem in ipairs(usesItems) do
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usedItem:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local qty = 1
local quantity = 1
if quantities and type(quantities) == "table" then
if quantities and type(quantities) == "table" then
local quantityString = quantities[index]
local quantityString = quantities[index]
local _, q = quantityString:match("(.-),(%d+)")
local _, qty = quantityString:match("(.-),(%d+)")
qty = tonumber(q) or 1
quantity = tonumber(qty) or 1
mw.logObject(tonumber(qty))
end
end
_processMaterial(pageName, qty)
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity })
end
end
else
else
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local pageName = usesItems:gsub("%[%[", ""):gsub("%]%]", ""):gsub("|.*", ""):gsub("^:", "")
local qty = 1
local quantity = 1
if quantities and type(quantities) == "string" then
if quantities and type(quantities) == "string" then
local _, q = quantities:match("(.-),(%d+)")
local _, qty = quantities:match("(.-),(%d+)")
qty = tonumber(q) or 1
quantity = tonumber(qty) or 1
end
end
_processMaterial(pageName, qty)
table.insert(rawMaterials, { ["name"] = pageName, ["quantity"] = quantity })
end
end
else
else
-- The item is a raw material, add it to rawMaterials
-- The item is a raw material, add it to rawMaterials
table.insert(rawMaterials, { ["name"] = material, ["quantity"] = quantity })
table.insert(rawMaterials, { ["name"] = item["name"], ["quantity"] = item["quantity"] })
mw.logObject(item)
end
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
end
end
Line 379: Line 345:
return { rawMaterials = rawMaterials, intermediateMaterials = intermediateMaterials }
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: