Module:RecipeDetails/Pull: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
local metat = mw.loadData("RecipeDetails/data").allRecipes
local p = {}
local t = {}
local cargo = mw.ext.cargo
function recFormat(tab)
 
local inner = {}
local function RecipeBreakdownFormatter(inFullTable)
for k, v in pairs(tab) do
local fullTable = inFullTable
inner[k] = recFormat(v)
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  = tonumber(fullTable[n])
    local item  = fullTable[n+1]
    local amount = tonumber(fullTable[n+2])
if index > 1 then index = index * 20 + 5 else index = 6 end
results[#results+1] = string.format(Row, index, item, amount)
-- Iterate to the end of set.
n = n + 3
end
end
return inner
-- Concatenate strings together.
return table.concat(results, "")
end
end
t = recFormat(metat)
 
return t
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  = baseIngTable[n]
    local amount = tonumber(baseIngTable[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={}
        -- Lua doesn't seem to have a pattern match sequence for "exactly these characters in this order" that works with a gmatch which returns a capture.
        for str in inputstr:gsub(', ', '\n'):gmatch('[^\r\n]+') 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, breakdown, totals)) .. "</div>"
end
 
return p

Latest revision as of 03:18, 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  = tonumber(fullTable[n])
    	local item   = fullTable[n+1]
    	local amount = tonumber(fullTable[n+2])
		if index > 1 then index = index * 20 + 5 else index = 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   = baseIngTable[n]
    	local amount = tonumber(baseIngTable[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={}
        -- Lua doesn't seem to have a pattern match sequence for "exactly these characters in this order" that works with a gmatch which returns a capture.
        for str in inputstr:gsub(', ', '\n'):gmatch('[^\r\n]+') 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, breakdown, totals)) .. "</div>"
end

return p