Module:Products: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(Make limit a parameter; Make used item images smaller) |
(Sort output based on recipe level) |
||
Line 1: | Line 1: | ||
require('Module:Mw.html extension') |
require('Module:Mw.html extension') |
||
local Array = require('Module:Array') |
|||
local currency = require('Module:Currency').parse |
local currency = require('Module:Currency').parse |
||
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
||
Line 7: | Line 8: | ||
function p.main(frame) |
function p.main(frame) |
||
return p._main(frame:getParent().args) |
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 |
end |
||
Line 31: | Line 46: | ||
end |
end |
||
-- Create a list of all recipes, grouped by output item (to keep them together in the sort) |
|||
⚫ | |||
local produced_items = {} |
|||
for _, product in ipairs(smw_data) do |
for _, product in ipairs(smw_data) do |
||
local jsons = product['Recipe JSON'] |
|||
if type(jsons) == 'string' then |
|||
product['Recipe JSON'] = { product['Recipe JSON'] } |
|||
jsons = { jsons } |
|||
end |
end |
||
local parsed = {} |
|||
⚫ | |||
for _, json in ipairs(jsons) do |
|||
local json = mw.text.jsonDecode(json) |
|||
json.Value = product.Value |
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 |
|||
⚫ | |||
for _, product in ipairs(produced_items) do |
|||
⚫ | |||
table.insert(recipes, json) |
table.insert(recipes, json) |
||
end |
end |
||
Line 72: | Line 119: | ||
-- Create table |
-- Create table |
||
local out = mw.html.create('table') |
local out = mw.html.create('table') |
||
:addClass('wikitable align-right-1') |
:addClass('wikitable align-right-1 sortable') |
||
:tag('tr') |
:tag('tr') |
||
:tag('th') |
:tag('th') |
||
Line 92: | Line 139: | ||
:tag('td') |
:tag('td') |
||
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
:css{ ['border-right'] = '0', ['padding-right'] = '0' } |
||
:attr{ ['data-sort-value'] = recipe.output[1].name } |
|||
:wikitext(recipe.output[1].quantity .. ' ×') |
:wikitext(recipe.output[1].quantity .. ' ×') |
||
:done() |
:done() |
||
Line 117: | Line 165: | ||
:done() |
:done() |
||
else |
else |
||
row:tag('td' |
row:tag('td') |
||
:attr{ ['data-sort-value'] = '' } |
|||
:wikitext('Unknown') |
|||
:done() |
|||
end |
end |
||
end |
end |
||
local ingredients = row:tag('td') |
local ingredients = row:tag('td') |
||
:attr{ ['data-sort-value'] = table.concat(Array.map(recipe.materials, function(item) return item.name end), '\0') } |
|||
:tag('ul') |
:tag('ul') |
||
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |
:css{ ['list-style'] = 'none', ['margin'] = '0', ['padding-left'] = '0' } |