Module:RecipeDetails

From IdleOn MMO Wiki
Revision as of 17:10, 26 March 2024 by Kiokurashi (talk | contribs)

Documentation for this module may be created at Module:RecipeDetails/doc

local p = {}
local cargo = mw.ext.cargo

-- Takes a list of items and an optional iterator for indentation.
local function RecipeBreakdownRecursive(items, i)
	local indent = i or 1 -- Used to set padding later.
	local fullTableStruct = { --[[ inserted as (indent, itemName, amount) ]]	}
	for n = 1, #items do -- Should be a max of 4 items per recipe.
		-- Insert item into fullTableStruct.
		-- Query smithing table for the recipe and retrieve the ingeredients
		-- If previous line's result is not nil then recursively call this function and increment i.
	end
	
	return fullTableStruct
end

local function RecipeBreakdownFormatter(fullTable)
	local result = ''
	local Row = [==[<tr><td style="padding-left:%spx">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==]
    -- Index is 6 for the first tier, and 20n+5 for the rest.
	-- Format output rows.
	-- Concatenate strings together.
	return result
end

local function RecipeTotalsFormatter(fullTable)
	local result = ''
	local Row = [==[<tr><td style="text-align:right">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==]
	-- Sort list
	-- Add same ingrediets together.
	-- Format output rows.
	-- Concatenate strings together.
	
	return result
end

-- Query smithing table for the recipe and retrieve the ingeredients
local function getReecipeItems( item )
	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 0 end -- if no results, exit with 0
    
    local tuple = {}
    for n=1, 4 do
    	if results['Resource'..n] == nil or results['Resource'..n] == '' then break end -- If an empty cell is encountered, stop processing items.
    	tuple.item = results['Resource'..n]
    	tuple.amount = results['Quantity'..n]
		ingredients[n] = tuple
    end
	
	-- Return table of items and values.
    return ingredients
end

function p.Builder( frame )
	local item = frame.args[1]
	local items = getRecipeItems(item)
	local fullTableStruct = RecipeBreakdownRecursive(items)
	
	-- build the data.
	local breakdown = RecipeBreakdownFormatter(fullTableStruct)
	local totals    = RecipeTotalsFormatter(fullTableStruct)
	
	-- Declare the Cargo Table: item, breakdown, totals.
	local cargoTable = 'DetailedRecipes'
    local args = {
        Item = 'String',
        Breakdown = 'Wikitext',
        Totals = 'Wikitext' }
    local results = cargo.declare( cargoTable, args )
    
	-- Store it in the Cargo Table: item, breakdown, totals.
    local args = {
        Item = item,
        Breakdown = breakdown,
        Totals = totals }
    local results = cargo.store( cargoTable, args )
	
end

function p.Main( frame )
	local item = frame.args[1]
	
	-- Query 'Builder' table for the recipe and retrieve the outputs
	local tables = 'DetailedRecipes'
    local fields = 'Breakdown, Totals'
    local args = {
        where = 'Item = "' .. item .. '"'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end
    
	-- return table data for item
    return string.format("{{Detrecipe/tab|reci=%s|tot=%s}}", results.Breakdown, results.Totals)
end

return p