Module:GetRecipes: Difference between revisions
From Eco - English Wiki
[unchecked revision] | [unchecked revision] |
HACK: Handle issue with Tools data |
Add ability to get recipes for a tag |
||
Line 13: | Line 13: | ||
-- assign variables for item, table, and group | -- assign variables for item, table, and group | ||
local item = args.item | local item = args.item | ||
local | local tag = args.tag | ||
local craftTable = args.table | |||
local group = args.group | local group = args.group | ||
Line 33: | Line 34: | ||
-- priority: item > table > group | -- priority: item > table > group | ||
if item ~= nil then | if item ~= nil then | ||
local itemProducts | |||
-- HACK Attempt to handle issue with tool recipes | |||
if productsList[item] and #productsList[item] == 1 and productsList[item][1] == '' then | |||
itemProducts = {item} | |||
elseif productsList[item] then | |||
itemProducts = productsList[item] | |||
end | |||
if itemProducts ~= nil then | |||
local built = 1 | local built = 1 | ||
for _, itemProduct in ipairs( | for _, itemProduct in ipairs(itemProducts) do | ||
if (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then | if (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then | ||
products[built] = recipeList[itemProduct] | products[built] = recipeList[itemProduct] | ||
Line 47: | Line 53: | ||
end | end | ||
if | local itemIngredients | ||
-- | if tag and tag == 'true' then | ||
if | itemIngredients = {} | ||
-- Tags do not appear in the 'products' list, so have to process manually | |||
for recipeName, recipe in pairs(recipeList) do | |||
local foundIngredient = false | |||
for _, variant in pairs(recipe.variants) do | |||
for _, ingredient in pairs(variant.ingredients) do | |||
if ingredient[1] == 'TAG' and ingredient[2] == item then | |||
foundIngredient = true | |||
end | |||
end | |||
end | |||
if foundIngredient then | |||
table.insert(itemIngredients, recipeName) | |||
end | |||
end | end | ||
else | |||
for _, itemIngredient in ipairs( | if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then | ||
itemIngredients = {item} | |||
elseif ingredientsList[item] then | |||
itemIngredients = ingredientsList[item] | |||
end | |||
end | |||
if itemIngredients ~= nil then | |||
for _, itemIngredient in ipairs(itemIngredients) do | |||
if itemIngredient ~= '' then | if itemIngredient ~= '' then | ||
if recipeList[itemIngredient] ~= nil then | if recipeList[itemIngredient] ~= nil then | ||
ingredients | table.insert(ingredients, recipeList[itemIngredient]) | ||
-- HACK: Skill books don't show us as a product of research pages, only the skill name. | -- HACK: Skill books don't show us as a product of research pages, only the skill name. | ||
elseif recipeList[itemIngredient .. ' Skill Book'] ~= nil then | elseif recipeList[itemIngredient .. ' Skill Book'] ~= nil then | ||
ingredients | table.insert(ingredients, recipeList[itemIngredient .. ' Skill Book']) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
mw.logObject(ingredients) | |||
return parser.formattedItem(products, ingredients, item) | --return parser.formattedItem(products, ingredients, item) | ||
elseif | elseif craftTable ~= nil then | ||
for num = 1, #tableList[ | for num = 1, #tableList[craftTable] do | ||
recipes[num] = recipeList[tableList[ | recipes[num] = recipeList[tableList[craftTable][num]] | ||
end | end | ||
return parser.formattedTable(recipes, | return parser.formattedTable(recipes, craftTable) | ||
else | else |
Revision as of 21:29, 27 February 2021
Documentation
This module is called by Template:GetRecipes, and is the entry point for modules in the system that displays crafting recipes. It takes the item passed to the template, and uses Module:RecipeData to get every recipe related to the item. The resulting list of recipes is then passed to Module:CraftingParser, and the results from CraftingParser are returned to the template.
If the template is passed, this module creates a table using details from the following Modules:
-- Module:GetRecipes (https://wiki.play.eco/en/Module:GetRecipes)
local p = {}
local Utils = require('Module:Utils')
-- Main entry point for the Module
function p.main(frame)
local test = ''
-- get args from the Template
local args = Utils.normaliseArgs(frame)
-- assign variables for item, table, and group
local item = args.item
local tag = args.tag
local craftTable = args.table
local group = args.group
-- load list of recipes
local completeList = require("Module:CraftingRecipes")
local recipeList = completeList.recipes
local ingredientsList = completeList.ingredients
local productsList = completeList.products
local tableList = completeList.tables
-- initialize a table to store recipes
local recipes = {}
local products = {}
local ingredients = {}
-- load CraftingParser
local parser = require("Module:CraftingParser")
-- priority: item > table > group
if item ~= nil then
local itemProducts
-- HACK Attempt to handle issue with tool recipes
if productsList[item] and #productsList[item] == 1 and productsList[item][1] == '' then
itemProducts = {item}
elseif productsList[item] then
itemProducts = productsList[item]
end
if itemProducts ~= nil then
local built = 1
for _, itemProduct in ipairs(itemProducts) do
if (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then
products[built] = recipeList[itemProduct]
built = built + 1
end
end
end
local itemIngredients
if tag and tag == 'true' then
itemIngredients = {}
-- Tags do not appear in the 'products' list, so have to process manually
for recipeName, recipe in pairs(recipeList) do
local foundIngredient = false
for _, variant in pairs(recipe.variants) do
for _, ingredient in pairs(variant.ingredients) do
if ingredient[1] == 'TAG' and ingredient[2] == item then
foundIngredient = true
end
end
end
if foundIngredient then
table.insert(itemIngredients, recipeName)
end
end
else
if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then
itemIngredients = {item}
elseif ingredientsList[item] then
itemIngredients = ingredientsList[item]
end
end
if itemIngredients ~= nil then
for _, itemIngredient in ipairs(itemIngredients) do
if itemIngredient ~= '' then
if recipeList[itemIngredient] ~= nil then
table.insert(ingredients, recipeList[itemIngredient])
-- HACK: Skill books don't show us as a product of research pages, only the skill name.
elseif recipeList[itemIngredient .. ' Skill Book'] ~= nil then
table.insert(ingredients, recipeList[itemIngredient .. ' Skill Book'])
end
end
end
end
mw.logObject(ingredients)
--return parser.formattedItem(products, ingredients, item)
elseif craftTable ~= nil then
for num = 1, #tableList[craftTable] do
recipes[num] = recipeList[tableList[craftTable][num]]
end
return parser.formattedTable(recipes, craftTable)
else
return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
end
end
return p