Module:RecipeDetails: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
Line 3: Line 3:


-- Takes a list of items and an optional iterator for indentation.
-- Takes a list of items and an optional iterator for indentation.
function p.RecipeBreakdownRecursive(items, i)
local function RecipeBreakdownRecursive(items, i)
local indent = i or 1 -- Used to set padding later.
local indent = i or 1 -- Used to set padding later.
local fullTableStruct = {
local fullTableStruct = { --[[ inserted as (indent, itemName, amount) ]] }
-- inserted as ( indent, itemName, amount )
}
for n = 1, #items do -- Should be a max of 4 items per recipe.
for n = 1, #items do -- Should be a max of 4 items per recipe.
-- Insert item into fullTableStruct.
-- Insert item into fullTableStruct.
Line 17: Line 15:
end
end


local function RecipeBreakdownFormatter(fullList)
local function RecipeBreakdownFormatter(fullTable)
local result = ''
local result = ''
local Row = [==[<tr></tr>]==]
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.
-- Format output rows.
-- Concatenate strings together.
-- Concatenate strings together.
Line 25: Line 24:
end
end


local function RecipeTotalsFormatter(fullList)
local function RecipeTotalsFormatter(fullTable)
local result = ''
local result = ''
local Row = [==[<tr></tr>]==]
local Row = [==[<tr><td style="text-align:right">{{CraftReq|%s}}</td><td>{{Numdisplay|%s}}</td></tr>]==]
-- Sort list
-- Sort list
-- Add same ingrediets together.
-- Add same ingrediets together.
Line 49: Line 48:
     local tuple = {}
     local tuple = {}
     for n=1, 4 do
     for n=1, 4 do
     if results['Resource'..n] ~= nil then break end -- If an empty cell is encountered, stop processing items.
     if results['Resource'..n] == nil or results['Resource'..n] == '' then break end -- If an empty cell is encountered, stop processing items.
     tuple = {name = results['Resource'..n], amount = results['Quantity'..n]}
     tuple.item = results['Resource'..n]
    tuple.amount = results['Quantity'..n]
ingredients[n] = tuple
ingredients[n] = tuple
     end
     end
Line 60: Line 60:
function p.Builder( frame )
function p.Builder( frame )
local item = frame.args[1]
local item = frame.args[1]
-- build the data and store it in the table.
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
end


function p.Main( frame )
function p.Main( frame )
-- Query 'Builder' table for the recipe and retrieve the ingeredients
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 table data for item
    return string.format("{{Detrecipe/tab|reci=%s|tot=%s}}", results.Breakdown, results.Totals)
end
end


return p
return p

Revision as of 17:10, 26 March 2024

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