Modul:CraftingParser: Unterschied zwischen den Versionen

Aus Eco - Deutsches Wiki
Wechseln zu:Navigation, Suche
[unmarkierte Version][unmarkierte Version]
K 1 Version importiert
Keine Bearbeitungszusammenfassung
Zeile 20: Zeile 20:
-- Compares two recipes ( a < b ? true : false )
-- Compares two recipes ( a < b ? true : false )
function recipeCompare(a, b)
function recipeCompare(a, b)
     if a.affSkill1[1] < b.affSkill1[1] then return true
      
     elseif a.affSkill1[1] > b.affSkill1[1] then return false
    --not in v0.9
     elseif a.skillNeed[1] < b.skillNeed[1] then return true
    --if not a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
    elseif a.skillNeed[1] > b.skillNeed[1] then return false
    --elseif a.efficiencySkills[1] and not b.efficiencySkills[1] then return false
    elseif not a.skillNeed[3] and b.skillNeed[3] then return true
    --elseif not a.efficiencySkills[1] and b.efficiencySkills[1] then return true
    elseif a.skillNeed[3] and not b.skillNeed[3] then return false
    --elseif a.efficiencySkills[1] < b.efficiencySkills[1] then return true
    elseif a.skillNeed[3] and a.skillNeed[3] < b.skillNeed[3] then return true
     --elseif a.efficiencySkills[1] > b.efficiencySkills[1] then return false
    elseif a.skillNeed[3] and a.skillNeed[3] > b.skillNeed[3] then return false
      
     elseif a.item1[1] < b.item1[1] then return true
if (a.skillNeeds[1] ~= nil and b.skillNeeds[1] ~= nil) then
     elseif a.item1[1] > b.item1[1] then return false
if a.skillNeeds[1][1] < b.skillNeeds[1][1] then return true
     else return a.mater1[1] <= b.mater1[1]
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
end
Zeile 56: Zeile 65:
             ['mater3'] = {args['material'..i..'.3'], args['materialImg'..i..'.3'], args['materialNum'..i..'.3']},
             ['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']},
             ['mater4'] = {args['material'..i..'.4'], args['materialImg'..i..'.4'], args['materialNum'..i..'.4']},
             ['ctime'] = args['craftTime'..i],
             ['cTime'] = args['craftTime'..i],
             ['affSkill1'] = {args['affectSkill'..i..'.1'], args['affectSkillImg'..i..'.1']},
             ['affSkill1'] = {args['affectSkill'..i..'.1'], args['affectSkillImg'..i..'.1']},
             ['affSkill2'] = {args['affectSkill'..i..'.2'], args['affectSkillImg'..i..'.2']},
             ['affSkill2'] = {args['affectSkill'..i..'.2'], args['affectSkillImg'..i..'.2']},
            --TODO - construct new parts of the table here
         })
         })
     end
     end
Zeile 68: Zeile 78:
-- Formats and sorts recipes
-- Formats and sorts recipes
function formatRecipes( recipes )
function formatRecipes( recipes )
    -- Iterate over every recipe
  -- Sort recipes
    for k, v in ipairs( recipes ) do
  table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )
        if recipes[k].item1[3] == '1' then recipes[k].item1[3] = nil end
        if recipes[k].item2[3] == '1' then recipes[k].item2[3] = nil end
        if recipes[k].mater1[3] == '1' then recipes[k].mater1[3] = nil end
        if recipes[k].mater2[3] == '1' then recipes[k].mater2[3] = nil end
        if recipes[k].mater3[3] == '1' then recipes[k].mater3[3] = nil end
        if recipes[k].mater4[3] == '1' then recipes[k].mater4[3] = nil end
        if not recipes[k].skillNeed[1] then
            recipes[k].skillNeed[1] = ''
            recipes[k].skillNeed[3] = ''
        end
    end
 
    -- Sort recipes
    table.sort( recipes, function( a, b ) return recipeCompare( a, b ) end )


    return recipes
  return recipes
end
end


Zeile 92: Zeile 88:
function seperate( recipes, item )
function seperate( recipes, item )
     local sepRecipes = { {}, {} }
     local sepRecipes = { {}, {} }
    local k = 1
     for i = 1, #recipes do
     for i = 1, #recipes do
         if recipes[i].item1[1] == item then
         --v0.8 if recipes[i].products[1][1] == item then
            table.insert( sepRecipes[1], recipes[i] )
        --v0.9 need to find product in recipes[i].variants object, using pairs
        elseif recipes[i].item2[1] ~= nil and recipes[i].item2[1] == item then
 
            table.insert( sepRecipes[1], recipes[i] )
      --cycle variants ingredients to build 'used in' list
        elseif recipes[i].mater1[1] == item then
        if recipes[i] ~= nil then
            table.insert( sepRecipes[2], recipes[i] )
        if recipes[i].variants ~= nil then
        elseif recipes[i].mater2[1] ~= nil and recipes[i].mater2[1] == item then
        for k, v in pairs(recipes[i].variants) do
            table.insert( sepRecipes[2], recipes[i] )
        if v.products[1][1] == item then
        elseif recipes[i].mater3[1] ~= nil and recipes[i].mater3[1] == item then
        table.insert( sepRecipes[1], recipes[i] )
            table.insert( sepRecipes[2], recipes[i] )
        elseif v.products[2] ~= nil and v.products[2][1] == item then
        elseif recipes[i].mater4[1] ~= nil and recipes[i].mater4[1] == item then
        table.insert( sepRecipes[1], recipes[i] )
            table.insert( sepRecipes[2], 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
Zeile 131: Zeile 148:
-- Returns the wikitext returned by Module:RecipeTable
-- Returns the wikitext returned by Module:RecipeTable
function p.formattedItem( recipes, item )
function p.formattedItem( recipes, item )
   
     -- Seperate recipes into crafting recipes and used in recipes
     -- Seperate recipes into crafting recipes and used in recipes
     local sepRecipes = seperate( recipes, item )
     local sepRecipes = seperate( recipes, item )
Zeile 137: Zeile 155:
     local recipeTable = require( "Module:RecipeTable" )
     local recipeTable = require( "Module:RecipeTable" )
     local returnVal = '=== Crafting Recipes ===\n\n'
     local returnVal = '=== Crafting Recipes ===\n\n'
   
     if #sepRecipes[1] == 0 then
     if #sepRecipes[1] == 0 then
         returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n"
         returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n "
     else
     else
         returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[1] ) ) .. '\n\n=== Used in Recipes ===\n\n'
         returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[1] ) ) .. '\n\n=== Used in Recipes ===\n\n'

Version vom 15. Oktober 2020, 03:37 Uhr

This module contains functions for Modul:GetRecipes. It uses the following Modules:


local p = {}

-- 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


-- 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( 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
    local recipeTable = require( "Module:RecipeTable" )
    local returnVal = '=== Crafting Recipes ===\n\n'
    
    if #sepRecipes[1] == 0 then
        returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n "
    else
        returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[1] ) ) .. '\n\n=== Used in Recipes ===\n\n'
    end
    if #sepRecipes[2] == 0 then
        returnVal = returnVal .. "''None''\n"
    else
        returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[2] ) )
    end

    return returnVal
end


-- Called from Module:GetRecipes
-- Calls Module:RecipeTable
-- Returns formatted wikitext, including wikitext returned from Module:RecipeTable
function p.formattedTable( recipes, table )
    -- 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
    
    return returnVal
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