Module:GetRecipes: Difference between revisions

From Eco - English Wiki
[unchecked revision][checked revision]
No edit summary
No edit summary
 
(37 intermediate revisions by 4 users not shown)
Line 1: Line 1:
-- Module:GetRecipes (https://wiki.play.eco/en/Module:GetRecipes)
local p = {}
local p = {}


local Utils = require('Module:Utils')


-- Grabs args from the parent frame
-- Main entry point for the Module
-- Trims and parses the args into a table, then returns the table
function p.main(frame)
function norm()
  local test = ''
    local origArgs = mw.getCurrentFrame():getParent().args
 
     local args = {}
  -- get args from the Template
      
  local args = Utils.normaliseArgs(frame)
     for k, v in pairs( origArgs ) do
 
         v = mw.text.trim( tostring( v ) )
  return p.renderTable(args.item, args.tag, args.table, args.group)
         if v ~= '' then
end
             args[k] = v
 
 
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
     end
     end
   
     return parser.formattedItem(products, ingredients, item)
     return args
end


  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


-- Main entry point for the Module
    if itemIngredients ~= nil then
function p.main()
      for _, itemIngredient in pairs(itemIngredients) do
     -- get args from the Template
        if itemIngredient ~= '' then
     local args = norm()
          if recipeList[itemIngredient] ~= nil then
    table.insert(ingredients, recipeList[itemIngredient])
          end
        end
      end
     end
     return parser.formattedItem(products, ingredients, tag)
      
      
    -- assign variables for item, table, and group
  elseif craftTable ~= nil then
    local item = args.item
    local table = args.table
    local group = args.group


     -- load list of recipes
     if tableList[craftTable] == nil then
    local completeList = require( "Module:CraftingRecipes" )
        return 'This object is not a crafting table and cannot craft any objects.'
    local recipeList = completeList.recipes
     end
    local itemList = completeList.items
    local tableList = completeList.tables
   
    -- groups no longer exists. Need to check all dependencies on this throughout other modules. Expecting implication to be removal of 'used in' table in the wiki.
     --local groupList = completeList.groups
   
    -- initialize a table to store recipes
    local recipes = {}
   
    -- load CraftingParser
    local parser = require( "Module:CraftingParser" )


local debugString = ''
    for _, tbl in pairs(tableList[craftTable]) do
      table.insert(recipes, recipeList[tbl])
    end
      
      
    return parser.formattedTable(recipes, craftTable)
      
      
    -- priority: item > table > group
  else
    if item ~= nil then
     return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
        if itemList[item] == nil then
  end
            return '===Crafting Recipes===\n\'\'None\'\'\n===Used in Recipes (potentially deprecated due to removal of groups - clean this up)===\n\'\'None\'\'\n'
        end
        for num = 1, #itemList[item] do
            --nil entries in itemList are causing errors. Need to deal with exceptions where entry is ''
            if itemList[item][num] ~= '' then --skip blank entry in array. Applies to Bars (Iron Bars, Steel Bars, etc).
            recipes[num] = recipeList[itemList[item][num]]
            debugString = debugString .. '==' .. recipeList[itemList[item][num]]
            end
            --handle blank entries in itemList
            if #itemList[item] == 1 and itemList[item][num] == '' then
            --Handle situations where itemList has a single, blank entry. Set value to item name provided by the calling page. Occurs for all tools. Could cause other issues for invalid item names.
            recipes[num] = recipeList[item]
return '===SINGLE, EMPTY STRING FOUND ===\n\'\'None\'\'\n'
            end
        end
       
        return debugString
       
        --return parser.formattedItem( recipes, item )
       
    elseif table ~= nil then
--GROUP deprecated
        --if group ~= nil then
            --local count = 1
            --for num = 1, #tableList[table] do
                --for gnum = 1, #groupList[group] do
                    --if tableList[table][num] == groupList[group][gnum] then
                        --recipes[count] = recipeList[tableList[table][num]]
                        --count = count + 1
                    --end
                --end
            --end
        --else
            for num = 1, #tableList[table] do
                recipes[num] = recipeList[tableList[table][num]]
            end
        --end
        return parser.formattedTable( recipes, table )
    --GROUP deprecated
     --elseif group ~= nil then
        --for num = 1, #groupList[group] do
            --recipes[num] = recipeList[groupList[group][num]]
        --end
        --return parser.formattedGroup( recipes, group )
    else
        return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
    end
end
end


return p
return p

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