Module:RecipeDetails: Difference between revisions
From IdleOn MMO Wiki
Kiokurashi (talk | contribs) mNo edit summary |
Kiokurashi (talk | contribs) mNo edit summary |
||
Line 3: | Line 3: | ||
local function appendTable(tableA, tableB) | local function appendTable(tableA, tableB) | ||
local aLen = # | local a = tableA | ||
for n=1, # | local b = tableB | ||
local aLen = #a | |||
for n=1, #b do | |||
a[aLen+n] = b[n] | |||
end | end | ||
return | return a | ||
end | end | ||
local function fuseBaseIngredients(oldTable, newTable) | local function fuseBaseIngredients(oldTable, newTable) | ||
for n=1, # | local old = oldTable | ||
local new = newTable | |||
for n=1, #new do -- For each item in the new table | |||
local newItem = true -- Initialize newItem boolean | local newItem = true -- Initialize newItem boolean | ||
for o=1, # | for o=1, #old do -- Check each item in the old table | ||
if | if old[o].Item == new[n].Item then -- If the there is a match | ||
-- Add value, set boolean to false, and break loop. | -- Add value, set boolean to false, and break loop. | ||
old[o].Amount = old[o].Amount + new[n].Amount | |||
newItem = false | newItem = false | ||
break | break | ||
end | end | ||
end | end | ||
if newItem == true then | if newItem == true then old[#old+1] = new[n] end -- If it is a new item then add new item. | ||
end | end | ||
return | return old | ||
end | end | ||
-- Query smithing table for the recipe and retrieve the ingeredients | -- Query smithing table for the recipe and retrieve the ingeredients | ||
local function getRecipeItems( | local function getRecipeItems( inItem, multi ) | ||
local item = inItem | |||
local multiplier = multi or 1 | local multiplier = multi or 1 | ||
local ingredients = {} | local ingredients = {} | ||
Line 51: | Line 56: | ||
-- Takes a list of items and an optional iterator for indentation. | -- Takes a list of items and an optional iterator for indentation. | ||
local function RecipeBreakdownRecursive( | local function RecipeBreakdownRecursive(inItems, i, inFullTable) | ||
local indent = i | local results = inFullTable | ||
local fullTableStruct = { --[[ inserted as (Padding, Item, Amount) ]] } | local items = inItems | ||
local baseIngredients = { --[[ insereted as (Item, Amount) ]] } | local indent = i | ||
local fullTableStruct = results.recipe ~= nil and results.recipe or { --[[ inserted as (Padding, Item, Amount) ]] } | |||
local baseIngredients = results.ingredients ~= nil and results.ingredients or { --[[ insereted as (Item, Amount) ]] } | |||
for n = 1, #items do | for n = 1, #items do | ||
Line 69: | Line 76: | ||
else | else | ||
-- If reciItems is not nil then recursively call this function and increment i. | -- If reciItems is not nil then recursively call this function and increment i. | ||
local retTable = RecipeBreakdownRecursive(reciItems, indent + 1 | results.recipe = fullTableStruct | ||
results.ingredients = baseIngredients | |||
local retTable = RecipeBreakdownRecursive(reciItems, indent + 1, results) | |||
end | end | ||
end | end | ||
results.recipe = fullTableStruct | results.recipe = fullTableStruct | ||
results.ingredients = baseIngredients | results.ingredients = baseIngredients | ||
Line 81: | Line 87: | ||
end | end | ||
local function RecipeBreakdownFormatter(fullTable | local function RecipeBreakdownFormatter(inFullTable) | ||
local fullTable = inFullTable | |||
local results = {} | local results = {} | ||
local Row = [==[<tr><td style="padding-left:%spx">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==] | local Row = [==[<tr><td style="padding-left:%spx">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==] | ||
Line 95: | Line 102: | ||
end | end | ||
local function RecipeTotalsFormatter(baseIngTable | local function RecipeTotalsFormatter(inBaseIngTable) | ||
local baseIngTable = inBaseIngTable | |||
local result = {} | local result = {} | ||
local Row = [==[<tr><td style="text-align:right">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==] | local Row = [==[<tr><td style="text-align:right">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==] | ||
Line 106: | Line 114: | ||
end | end | ||
local function checkDeepestLevel(recipesTable | local function checkDeepestLevel(inRecipesTable) | ||
local recipesTable = inRecipesTable | |||
local deepestLevel = 1 | local deepestLevel = 1 | ||
for n=1, #recipesTable do | for n=1, #recipesTable do | ||
Line 120: | Line 129: | ||
local item = frame.args.Item | local item = frame.args.Item | ||
local items = getRecipeItems(item) | local items = getRecipeItems(item) | ||
local doubleTable = RecipeBreakdownRecursive(items) | local doubleTable = RecipeBreakdownRecursive(items, 1, {}) | ||
local fullTableStruct = doubleTable.recipe | local fullTableStruct = doubleTable.recipe | ||
local baseIngredients = doubleTable.ingredients | local baseIngredients = doubleTable.ingredients |
Revision as of 19:36, 10 April 2024
Documentation for this module may be created at Module:RecipeDetails/doc
local p = {}
local cargo = mw.ext.cargo
local function appendTable(tableA, tableB)
local a = tableA
local b = tableB
local aLen = #a
for n=1, #b do
a[aLen+n] = b[n]
end
return a
end
local function fuseBaseIngredients(oldTable, newTable)
local old = oldTable
local new = newTable
for n=1, #new do -- For each item in the new table
local newItem = true -- Initialize newItem boolean
for o=1, #old do -- Check each item in the old table
if old[o].Item == new[n].Item then -- If the there is a match
-- Add value, set boolean to false, and break loop.
old[o].Amount = old[o].Amount + new[n].Amount
newItem = false
break
end
end
if newItem == true then old[#old+1] = new[n] end -- If it is a new item then add new item.
end
return old
end
-- Query smithing table for the recipe and retrieve the ingeredients
local function getRecipeItems( inItem, multi )
local item = inItem
local multiplier = multi or 1
local ingredients = {}
-- Query
local args = {
where = 'AnvilCraft.Item="'.. item ..'"',
orderBy = 'AnvilCraft.Item'
}
local results = cargo.query('AnvilCraft', 'Resource1, Quantity1, Resource2, Quantity2, Resource3, Quantity3, Resource4, Quantity4', args )
if #results == 0 then return {} end -- if no results, exit with an empty table.
local tuple = {}
for n=1, 4 do
if results[1]['Resource'..n] == nil or results[1]['Resource'..n] == '' then break end -- If an empty cell is encountered, stop processing items.
local tuple = { item = results[1]['Resource'..n], amount = tonumber(results[1]['Quantity'..n]) * multiplier}
ingredients[n] = tuple
end
-- Return table of items and values.
return ingredients
end
-- Takes a list of items and an optional iterator for indentation.
local function RecipeBreakdownRecursive(inItems, i, inFullTable)
local results = inFullTable
local items = inItems
local indent = i
local fullTableStruct = results.recipe ~= nil and results.recipe or { --[[ inserted as (Padding, Item, Amount) ]] }
local baseIngredients = results.ingredients ~= nil and results.ingredients or { --[[ insereted as (Item, Amount) ]] }
for n = 1, #items do
local tuple = {Padding = indent, Item = items[n].item, Amount = items[n].amount}
-- Insert item into fullTableStruct.
fullTableStruct[#fullTableStruct+1] = tuple
-- Query smithing table for the recipe and retrieve the ingeredients
local reciItems = getRecipeItems(items[n].item, items[n].amount)
-- If previous line's result is not empty then recursively call this function and increment i.
if #reciItems == 0 then
-- If item has value in baseingredients then increase, otherwise set to the value.
local baseItem = {Item = items[n].item, Amount = items[n].amount}
baseIngredients = fuseBaseIngredients(baseIngredients, {baseItem})
else
-- If reciItems is not nil then recursively call this function and increment i.
results.recipe = fullTableStruct
results.ingredients = baseIngredients
local retTable = RecipeBreakdownRecursive(reciItems, indent + 1, results)
end
end
results.recipe = fullTableStruct
results.ingredients = baseIngredients
return results
end
local function RecipeBreakdownFormatter(inFullTable)
local fullTable = inFullTable
local results = {}
local Row = [==[<tr><td style="padding-left:%spx">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==]
-- Format output rows.
local padding = 0
for n=1, #fullTable do
-- Index is 6 for the first tier, and 20n+5 for the rest.
if fullTable[n].Padding > 1 then padding = fullTable[n].Padding * 20 + 5 else padding = 6 end
results[n] = string.format(Row, padding, fullTable[n].Item, fullTable[n].Amount)
end
-- Concatenate strings together.
return table.concat(results, "")
end
local function RecipeTotalsFormatter(inBaseIngTable)
local baseIngTable = inBaseIngTable
local result = {}
local Row = [==[<tr><td style="text-align:right">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==]
-- Format output rows.
for n=1, #baseIngTable do
result[n] = string.format(Row, baseIngTable[n].Item, baseIngTable[n].Amount)
end
return table.concat(result, "")
end
local function checkDeepestLevel(inRecipesTable)
local recipesTable = inRecipesTable
local deepestLevel = 1
for n=1, #recipesTable do
if recipesTable[n].Padding > deepestLevel then
deepestLevel = recipesTable[n].Padding
end
end
return deepestLevel
end
function p.Builder( frame )
local item = frame.args.Item
local items = getRecipeItems(item)
local doubleTable = RecipeBreakdownRecursive(items, 1, {})
local fullTableStruct = doubleTable.recipe
local baseIngredients = doubleTable.ingredients
-- Check if deepest level is greater than 1. If it is then continue.
if checkDeepestLevel(fullTableStruct) > 1 then
local tablehead = [[<table class="wikitable">
<tr><th style="width:80%;">Item</th><th>Qty</th></tr>]]
-- build the data.
local breakdown = tablehead..RecipeBreakdownFormatter(fullTableStruct).."</table>"
local totals = tablehead..RecipeTotalsFormatter(baseIngredients).."</table>"
-- Store it in the Cargo Table: item, breakdown, totals.
frame:expandTemplate{ title = 'DetailedRecipes', args = { Item = item, Breakdown = breakdown, Totals = totals } }
-- test output
-- mw.logObject(items, "items")
end
end
function p.Main( frame )
local item = frame.args.Item
local tabstr = [[Recipe=%s
|-|Totals=%s]]
-- Query 'DetailedRecipes' table for the recipe and retrieve the outputs
local tables = 'DetailedRecipes'
local fields = 'Breakdown, Totals'
local args = {
where = 'DetailedRecipes.Item="' .. item .. '"',
orderBy = 'DetailedRecipes.Item'
}
local results = cargo.query( tables, fields, args )
if #results == 0 then return end
-- mw.logObject(results)
-- return table data for item
return '<div class="detrecipe">' .. frame:extensionTag( 'tabber', string.format(tabstr, results[1].Breakdown, results[1].Totals)) .. "</div>"
-- frame:expandTemplate{ title = 'Detrecipe/tab', args = { reci = results[1].Breakdown, tot = results[1].Totals } }
end
return p