Editing Module:CraftingParser

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:CraftingParser (https://wiki.play.eco/en/Module:CraftingParser)
local p = {}
local p = {}


local Utils = require('Module:Utils')
-- Grabs args from the parent frame
local L = require('Module:Localization')
-- 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 )
-- Compares two recipes ( a < b ? true : false )
function recipeCompare(a, b)
function recipeCompare(a, b)
 
   
     --not in v0.9
     --not in v0.9
     --if not a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
     --if not a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
Line 15: Line 28:
     --elseif a.efficiencySkills[1] > b.efficiencySkills[1] then return false
     --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] ~= nil and b.skillNeeds[1] ~= nil) then  
        if a.skillNeeds[1][1] < b.skillNeeds[1][1] then  
if a.skillNeeds[1][1] < b.skillNeeds[1][1] then return true
            return true
elseif a.skillNeeds[1][1] > b.skillNeeds[1][1] then return false
        elseif a.skillNeeds[1][1] > b.skillNeeds[1][1] then  
elseif not a.skillNeeds[1][2] and b.skillNeeds[1][2] then return true
            return false
elseif a.skillNeeds[1][2] and not b.skillNeeds[1][2] then return false
        elseif not a.skillNeeds[1][2] and b.skillNeeds[1][2] then
elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] < b.skillNeeds[1][2] then return true
            return true
elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] > b.skillNeeds[1][2] then return false
        elseif a.skillNeeds[1][2] and not b.skillNeeds[1][2] then
end
            return false
        elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] < b.skillNeeds[1][2] then
     elseif a.defaultVariant[1] < b.defaultVariant[1] then return true
            return true
     elseif a.defaultVariant[1] > b.defaultVariant[1] then return false
        elseif a.skillNeeds[1][2] and a.skillNeeds[1][2] > b.skillNeeds[1][2] then
     else return a.defaultVariant[1] <= b.defaultVariant[1]
            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
end
Line 74: Line 79:
function formatRecipes( recipes )
function formatRecipes( recipes )
   -- Sort recipes
   -- Sort recipes
   table.sort( recipes, recipeCompare )
   table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )


   return recipes
   return recipes
end
end
-- Seperates a list of recipes into two tables
function seperate( recipes, item )
    local sepRecipes = { {}, {} }
    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].defaultVariants == item then
        table.insert(sepRecipes[1], recipes[i] )
        for k, v in pairs(recipes[i].variants) do
        --if v.products[1][1] == item then
          --  table.insert( sepRecipes[1], recipes[i] )
        --if v.products[2] ~= nil and v.products[2][2] == item then
            --table.insert( sepRecipes[1], recipes[i] )
        --TODO REWRITE this section to cycle through ingredients properly - not hard check to prevent errors.
        if v.ingredients[1][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
        elseif v.ingredients[2][1] ~= nil and v.ingredients[2][2] == item then
            table.insert( sepRecipes[2], recipes[i] )
        --TODO REWRITE this section to cycle through k, v properly - not hard check to prevent errors.
        --elseif v.ingredients[3][1] ~= nil and v.ingredients[3][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
        --elseif v.ingredients[4][1] ~= nil and v.ingredients[4][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
--elseif v.ingredients[5][1] ~= nil and v.ingredients[5][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
        --elseif v.ingredients[6][1] ~= nil and v.ingredients[6][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
        --elseif v.ingredients[7][1] ~= nil and v.ingredients[7][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
--elseif v.ingredients[8][1] ~= nil and v.ingredients[8][1] == item then
            --table.insert( sepRecipes[2], recipes[i] )
            end
        end
        end
    end
    return sepRecipes
end


-- Called from Template:RecipeTable
-- Called from Template:RecipeTable
Line 84: Line 131:
function p.raw( f )
function p.raw( f )
     -- get args from the Template, parse them into recipes
     -- get args from the Template, parse them into recipes
     local recipes = frameParse( Utils.normaliseArgs(f) )
     local recipes = frameParse( norm( f ) )


     -- Format the recipes
     -- Format the recipes
Line 90: Line 137:


     -- Call RecipeTable
     -- Call RecipeTable
     local recipeTable = require( "Module:RecipeTable" )
     local recipeTable = require( "Module:TestRecipeTable" )
     return recipeTable.main( recipes )
     return recipeTable.main( recipes )
end
end
Line 96: Line 143:


-- Called from Module:GetRecipes
-- Called from Module:GetRecipes
-- Calls Module:RecipeTable
-- Calls Module:RecipTable
-- Returns the wikitext returned by Module:RecipeTable
-- Returns the wikitext returned by Module:RecipeTable
function p.formattedItem( products, ingredients, item )
function p.formattedItem( recipes, item )
   
    -- Seperate recipes into crafting recipes and used in recipes
    local sepRecipes = seperate( recipes, item )
 
     -- Format the recipes, and call RecipeTable on each table
     -- Format the recipes, and call RecipeTable on each table
     local recipeTable = require( "Module:RecipeTable" )
     local recipeTable = require( "Module:TestRecipeTable" )
     local returnVal = '=== '..L.t('Crafting Recipes')..' ===\n\n'
     local returnVal = '=== Crafting Recipes ===\n\n'
      
      
     if (products ~= nil and #products ~= 0) then
     if #sepRecipes[1] == 0 then
         returnVal = returnVal .. recipeTable.main( formatRecipes(products) ) .. '\n\n=== '..L.t('Used in Recipes')..' ===\n\n'       
         returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n "
     else
     else
         returnVal = returnVal .. "''"..L.t('None').."''\n\n=== "..L.t('Used in Recipes').." ===\n\n "
         returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[1] ) ) .. '\n\n=== Used in Recipes ===\n\n'
     end
     end
   
     if #sepRecipes[2] == 0 then
     if (ingredients ~= nil and #ingredients ~= 0) then
         returnVal = returnVal .. "''None''\n"
         returnVal = returnVal .. recipeTable.main( formatRecipes(ingredients) )
     else
     else
         returnVal = returnVal .. "''"..L.t('None').."''\n"     
         returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[2] ) )
     end
     end


Line 124: Line 174:
function p.formattedTable( recipes, table )
function p.formattedTable( recipes, table )
     -- Format the recipes, and call RecipeTable
     -- Format the recipes, and call RecipeTable
     local recipeTable = require( "Module:RecipeTable" )
     local recipeTable = require( "Module:TestRecipeTable" )
     local returnVal = '=== ' .. L.t('Recipes at %s'):format(table) .. ' ===\n\n'
     local returnVal = '=== Recipes at ' .. table .. ' ===\n\n'
     if #recipes == 0 then
     if #recipes == 0 then
         returnVal = returnVal .. "''"..L.t('None').."''\n"
         returnVal = returnVal .. "''None''\n"
     else
     else
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
Line 141: Line 191:
function p.formattedGroup( recipes, group )
function p.formattedGroup( recipes, group )
     -- Format the recipes, and call RecipeTable
     -- Format the recipes, and call RecipeTable
     local recipeTable = require( "Module:RecipeTable" )
     local recipeTable = require( "Module:TestRecipeTable" )
     local returnVal = '=== ' .. L.t('%s Recipes'):format(group) .. ' ===\n\n'
     local returnVal = '=== ' .. group .. ' Recipes ===\n\n'
     if #recipes == 0 then
     if #recipes == 0 then
         returnVal = returnVal .. "''"..L.t('None').."''\n"
         returnVal = returnVal .. "''None''\n"
     else
     else
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
         returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) )
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: