Module:RecipeDetails/Pull: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
Line 51: Line 51:
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return end
     if #results == 0 then return end
    local function split(inputstr)
        local t={}
        for str in string.gmatch(inputstr, "([^,%s]+)") do t[#t+1] = str end
        return t
end
      
      
local tablehead = [[<table class="wikitable">
local tablehead = [[<table class="wikitable">
<tr><th style="width:80%;">Item</th><th>Qty</th></tr>]]
<tr><th style="width:80%;">Item</th><th>Qty</th></tr>]]
-- build the data.
-- build the data.
local breakdown = tablehead..RecipeBreakdownFormatter(results[1].Breakdown).."</table>"
local formatBreakdown = split(results[1].Breakdown)
local totals    = tablehead..RecipeTotalsFormatter(results[1].Totals).."</table>"
local formatTotals = split(results[1].Totals)
local breakdown = tablehead..RecipeBreakdownFormatter(formatBreakdown).."</table>"
local totals    = tablehead..RecipeTotalsFormatter(formatTotals).."</table>"
return '<div class="detrecipe">' .. frame:extensionTag( 'tabber', string.format(tabstr, results[1].Breakdown, results[1].Totals)) .. "</div>"
return '<div class="detrecipe">' .. frame:extensionTag( 'tabber', string.format(tabstr, results[1].Breakdown, results[1].Totals)) .. "</div>"
end
end


return p
return p

Revision as of 02:43, 17 April 2024

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

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

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 n = 1
	while n < #fullTable do
    	-- Index is 6 for the first tier, and 20n+5 for the rest.
    	local index  = fullTable[n]
    	local item   = fullTable[n+1]
    	local amount = fullTable[n+2]
		if index > 1 then index = index * 20 + 5 else padding = 6 end
		results[#results+1] = string.format(Row, index, item, amount)
		-- Iterate to the end of set.
		n = n + 3
	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.
	local n = 1
	while n < #baseIngTable do
    	local item   = fullTable[n]
    	local amount = fullTable[n+1]
		result[#result+1] = string.format(Row, item, amount)
		n = n + 2
	end
	
	return table.concat(result, "")
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
    local function split(inputstr)
        local t={}
        for str in string.gmatch(inputstr, "([^,%s]+)") do t[#t+1] = str end
        return t
	end
    
	local tablehead = [[<table class="wikitable">
						<tr><th style="width:80%;">Item</th><th>Qty</th></tr>]]
	-- build the data.
	local formatBreakdown = split(results[1].Breakdown)
	local formatTotals = split(results[1].Totals)
	local breakdown = tablehead..RecipeBreakdownFormatter(formatBreakdown).."</table>"
	local totals    = tablehead..RecipeTotalsFormatter(formatTotals).."</table>"
	return '<div class="detrecipe">' .. frame:extensionTag( 'tabber', string.format(tabstr, results[1].Breakdown, results[1].Totals)) .. "</div>"
end

return p