Module:AwesomeSinkRecipeTable

local cargo = mw.ext.cargo local p = {} local json = require('Module:Json') local sinkPoints = json.decode(mw.getCurrentFrame:expandTemplate{title='Template:ResourceSinkPoints.json'})

local getRecipes = function(itemName) local fields = 'recipeName, alternateRecipe, mainRecipe, craftedIn, inCraftBench, inWorkShop, craftingTime, craftingClicks, researchTier, unreleased, experimental, product=product1, product2, product3, product4, productCount=productCount1, productCount2, productCount3, productCount4, ingredient1, quantity1, ingredient2, quantity2, ingredient3, quantity3, ingredient4, quantity4, ingredient5, quantity5, ingredient6, quantity6, ingredient7, quantity7, ingredient8, quantity8' local recipes = cargo.query('crafting_recipes', fields, {where = 'product = "'.. itemName ..'" and NOT experimental and NOT craftedIn="Miner"', orderBy = 'mainRecipe DESC, alternateRecipe ASC'}) for i=1, #recipes, 1 do       local recipe = recipes[i] -- combine ingredient and product fields into arrays local ingredients = {} for i=1, 9, 1 do           local item = recipe["ingredient" .. i]           if item ~= nil and #item > 0 then ingredients[i] = { name = item, amount = tonumber(recipe["quantity" .. i]) or 1 } end end

local products = {} for i=1, 4, 1 do           local item = recipe["product" .. i]           if item ~= nil and #item > 0 then products[i] = { name = item, amount = tonumber(recipe["productCount" .. i]) or 1 } end end

recipe.ingredients = ingredients recipe.products = products end return recipes end

local getKeysSortedByValue = function(tbl, sortFunction) -- create default comparator if none was provided if sortFunction == nil then sortFunction = function(a, b) return a < b end end

local keys = {} for key in pairs(tbl) do       table.insert(keys, key) end

table.sort(keys, function(a, b) return sortFunction(tbl[a], tbl[b]) end)

return keys end

local renderIngredientsOrProducts = function(frame, list) local ingredientsValue = 0 local ingredientsColumn = '' local intangible = false for i=1, #list, 1 do       local ingredient = list[i] local ingredientLink = frame:expandTemplate{ title = 'ItemLink', args = { ingredient.name } }

local ingredientValue = sinkPoints[ingredient.name]

if ingredientValue == nil then ingredientValue = 'No point value' intangible = true else ingredientsValue = ingredientsValue + (ingredientValue * ingredient.amount) ingredientValue = ingredientValue .. ' points per item' end

ingredientsColumn = ingredientsColumn .. ingredient.amount .. ' x ' .. ingredientLink .. ' ('.. ingredientValue ..') ' end return {column = ingredientsColumn, value = ingredientsValue, intangible = intangible} end

local renderRecipeRow = function(frame, recipe)

local recipeNameColumn = recipe.recipeName if recipe.alternateRecipe == "1" then recipeNameColumn = recipe.recipeName .. ' ' .. 'Alternate ' end

local ingredientCalc = renderIngredientsOrProducts(frame, recipe.ingredients) local ingredientsValue = ingredientCalc.value local ingredientsColumn = ingredientCalc.column

local productCalc = renderIngredientsOrProducts(frame, recipe.products) local productValue = productCalc.value local productColumn = productCalc.column

local ratio = productValue / ingredientsValue

if ingredientCalc.intangible then ingredientsValue = ingredientsValue .. ' (Some ingredients have no ticket value)' ratio = ratio .. ' (Ratio invalid as some ingredients have no ticket value)' end

return '\n|-\n| ' .. recipeNameColumn .. ' || ' .. ingredientsColumn .. ' || ' .. ingredientsValue .. ' || ' .. productColumn .. ' || ' .. productValue .. ' || ' .. ratio

end

p.getItemsTable = function(frame) local txt = '{| class="wikitable sortable mw-collapsible mw-collapsed"\n|-\n! Item !! Points !! Recipe !! Ingredients !! Ingredients Value !! Products !! Products Value !! Improvement Ratio'

local sortedKeys = getKeysSortedByValue(sinkPoints)

for i=1, #sortedKeys, 1 do       local itemName = sortedKeys[i] local points = sinkPoints[sortedKeys[i]] local recipes = getRecipes(itemName)

local itemLink = frame:expandTemplate{ title = 'ItemLink', args = { itemName } }

local rowspan = ' rowspan="'.. (#recipes + 1) ..'" '

txt = txt .. '\n|-\n|'.. rowspan .. ' | ' .. itemLink .. ' || ' .. rowspan .. ' | ' .. points

for i=1, #recipes, 1 do           txt = txt .. renderRecipeRow(frame, recipes[i]) end

end

txt = txt .. '\n|}' return txt end

return p