Module:AlternateRecipesTable

local cargo = mw.ext.cargo local recipeUsage = require(Module:RecipeUsage)

local p = {}

-- displays a table with info about alternate recipes compared to their default counterpart function p.table local rows = {}

-- lookup recipe data from database local fields = table.concat(recipeUsage.argList, ',') local queryArgs = { where = 'product IS NOT NULL', orderBy = 'alternateRecipe ASC, researchTier ASC, product ASC, recipeName ASC', limit = 500 } local recipes = cargo.query('crafting_recipes', fields, queryArgs)

-- store the default recipes for comparison local defaultRecipes = {} local r = 1 while recipes[r] and not (recipes[r].alternateRecipe and recipes[r].alternateRecipe == '1') do   local craftingTime = tonumber(recipes[r].craftingTime) if craftingTime and craftingTime > 0 then defaultRecipes[recipes[r].product] = recipes[r]

local ingRows = {} local ii = 1 local batchPerMinute = 60 / craftingTime while recipes[r]['ingredient'..ii] and tonumber(recipes[r]['quantity'..ii]) do       table.insert(ingRows, ' '..recipes[r]['ingredient'..ii]..' '..round(batchPerMinute * tonumber(recipes[r]['quantity'..ii]))..' ') ii = ii + 1 end defaultRecipes[recipes[r].product].ingredientTable = table.concat(ingRows,' ') end r = r + 1 end

-- header local cols={'Alternate name','Product', 'Ingredients /min', 'Rate/min', 'original Rate/min', 'original Ingredients /min', 'Research Tier'} table.insert(rows,' '..table.concat(cols,'  ')..'  ')

-- loop the alt recipes local altRecipesCount = 0 while recipes[r] do   local craftingTime = tonumber(recipes[r].craftingTime) if craftingTime and craftingTime > 0 then local batchPerMinute = 60 / craftingTime

local ingRows = {} local ii = 1 while recipes[r]['ingredient'..ii] and tonumber(recipes[r]['quantity'..ii]) do       table.insert(ingRows, ' '..recipes[r]['ingredient'..ii]..' '..round(batchPerMinute * tonumber(recipes[r]['quantity'..ii]))..' ') ii = ii + 1 end

cols = {''..((recipes[r].recipeName and recipes[r].recipeName ~= '') and recipes[r].recipeName or recipes[r].product)..' ', ' '..recipes[r].product..' ', ''..table.concat(ingRows,' ')..' ', ''..round(recipes[r].productsPerMinute)..' ' }     -- if default recipe is available, show it for comparison if defaultRecipes[recipes[r].product] then table.insert(cols, ''..round(defaultRecipes[recipes[r].product].productsPerMinute)..' ') table.insert(cols, ''..defaultRecipes[recipes[r].product].ingredientTable..' ') else table.insert(cols,' ') table.insert(cols,' ') end table.insert(cols, ' '..(recipes[r].researchTier and recipes[r].researchTier or '')..' ') table.insert(rows,' '..table.concat(cols)..' ') altRecipesCount = altRecipesCount + 1 end r = r + 1 end

return ' Total alternate recipes: '..altRecipesCount..' ' end

-- round to 1 decimal for display (string) function round(n) local tentimes = math.floor(n*10+0.5) local suffix = '' if tentimes % 10 == 0 then suffix = '.0' end return (tentimes/10)..suffix end

return p