Module:Products: Difference between revisions

"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
(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:
 
local function recipe_sort(recipe_a, recipe_b)
--if Sortone unknownis nil but not both, put nil levels toafter theknown endlevels
if (recipe_a.level == nil) ~= (recipe_b.level == nil) then
return recipe_b.level == nil
end
 
-- if recipe_a.levelboth ~=are nil, sort by thenname
if first1recipe_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
end
 
-- Sortif byneither nameis ifnil and levels are the same, levelsort by name
return recipe_a.output[1].name < recipe_b.output[1].name
end
Line 49 ⟶ 55:
end
 
-- Create a list of all recipes, groupedthat byuse outputthis item (to keep them together in the sort)
local produced_itemsrecipes = {}
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']
if type(jsons) == 'string' then
jsons = { jsons }
end
local parsed = {}
for _, json in ipairs(jsons) do
local json = mw.text.jsonDecode(json)
Line 64 ⟶ 70:
return mat.name == item
end) then
table.insert(parsedrecipes, json)
end
end
table.sort(parsed, recipe_sort)
table.insert(produced_items, parsed)
end
 
-- Sort list of recipes by the smallestlevel recipe inof the grouprecipe (cross-profession)
table.sort(produced_itemsrecipes, function(item1, item2recipe_sort)
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
13,097

edits