Module:GetRecipes: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
No edit summary
No edit summary
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
-- Grabs args from the parent frame
-- Trims and parses the args into a table, then returns the table
-- Trims and parses the args into a table, then returns the table
function norm( origArgs )
function norm()
     origArgs = mw.getCurrentFrame():getParent().args
     local origArgs = mw.getCurrentFrame():getParent().args
     local args = {}
     local args = {}
      
      
Line 19: Line 20:




-- Compares two recipes ( a < b ? true : false )
-- Main entry point for the Module
function recipeCompare(a, b)
function p.main()
    -- get args from the Template
    local args = norm()
   
    -- 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
      
      
     --not in v0.9
     -- 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.
    --if not a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
     --local groupList = completeList.groups
    --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
     -- initialize a table to store recipes
if a.skillNeeds[1][1] < b.skillNeeds[1][1] then return true
     local recipes = {}
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
 
 
-- Parses arguments into a table of recipes
function frameParse( args )
     local parsedRecipes = {}
 
    -- 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
 
 
-- Formats and sorts recipes
function formatRecipes( recipes )
  -- Sort recipes
  table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )
 
  return recipes
end
 
 
-- Seperates a list of recipes into two tables
function seperate( recipes, item )
    local sepRecipes = { {}, {} }
    local k = 1
    for i = 1, #recipes do
        --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
    return sepRecipes
end
 
 
-- Called from Template:RecipeTable
-- Calls Module:RecipeTable
-- Returns the wikitext returned by Module:RecipeTable
function p.raw( f )
    -- 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
    -- load CraftingParser
        returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n "
    local parser = require( "Module:CraftingParser" )
    else
        returnVal = returnVal .. recipeTable.main( formatRecipes( products ) ) .. '\n\n=== Used in Recipes ===\n\n'
    -- priority: item > table > group
    end
     if item ~= nil then
    if #ingredients == 0 then
      local products = productsList[item]
        returnVal = returnVal .. "''None''\n"
  local ingredients = ingredientsList[item]
    else
       
        returnVal = returnVal .. recipeTable.main( formatRecipes( ingredients ) )
      return parser.formattedItem( products, ingredients, item )
    end
       
 
    elseif table ~= nil then
    return returnVal
--GROUP deprecated
end
        --if group ~= nil then
 
            --local count = 1
 
            --for num = 1, #tableList[table] do
-- Called from Module:GetRecipes
                --for gnum = 1, #groupList[group] do
-- Calls Module:RecipeTable
                    --if tableList[table][num] == groupList[group][gnum] then
-- Returns formatted wikitext, including wikitext returned from Module:RecipeTable
                        --recipes[count] = recipeList[tableList[table][num]]
function p.formattedTable( recipes, table )
                        --count = count + 1
     -- 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
        --else
         returnVal = returnVal .. "''None''\n"
            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
     else
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
         return 'Invalid use of parameters, please see [[Template:GetRecipes]].'
     end
     end
   
    return returnVal
end
end


-- Called from Module:GetRecipes
-- 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
   
    return returnVal
end


return p
return p

Revision as of 04:33, 31 January 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 = {}


-- Grabs args from the parent frame
-- 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
function p.main()
    -- get args from the Template
    local args = norm()
    
    -- 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
    
    -- 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" )
	
    -- priority: item > table > group
    if item ~= nil then
       local products = productsList[item]
	   local ingredients = ingredientsList[item]
        
       return parser.formattedItem( products, ingredients, 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


return p