Module:Products: Difference between revisions

Sort output based on recipe level
(Make limit a parameter; Make used item images smaller)
(Sort output based on recipe level)
Line 1:
require('Module:Mw.html extension')
local Array = require('Module:Array')
local currency = require('Module:Currency').parse
local yesno = require('Module:Yesno')
Line 7 ⟶ 8:
function p.main(frame)
return p._main(frame:getParent().args)
end
 
local function recipe_sort(recipe_a, recipe_b)
-- Sort unknown levels to the end
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
return recipe_b.level == nil
end
 
if recipe_a.level ~= nil then
return recipe_a.level < recipe_b.level
end
 
-- Sort by name if same level
return recipe_a.output[1].name < recipe_b.output[1].name
end
 
Line 31 ⟶ 46:
end
 
-- 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
iflocal jsons = type(product['Recipe JSON']) == 'string' then
if type(jsons) == 'string' then
product['Recipe JSON'] = { product['Recipe JSON'] }
jsons = { jsons }
end
local parsed = {}
for _, json in ipairs(product['Recipe JSON']) do
for _, json =in mw.text.jsonDecodeipairs(jsonjsons) do
local json = mw.text.jsonDecode(json)
json.Value = product.Value
-- Filter out when this item isn't actually used (Will happen on pages with multiple recipes)
if Array.any(json.materials, function(mat)
return mat.name == item
end) then
table.insert(parsed, json)
end
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
 
-- Sort by the smallest recipe in the group
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['Recipe JSON']) do
table.insert(recipes, json)
end
Line 72 ⟶ 119:
-- Create table
local out = mw.html.create('table')
:addClass('wikitable align-right-1 sortable')
:tag('tr')
:tag('th')
Line 92 ⟶ 139:
:tag('td')
:css{ ['border-right'] = '0', ['padding-right'] = '0' }
:attr{ ['data-sort-value'] = recipe.output[1].name }
:wikitext(recipe.output[1].quantity .. ' &times;')
:done()
Line 117 ⟶ 165:
:done()
else
row:tag('td'):wikitext('Unknown'):done()
:attr{ ['data-sort-value'] = '' }
:wikitext('Unknown')
:done()
end
end
 
local ingredients = row:tag('td')
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') }
:tag('ul')
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' }
707

edits