ATTENTION! The process of updating WiKi to version Eco 10.x has begun. Those wishing to participate can find out more Information on our ECO Contribution Wiki Discord.
From April 26 to May 12, errors may occur in the Wiki, as we will be carrying out a major update to the information processing modules.

Module:GetRecipes: Difference between revisions

From Eco - English Wiki
[checked revision][checked revision]
(HACK: Until Skill Books/Research Pages work correctly)
No edit summary
 
(17 intermediate revisions by 2 users not shown)
Line 10: Line 10:
   -- get args from the Template
   -- get args from the Template
   local args = Utils.normaliseArgs(frame)
   local args = Utils.normaliseArgs(frame)
 
 
   -- assign variables for item, table, and group
   return p.renderTable(args.item, args.tag, args.table, args.group)
  local item = args.item
end
  local table = args.table
 
  local group = args.group
 
 
function p.renderTable(item, tag, craftTable, group)
   -- load list of recipes
   -- load list of recipes
   local completeList = require("Module:CraftingRecipes")
   local completeList = require("Module:CraftingRecipes")
Line 31: Line 31:
   local parser = require("Module:CraftingParser")
   local parser = require("Module:CraftingParser")


   -- priority: item > table > group
   -- priority: item > tag > table > group
   if item ~= nil then
   if item ~= nil then
     if productsList[item] ~= nil then
    local itemProducts
       local built = 1
 
       for _, itemProduct in ipairs(productsList[item]) do
    -- HACK Attempt to handle issue with tool recipes
         if (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then
     if productsList[item] and #productsList[item] == 1 and productsList[item][1] == '' then
          products[built] = recipeList[itemProduct]
      itemProducts = {item}
          built = built + 1
    elseif productsList[item] then
       itemProducts = productsList[item]
    end
 
    if itemProducts ~= nil then
       for _, itemProduct in pairs(itemProducts) do
        -- HACK: Skill books don't show us as a product of research pages, only the skill name.
         if string.sub(item, string.len(item) - 9) == 'Skill Book' then
            if (itemProduct ~= '' and recipeList[itemProduct .. ' Skill Book'] ~= nil) then
                table.insert(products, recipeList[itemProduct .. ' Skill Book'])
            end
        elseif (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then
            table.insert(products, recipeList[itemProduct])
         end
         end
       end
       end
     end
     end


     if ingredientsList[item] ~= nil then
    local itemIngredients
       local built = 1
 
       for _, itemIngredient in ipairs(ingredientsList[item]) do
    -- HACK Attempt to handle issue with tool recipes
     if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then
      itemIngredients = {item}
    elseif ingredientsList[item] then
       itemIngredients = ingredientsList[item]
    end
 
    if itemIngredients ~= nil then
       for _, itemIngredient in pairs(itemIngredients) do
         if itemIngredient ~= '' then
         if itemIngredient ~= '' then
        if recipeList[itemIngredient] ~= nil then
          if recipeList[itemIngredient] ~= nil then
          ingredients[built] = recipeList[itemIngredient]
            table.insert(ingredients, recipeList[itemIngredient])
          built = built + 1
          -- 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
            table.insert(ingredients, recipeList[itemIngredient .. ' Skill Book'])
        ingredients[built] = recipeList[itemIngredient .. ' Skill Book']
          end
          built = built + 1
        end
         end
         end
       end
       end
     end
     end
    return parser.formattedItem(products, ingredients, item)


     return parser.formattedItem(products, ingredients, item)
  elseif tag ~= nil 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] == tag then
            foundIngredient = true
          end
        end
      end
      if foundIngredient then
        table.insert(itemIngredients, recipeName)
      end
    end
 
    if itemIngredients ~= nil then
      for _, itemIngredient in pairs(itemIngredients) do
        if itemIngredient ~= '' then
          if recipeList[itemIngredient] ~= nil then
    table.insert(ingredients, recipeList[itemIngredient])
          end
        end
      end
    end
     return parser.formattedItem(products, ingredients, tag)
      
      
   elseif table ~= nil then
   elseif craftTable ~= nil then
     for num = 1, #tableList[table] do
 
       recipes[num] = recipeList[tableList[table][num]]
    if tableList[craftTable] == nil then
        return 'This object is not a crafting table and cannot craft any objects.'
    end
 
     for _, tbl in pairs(tableList[craftTable]) do
       table.insert(recipes, recipeList[tbl])
     end
     end
      
      
     return parser.formattedTable(recipes, table)
     return parser.formattedTable(recipes, craftTable)
      
      
   else
   else

Latest revision as of 15:43, 1 January 2024

Documentation[edit source]

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:CraftingRecipes 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)

  return p.renderTable(args.item, args.tag, args.table, args.group) 
end


function p.renderTable(item, tag, craftTable, 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 > tag > 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
      for _, itemProduct in pairs(itemProducts) do
         -- HACK: Skill books don't show us as a product of research pages, only the skill name.
        if string.sub(item, string.len(item) - 9) == 'Skill Book' then
            if (itemProduct ~= '' and recipeList[itemProduct .. ' Skill Book'] ~= nil) then
                table.insert(products, recipeList[itemProduct .. ' Skill Book'])
            end
        elseif (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then
            table.insert(products, recipeList[itemProduct])
        end
      end
    end

    local itemIngredients

    -- HACK Attempt to handle issue with tool recipes
    if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then
      itemIngredients = {item}
    elseif ingredientsList[item] then
      itemIngredients = ingredientsList[item]
    end

    if itemIngredients ~= nil then
      for _, itemIngredient in pairs(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
    return parser.formattedItem(products, ingredients, item)

  elseif tag ~= nil 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] == tag then
             foundIngredient = true
           end
        end
      end
      if foundIngredient then
        table.insert(itemIngredients, recipeName)
      end
    end

    if itemIngredients ~= nil then
      for _, itemIngredient in pairs(itemIngredients) do
        if itemIngredient ~= '' then
          if recipeList[itemIngredient] ~= nil then
	    table.insert(ingredients, recipeList[itemIngredient])
          end
        end
      end
    end
    return parser.formattedItem(products, ingredients, tag)
    
  elseif craftTable ~= nil then

    if tableList[craftTable] == nil then
        return 'This object is not a crafting table and cannot craft any objects.'
    end

    for _, tbl in pairs(tableList[craftTable]) do
      table.insert(recipes, recipeList[tbl])
    end
    
    return parser.formattedTable(recipes, craftTable)
    
  else
    return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
  end
end

return p