Module:RecipeUsage

local p = {}

local recipesData = mw.loadData('Module:CraftingRecipes') local dpl_argList = { 'product_count', 'product_name', 'energy', 'time', 1, 2, 3, 4, 5, 6, 7, 8 }

--Creates crafting table for given recipes -- function createTable(recipes) local table = "" for i=1,#recipes do   local tableArgs = {} tableArgs.energy = recipes[i].energy tableArgs.time = recipes[i].time tableArgs.product_count = recipes[i].product[1] tableArgs.product_name = recipes[i].product[2] local t = 1 for k,ingredient in pairs(recipes[i].ingredients) do       tableArgs[t]   = ingredient[1] tableArgs[t+1] = ingredient[2] t = t + 2 end if i == #recipes then tableArgs.foot = 1 end table = table .. mw.getCurrentFrame:expandTemplate{title='CraftingTable', args=tableArgs} end return table end

--Creates crafting table for given recipes -- function createTableNew(recipes) local table = "" for i=1,#recipes do   if i == #recipes then recipes[i].foot = 1 end table = table .. mw.getCurrentFrame:expandTemplate{title='CraftingTable', args=recipes[i]} end return table end

--Extracts the anonymous pipe-delimited arguments from the   DPL query into a table with the corresponding keys, skipping    templates with `ignoreusage` set, and skipping duplicate templates -- local extractArgs do local seen = {} extractArgs = function( template ) -- Check for duplicate template if seen[template] then return end

seen[template] = true

local tArgs = {} local i = 1 for arg in mw.text.gsplit( template, '\n|' ) do     if arg ~= '' then tArgs[dpl_argList[i]] = arg end i = i + 1 end return tArgs end end

-- -- function filterCraftingRecipes(recipeTable, ingredient) local filtered = {} for i=1,#recipeTable do   if recipeTable[i][2] == ingredient or recipeTable[i][4] == ingredient or recipeTable[i][6] == ingredient or recipeTable[i][8] == ingredient then filtered[#filtered + 1] = recipeTable[i] end end return filtered end

--Performs the DPL query which retrieves the arguments from the crafting   templates on the pages within the requested categories -- function dplQuery return mw.getCurrentFrame:callParserFunction( '#dpl:', {   category = 'Items',    include = '{CraftingTable}:' .. table.concat( dpl_argList, ':' ),    ordermethod = 'title',    mode = 'userformat',    secseparators = '====',    multisecseparators = '===='  } ) end

--REMOVE THIS FUNCTION -- p.crafting = function(f) local productName = mw.title.getCurrentTitle.text local recipes = {} for k,recipe in pairs(recipesData) do   if recipe.product[2] == productName then recipes[1] = recipe return createTable(recipes) end end return "No crafting recipe was found in Module:CraftingRecipes for " .. pageName end

p.usage = function(f) local ingredientName = mw.title.getCurrentTitle.text local recipes = {} for k,recipe in pairs(recipesData) do   for i_k, ingredient in pairs(recipe.ingredients) do       if ingredient[2] == ingredientName then recipes[#recipes + 1] = recipe end end end return createTable(recipes) end

p.usage_test = function local data = dplQuery

local recipes = {} for templateArgs in mw.text.gsplit( data:sub( 5 ), '====' ) do   local tArgs = extractArgs( templateArgs ) if tArgs ~= nil then recipes[#recipes + 1] = tArgs end end

local ingredient = mw.title.getCurrentTitle.text recipes = filterCraftingRecipes(recipes, ingredient)

return createTableNew(recipes) end

return p