Editing Module:Products

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 3: Line 3:
local currency = require('Module:Currency').parse
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
local purge = require('Module:Purge')._purge


local p = {}
local p = {}
Line 12: Line 11:


local function recipe_sort(recipe_a, recipe_b)
local function recipe_sort(recipe_a, recipe_b)
--if one is nil but not both, put nil levels after known levels
-- Sort unknown levels to the end
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
return recipe_b.level == nil
return recipe_b.level == nil
end
end

-- if both are nil, sort by name
if recipe_a.level ~= nil then
if recipe_a.level == nil then
return recipe_a.output[1].name < recipe_b.output[1].name
end
-- if neither is nil, sort first by level
if recipe_a.level ~= recipe_b.level then
return recipe_a.level < recipe_b.level
return recipe_a.level < recipe_b.level
end
end


-- if neither is nil and levels are the same, sort by name
-- Sort by name if same level
return recipe_a.output[1].name < recipe_b.output[1].name
return recipe_a.output[1].name < recipe_b.output[1].name
end
end
Line 40: Line 33:
limit = 500
limit = 500
end
end

-- Query for data
-- Query for data
local smw_data = mw.smw.ask{
local smw_data = mw.smw.ask{
'[[Uses item::' .. item .. ']] OR [[Activity input::' .. item .. ']] OR [[Activity container::' .. item .. ']]',
'[[Uses item::' .. item .. ']]',
'?Uses item',
'?Recipe JSON',
'?Recipe JSON',
'?Activity JSON',
showValues and '?Value',
showValues and '?Value',
limit = limit
limit = limit
Line 55: Line 48:
end
end


-- Create a list of all recipes that use this item
-- Create a list of all recipes, grouped by output item (to keep them together in the sort)
local recipes = {}
local produced_items = {}
for _, product in ipairs(smw_data) do
for _, product in ipairs(smw_data) do
local jsons = product['Recipe JSON']
-- this part will need to be addressed if a page has both a recipe and an activity
local jsons = product['Recipe JSON'] or product['Activity JSON']
if type(jsons) == 'string' then
if type(jsons) == 'string' then
jsons = { jsons }
jsons = { jsons }
end
end
local parsed = {}
for _, json in ipairs(jsons) do
for _, json in ipairs(jsons) do
local json = mw.text.jsonDecode(json)
local json = mw.text.jsonDecode(json)
Line 70: Line 63:
return mat.name == item
return mat.name == item
end) then
end) then
table.insert(recipes, json)
table.insert(parsed, json)
end
end
end
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
end


-- Sort list of recipes by the level of the recipe (cross-profession)
-- Sort by the smallest recipe in the group
table.sort(recipes, recipe_sort)
table.sort(produced_items, function(item1, item2)
local first1 = item1[1]
local first2 = item2[1]
if (first1 == nil) ~= (first2 == nil) then
return first2 == nil
end
if first1 == nil then
return false -- Both empty, equivalent
end

return recipe_sort(first1, first2)
end)

-- Flatten into a single list
local recipes = {}
for _, product in ipairs(produced_items) do
for _, json in ipairs(product) do
table.insert(recipes, json)
end
end


-- Calculate shop prices
-- Calculate shop prices
Line 111: Line 125:
:tag('th')
:tag('th')
:attr{ colspan = '3' }
:attr{ colspan = '3' }
:wikitext('Product')
:wikitext('Recipe')
:done()
:done()
:tag('th'):wikitext('Level'):done()
:tag('th'):wikitext('Level'):done()
Line 117: Line 131:
:tag('th'):wikitext('Value'):done()
:tag('th'):wikitext('Value'):done()
:END()
:END()
:tag('th'):wikitext('Inputs'):done()
:tag('th'):wikitext('Ingredients'):done()
:IF(showPrices)
:IF(showPrices)
:tag('th'):wikitext('Price'):done()
:tag('th'):wikitext('Price'):done()
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: