Module:RecipeDetails/Pull: Difference between revisions
From IdleOn MMO Wiki
Kiokurashi (talk | contribs) mNo edit summary |
Kiokurashi (talk | contribs) mNo edit summary |
||
(9 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
while n < #fullTable do | while n < #fullTable do | ||
-- Index is 6 for the first tier, and 20n+5 for the rest. | -- Index is 6 for the first tier, and 20n+5 for the rest. | ||
local index = fullTable[n] | local index = tonumber(fullTable[n]) | ||
local item = fullTable[n+1] | local item = fullTable[n+1] | ||
local amount = fullTable[n+2] | local amount = tonumber(fullTable[n+2]) | ||
if index > 1 then index = index * 20 + 5 else | if index > 1 then index = index * 20 + 5 else index = 6 end | ||
results[#results+1] = string.format(Row, index, item, amount) | results[#results+1] = string.format(Row, index, item, amount) | ||
-- Iterate to the end of set. | -- Iterate to the end of set. | ||
Line 29: | Line 29: | ||
local n = 1 | local n = 1 | ||
while n < #baseIngTable do | while n < #baseIngTable do | ||
local item = | local item = baseIngTable[n] | ||
local amount = | local amount = tonumber(baseIngTable[n+1]) | ||
result[#result+1] = string.format(Row, item, amount) | result[#result+1] = string.format(Row, item, amount) | ||
n = n + 2 | n = n + 2 | ||
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={} | |||
-- 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"> | 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 | local formatBreakdown = split(results[1].Breakdown) | ||
local totals = tablehead..RecipeTotalsFormatter( | local formatTotals = split(results[1].Totals) | ||
return '<div class="detrecipe">' .. frame:extensionTag( 'tabber', string.format(tabstr, | 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 | end | ||
return p | 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