Editing Module:GetRecipes

From Eco - English Wiki

Your changes will be displayed to readers once an authorized user accepts them. (help)

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
-- Module:GetRecipes (https://wiki.play.eco/en/Module:GetRecipes)
-- Module:CraftingParser (https://wiki.play.eco/en/Module:CraftingParser)
local p = {}
local p = {}


local Utils = require('Module:Utils')
-- Grabs args from the parent frame
-- Trims and parses the args into a table, then returns the table
function norm( origArgs )
    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
 
 
-- Compares two recipes ( a < b ? true : false )
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


-- Main entry point for the Module
function p.main(frame)
  local test = ''


  -- get args from the Template
-- Parses arguments into a table of recipes
  local args = Utils.normaliseArgs(frame)
function frameParse( args )
    local parsedRecipes = {}


  return p.renderTable(args.item, args.tag, args.table, args.group)  
    -- Set variables
    local dispCraftStn = tonumber( args.displayCraftStation )
    local checkImage = tonumber( args.checkForImage )
 
    -- Iterate over every recipe passed in
    for i = 1, args.numRecipes do
        table.insert( parsedRecipes, i, {
            ['dispCraftStn'] = dispCraftStn,
            ['checkImage'] = checkImage,
            ['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
 
    return parsedRecipes
end
end




function p.renderTable(item, tag, craftTable, group)
-- Formats and sorts recipes
  -- load list of recipes
function formatRecipes( recipes )
  local completeList = require("Module:CraftingRecipes")
   -- Sort recipes
  local recipeList = completeList.recipes
   table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )
  local ingredientsList = completeList.ingredients
 
  local productsList = completeList.products
   return recipes
  local tableList = completeList.tables
end
 
   -- 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
-- Seperates a list of recipes into two tables
     if productsList[item] and #productsList[item] == 1 and productsList[item][1] == '' then
function seperate( recipes, item )
      itemProducts = {item}
    local sepRecipes = { {}, {} }
    elseif productsList[item] then
     local k = 1
      itemProducts = productsList[item]
    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


    if itemProducts ~= nil then
      --cycle variants ingredients to build 'used in' list
      for _, itemProduct in pairs(itemProducts) do
        if recipes[i] ~= nil then
        -- HACK: Skill books don't show us as a product of research pages, only the skill name.
        if recipes[i].variants ~= nil then
        if string.sub(item, string.len(item) - 9) == 'Skill Book' then
        for k, v in pairs(recipes[i].variants) do
            if (itemProduct ~= '' and recipeList[itemProduct .. ' Skill Book'] ~= nil) then
        if v.products[1][1] == item then
                table.insert(products, recipeList[itemProduct .. ' Skill Book'])
        table.insert( sepRecipes[1], recipes[i] )
            end
        elseif v.products[2] ~= nil and v.products[2][1] == item then
        elseif (itemProduct ~= '' and recipeList[itemProduct] ~= nil) then
        table.insert( sepRecipes[1], recipes[i] )
            table.insert(products, recipeList[itemProduct])
        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
end


    local itemIngredients


     -- HACK Attempt to handle issue with tool recipes
-- Called from Template:RecipeTable
     if ingredientsList[item] and #ingredientsList[item] == 1 and ingredientsList[item][1] == '' then
-- Calls Module:RecipeTable
      itemIngredients = {item}
-- Returns the wikitext returned by Module:RecipeTable
     elseif ingredientsList[item] then
function p.raw( f )
      itemIngredients = ingredientsList[item]
     -- get args from the Template, parse them into recipes
     local recipes = frameParse( norm( f ) )
 
    -- Format the recipes
    recipes = formatRecipes( recipes )
 
    -- Call RecipeTable
    local recipeTable = require( "Module:RecipeTable" )
    return recipeTable.main( recipes )
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


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


    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
-- Called from Module:GetRecipes
      table.insert(recipes, recipeList[tbl])
-- Calls Module:RecipeTable
-- 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 parser.formattedTable(recipes, craftTable)
     return returnVal
   
  else
    return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
  end
end
end


return p
return p
Please note that all contributions to Eco - English Wiki are considered to be released under the CC BY-NC-SA 4.0 (see Eco:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)

Template used on this page: