Module:GetRecipes: Difference between revisions

From Eco - English Wiki
[unchecked revision][checked revision]
No edit summary
No edit summary
 
(29 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- Module:CraftingParser (https://wiki.play.eco/en/Module:CraftingParser)
-- Module:GetRecipes (https://wiki.play.eco/en/Module:GetRecipes)
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( origArgs )
-- Main entry point for the Module
    origArgs = mw.getCurrentFrame():getParent().args
function p.main(frame)
    local args = {}
  local test = ''
   
    for k, v in pairs( origArgs ) do
        v = mw.text.trim( tostring( v ) )
        if v ~= '' then
            args[k] = v
        end
    end
   
    return args
end


  -- get args from the Template
  local args = Utils.normaliseArgs(frame)


-- Compares two recipes ( a < b ? true : false )
  return p.renderTable(args.item, args.tag, args.table, args.group)  
function recipeCompare(a, b)
   
    --not in v0.9
    --if not a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
    --elseif a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
    --elseif not a.efficiencySkills[1] and b.efficiencySkills[1] then return true
    --elseif a.efficiencySkills[1] < b.efficiencySkills[1] then return true
    --elseif a.efficiencySkills[1] > b.efficiencySkills[1] then return false
   
if (a.skillNeeds[1] ~= nil and b.skillNeeds[1] ~= nil) then
if a.skillNeeds[1][1] < b.skillNeeds[1][1] then return true
elseif a.skillNeeds[1][1] > b.skillNeeds[1][1] then return false
elseif not a.skillNeeds[1][2] and b.skillNeeds[1][2] then return true
elseif a.skillNeeds[1][2] and not b.skillNeeds[1][2] then return false
elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] < b.skillNeeds[1][2] then return true
elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] > b.skillNeeds[1][2] then return false
end
    elseif a.defaultVariant < b.defaultVariant then return true
    elseif a.defaultVariant > b.defaultVariant then return false
    else return a.defaultVariant <= b.defaultVariant
    end
end
end




-- Parses arguments into a table of recipes
function p.renderTable(item, tag, craftTable, group)
function frameParse( args )
  -- load list of recipes
    local parsedRecipes = {}
  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")


    -- Set variables
  -- priority: item > tag > table > group
    local dispCraftStn = tonumber( args.displayCraftStation )
  if item ~= nil then
     local checkImage = tonumber( args.checkForImage )
     local itemProducts


     -- Iterate over every recipe passed in
     -- HACK Attempt to handle issue with tool recipes
     for i = 1, args.numRecipes do
     if productsList[item] and #productsList[item] == 1 and productsList[item][1] == '' then
        table.insert( parsedRecipes, i, {
      itemProducts = {item}
            ['dispCraftStn'] = dispCraftStn,
    elseif productsList[item] then
            ['checkImage'] = checkImage,
      itemProducts = productsList[item]
            ['craftStn'] = {args['craftStation'..i], args['craftStationImg'..i]},
            ['item1'] = {args['item'..i..'.1'], args['itemImg'..i..'.1'], args['itemNum'..i..'.1']},
            ['item2'] = {args['item'..i..'.2'], args['itemImg'..i..'.2'], args['itemNum'..i..'.2']},
            ['skillNeed'] = {args['skillNeed'..i], args['skillNeedImg'..i], args['skillLvlNeed'..i]},
            ['mater1'] = {args['material'..i..'.1'], args['materialImg'..i..'.1'], args['materialNum'..i..'.1']},
            ['mater2'] = {args['material'..i..'.2'], args['materialImg'..i..'.2'], args['materialNum'..i..'.2']},
            ['mater3'] = {args['material'..i..'.3'], args['materialImg'..i..'.3'], args['materialNum'..i..'.3']},
            ['mater4'] = {args['material'..i..'.4'], args['materialImg'..i..'.4'], args['materialNum'..i..'.4']},
            ['cTime'] = args['craftTime'..i],
            ['affSkill1'] = {args['affectSkill'..i..'.1'], args['affectSkillImg'..i..'.1']},
            ['affSkill2'] = {args['affectSkill'..i..'.2'], args['affectSkillImg'..i..'.2']},
            --TODO - construct new parts of the table here
        })
     end
     end


     return parsedRecipes
     if itemProducts ~= nil then
end
      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


-- Formats and sorts recipes
    -- HACK Attempt to handle issue with tool recipes
function formatRecipes( recipes )
    if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then
  -- Sort recipes
      itemIngredients = {item}
  table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )
    elseif ingredientsList[item] then
      itemIngredients = ingredientsList[item]
    end


  return recipes
     if itemIngredients ~= nil then
end
      for _, itemIngredient in pairs(itemIngredients) do
 
        if itemIngredient ~= '' then
 
          if recipeList[itemIngredient] ~= nil then
-- Seperates a list of recipes into two tables
            table.insert(ingredients, recipeList[itemIngredient])
function seperate( recipes, item )
          -- HACK: Skill books don't show us as a product of research pages, only the skill name.
    local sepRecipes = { {}, {} }
          elseif recipeList[itemIngredient .. ' Skill Book'] ~= nil then
     local k = 1
            table.insert(ingredients, recipeList[itemIngredient .. ' Skill Book'])
    for i = 1, #recipes do
          end
        --v0.8 if recipes[i].products[1][1] == item then
        --v0.9 need to find product in recipes[i].variants object, using pairs
 
      --cycle variants ingredients to build 'used in' list
        if recipes[i] ~= nil then
        if recipes[i].variants ~= nil then
        for k, v in pairs(recipes[i].variants) do
        if v.products[1][1] == item then
        table.insert( sepRecipes[1], recipes[i] )
        elseif v.products[2] ~= nil and v.products[2][1] == item then
        table.insert( sepRecipes[1], recipes[i] )
        elseif v.products[3] ~= nil and v.products[3][1] == item then
        table.insert( sepRecipes[1], recipes[i] )
elseif v.ingredients[1][2] == item then
        table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[2] ~= nil and v.ingredients[2][2] == item then
table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[3] ~= nil and v.ingredients[3][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[4] ~= nil and v.ingredients[4][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
elseif v.ingredients[5] ~= nil and v.ingredients[5][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[6] ~= nil and v.ingredients[6][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[7] ~= nil and v.ingredients[7][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
elseif v.ingredients[8] ~= nil and v.ingredients[8][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
            end
        end
        end
         end
         end
      end
     end
     end
     return sepRecipes
     return parser.formattedItem(products, ingredients, item)
end


 
  elseif tag ~= nil then
-- Called from Template:RecipeTable
     itemIngredients = {}   
-- Calls Module:RecipeTable
     
-- Returns the wikitext returned by Module:RecipeTable
     -- Tags do not appear in the 'products' list, so have to process manually
function p.raw( f )
     for recipeName, recipe in pairs(recipeList) do
     -- get args from the Template, parse them into recipes
      local foundIngredient = false
    local recipes = frameParse( norm( f ) )
      for _, variant in pairs(recipe.variants) do
 
        for _, ingredient in pairs(variant.ingredients) do
     -- Format the recipes
          if ingredient[1] == 'TAG' and ingredient[2] == tag then
     recipes = formatRecipes( recipes )
            foundIngredient = true
 
          end
    -- Call RecipeTable
         end
    local recipeTable = require( "Module:RecipeTable" )
      end
    return recipeTable.main( recipes )
      if foundIngredient then
end
         table.insert(itemIngredients, recipeName)
 
      end
 
-- Called from Module:GetRecipes
-- Calls Module:RecipTable
-- Returns the wikitext returned by Module:RecipeTable
function p.formattedItem( products, ingredients, item )
 
    -- Format the recipes, and call RecipeTable on each table
    local recipeTable = require( "Module:RecipeTable" )
    local returnVal = '=== Crafting Recipes ===\n\n'
   
    if #products == 0 then
        returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n "
    else
         returnVal = returnVal .. recipeTable.main( formatRecipes( products ) ) .. '\n\n=== Used in Recipes ===\n\n'
    end
    if #ingredients == 0 then
         returnVal = returnVal .. "''None''\n"
    else
        returnVal = returnVal .. recipeTable.main( formatRecipes( ingredients ) )
     end
     end


     return returnVal
     if itemIngredients ~= nil then
end
      for _, itemIngredient in pairs(itemIngredients) do
 
        if itemIngredient ~= '' then
 
          if recipeList[itemIngredient] ~= nil then
-- Called from Module:GetRecipes
    table.insert(ingredients, recipeList[itemIngredient])
-- Calls Module:RecipeTable
          end
-- Returns formatted wikitext, including wikitext returned from Module:RecipeTable
         end
function p.formattedTable( recipes, table )
      end
    -- Format the recipes, and call RecipeTable
    local recipeTable = require( "Module:RecipeTable" )
    local returnVal = '=== Recipes at ' .. table .. ' ===\n\n'
    if #recipes == 0 then
        returnVal = returnVal .. "''None''\n"
    else
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
     end
     end
    return parser.formattedItem(products, ingredients, tag)
      
      
    return returnVal
  elseif craftTable ~= nil then
end


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


-- Called from Module:GetRecipes
     for _, tbl in pairs(tableList[craftTable]) do
-- Calls Module:RecipeTable
      table.insert(recipes, recipeList[tbl])
-- Returns formatted wikitext, including wikitext returned from Module:RecipeTable
function p.formattedGroup( recipes, group )
     -- Format the recipes, and call RecipeTable
    local recipeTable = require( "Module:RecipeTable" )
    local returnVal = '=== ' .. group .. ' Recipes ===\n\n'
    if #recipes == 0 then
        returnVal = returnVal .. "''None''\n"
    else
        returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
     end
     end
      
      
     return returnVal
     return parser.formattedTable(recipes, craftTable)
   
  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