Module:GetRecipes: Difference between revisions

From Eco - English Wiki
[checked revision][unchecked revision]
No edit summary
m Use util function, fix formatting
Line 2: Line 2:
local p = {}
local p = {}


-- Grabs args from the parent frame
local Utils = require('Module:Utils')
-- Trims and parses the args into a table, then returns the table
function norm()
    local origArgs = mw.getCurrentFrame():getParent().args
    local args = {}
   
    for k, v in pairs( origArgs ) do
        v = mw.text.trim( tostring( v ) )
        if v ~= '' then
            args[k] = v
        end
    end
   
    return args
end


-- Main entry point for the Module
-- Main entry point for the Module
function p.main()
function p.main(frame)
    local test = ''
  local test = ''
 
-- get args from the Template
  -- get args from the Template
    local args = norm()
  local args = Utils.normaliseArgs(frame)
   
 
    -- assign variables for item, table, and group
  -- assign variables for item, table, and group
    local item = args.item
  local item = args.item
    local table = args.table
  local table = args.table
    local group = args.group
  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")


    -- load list of recipes
  -- priority: item > table > group
    local completeList = require( "Module:CraftingRecipes" )
  if item ~= nil then
    local recipeList = completeList.recipes
    if productsList[item] ~= nil then
    local ingredientsList = completeList.ingredients
      local built = 1
local productsList = completeList.products
      for num = 1, #productsList[item] do
    local tableList = completeList.tables
        if (productsList[item][num] ~= '' and recipeList[productsList[item][num]] ~= nil) then
   
          products[built] = recipeList[productsList[item][num]]
    -- initialize a table to store recipes
          built = built + 1
    local recipes = {}
local products = {}
local ingredients = {}
   
    -- load CraftingParser
    local parser = require( "Module:CraftingParser" )
    -- priority: item > table > group
    if item ~= nil then
if productsList[item] ~= nil then
local built = 1
for num = 1, #productsList[item] do
if (productsList[item][num] ~= '' and recipeList[productsList[item][num]] ~= nil) then
products[built] = recipeList[productsList[item][num]]
built = built + 1
end
end
end
if ingredientsList[item] ~= nil then
local built = 1
for num = 1, #ingredientsList[item] do
if (ingredientsList[item][num] ~= '' and recipeList[ingredientsList[item][num]] ~= nil) then
ingredients[built] = recipeList[ingredientsList[item][num]]
built = built + 1
end
end
end
      return parser.formattedItem( products, ingredients, item )
       
    elseif table ~= nil then
        for num = 1, #tableList[table] do
            recipes[num] = recipeList[tableList[table][num]]
         end
         end
      end
    end


         return parser.formattedTable( recipes, table )
    if ingredientsList[item] ~= nil then
      local built = 1
      for num = 1, #ingredientsList[item] do
         if (ingredientsList[item][num] ~= '' and recipeList[ingredientsList[item][num]] ~= nil) then
          ingredients[built] = recipeList[ingredientsList[item][num]]
          built = built + 1
        end
      end
    end


     else
     return parser.formattedItem(products, ingredients, item)
        return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
   
  elseif table ~= nil then
    for num = 1, #tableList[table] do
      recipes[num] = recipeList[tableList[table][num]]
     end
     end
   
    return parser.formattedTable(recipes, table)
   
  else
    return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
  end
end
end


return p
return p

Revision as of 15:07, 21 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 table = 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
    if productsList[item] ~= nil then
      local built = 1
      for num = 1, #productsList[item] do
        if (productsList[item][num] ~= '' and recipeList[productsList[item][num]] ~= nil) then
          products[built] = recipeList[productsList[item][num]]
          built = built + 1
        end
      end
    end

    if ingredientsList[item] ~= nil then
      local built = 1
      for num = 1, #ingredientsList[item] do
        if (ingredientsList[item][num] ~= '' and recipeList[ingredientsList[item][num]] ~= nil) then
          ingredients[built] = recipeList[ingredientsList[item][num]]
          built = built + 1
        end
      end
    end

    return parser.formattedItem(products, ingredients, item)
    
  elseif table ~= nil then
    for num = 1, #tableList[table] do
      recipes[num] = recipeList[tableList[table][num]]
    end
    
    return parser.formattedTable(recipes, table)
    
  else
    return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
  end
end

return p