Editing Module:CraftingParser
From Eco - English Wiki
Your changes will be displayed to readers once an authorized user accepts them. (help)
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: | ||
local p = {} | local p = {} | ||
-- Grabs args from the parent frame | |||
local | -- 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) | ||
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 | |||
elseif (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.products[1][1] < b.products[1][1] then return true | |||
elseif a.products[1][1] > b.products[1][1] then return false | |||
else return a.ingredients[1][1] <= b.ingredients[1][1] | |||
elseif a. | |||
elseif a. | |||
end | end | ||
end | end | ||
Line 59: | Line 63: | ||
['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], | ||
['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']}, | ||
}) | }) | ||
end | end | ||
Line 73: | Line 76: | ||
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 | |||
--TODO Could use this algorithm to cycle variants. For now, focus on default variant only | |||
--v0.8 if recipes[i].products[1][1] == item then | |||
--v0.9 need to find product in recipes[i].variants[j].products | |||
for j = 1, #recipes[i].variants do | |||
if recipes[i].variants[j].products[1][1] == item then | |||
table.insert( sepRecipes[1], recipes[i] ) | |||
elseif recipes[i].variants[j].products[2] ~= nil and recipes[i].variants[j].products[2][1] == item then | |||
table.insert( sepRecipes[1], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[1][2] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[2][1] ~= nil and recipes[i].variants[j].ingredients[2][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[3][1] ~= nil and recipes[i].variants[j].ingredients[3][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[4][1] ~= nil and recipes[i].variants[j].ingredients[4][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[5][1] ~= nil and recipes[i].variants[j].ingredients[5][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[6][1] ~= nil and recipes[i].variants[j].ingredients[6][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[7][1] ~= nil and recipes[i].variants[j].ingredients[7][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
elseif recipes[i].variants[j].ingredients[8][1] ~= nil and recipes[i].variants[j].ingredients[8][1] == item then | |||
table.insert( sepRecipes[2], recipes[i] ) | |||
end | |||
end | |||
end | |||
return sepRecipes | |||
end | |||
-- Called from Template:RecipeTable | -- Called from Template:RecipeTable | ||
Line 83: | Line 122: | ||
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( | local recipes = frameParse( norm( f ) ) | ||
-- Format the recipes | -- Format the recipes | ||
Line 89: | Line 128: | ||
-- Call RecipeTable | -- Call RecipeTable | ||
local recipeTable = require( "Module: | local recipeTable = require( "Module:TestRecipeTable" ) | ||
return recipeTable.main( recipes ) | return recipeTable.main( recipes ) | ||
end | end | ||
Line 95: | Line 134: | ||
-- Called from Module:GetRecipes | -- Called from Module:GetRecipes | ||
-- Calls Module: | -- Calls Module:RecipTable | ||
-- Returns the wikitext returned by Module:RecipeTable | -- Returns the wikitext returned by Module:RecipeTable | ||
function p.formattedItem( | 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: | local recipeTable = require( "Module:TestRecipeTable" ) | ||
local returnVal = '=== | local returnVal = '=== Crafting Recipes ===\n\n' | ||
if #sepRecipes[1] == 0 then | |||
if | returnVal = returnVal .. "''None''\n\n=== Used in Recipes ===\n\n " | ||
returnVal = returnVal .. | |||
else | else | ||
returnVal = returnVal .. | returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[1] ) ) .. '\n\n=== Used in Recipes ===\n\n' | ||
end | end | ||
if #sepRecipes[2] == 0 then | |||
if | returnVal = returnVal .. "''None''\n" | ||
returnVal = returnVal .. | |||
else | else | ||
returnVal = returnVal .. | returnVal = returnVal .. recipeTable.main( formatRecipes( sepRecipes[2] ) ) | ||
end | end | ||
Line 123: | Line 163: | ||
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: | local recipeTable = require( "Module:TestRecipeTable" ) | ||
local returnVal = '=== ' .. | local returnVal = '=== Recipes at ' .. table .. ' ===\n\n' | ||
if #recipes == 0 then | if #recipes == 0 then | ||
returnVal = returnVal .. "' | returnVal = returnVal .. "''None''\n" | ||
else | else | ||
returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) ) | returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) ) | ||
Line 140: | Line 180: | ||
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: | local recipeTable = require( "Module:TestRecipeTable" ) | ||
local returnVal = '=== ' .. | local returnVal = '=== ' .. group .. ' Recipes ===\n\n' | ||
if #recipes == 0 then | if #recipes == 0 then | ||
returnVal = returnVal .. "' | returnVal = returnVal .. "''None''\n" | ||
else | else | ||
returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) ) | returnVal = returnVal .. recipeTable.main( formatRecipes( recipes ) ) |