Module:Products: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(extend module to work on data from Profession info as well as Infobox Recipe) |
("pre-sort by output item" doesnt work because it was sorting by page that had the recipe, instead simply sort by level of the recipe) |
||
Line 12: | Line 12: | ||
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 |
||
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 |
-- if both are nil, sort by name |
||
⚫ | |||
return recipe_a.output[1].name < recipe_b.output[1].name |
|||
⚫ | |||
-- 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 |
||
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 49: | Line 55: | ||
end |
end |
||
-- Create a list of all recipes |
-- Create a list of all recipes that use this item |
||
local |
local recipes = {} |
||
for _, product in ipairs(smw_data) do |
for _, product in ipairs(smw_data) do |
||
-- 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'] |
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 64: | Line 70: | ||
return mat.name == item |
return mat.name == item |
||
end) then |
end) then |
||
table.insert( |
table.insert(recipes, json) |
||
end |
end |
||
end |
end |
||
table.sort(parsed, recipe_sort) |
|||
table.insert(produced_items, parsed) |
|||
end |
end |
||
-- Sort by the |
-- Sort list of recipes by the level of the recipe (cross-profession) |
||
table.sort( |
table.sort(recipes, recipe_sort) |
||
local first1 = item1[1] |
|||
local first2 = item2[1] |
|||
if (first1 == nil) ~= (first2 == nil) then |
|||
return first2 == nil |
|||
end |
|||
⚫ | |||
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 |
|||
⚫ | |||
-- Calculate shop prices |
-- Calculate shop prices |