Module:GearSet
Documentation for this module may be created at Module:GearSet/doc
local p = {}
local NUMERIC_COLUMNS = {
{ key = 'ac', header = 'AC', labels = { 'AC' } },
{ key = 'hp', header = 'HP', labels = { 'HP' } },
{ key = 'mana', header = 'Mana', labels = { 'MANA' } },
{ key = 'endurance', header = 'End', labels = { 'ENDURANCE', 'ENDUR', 'END' } },
{ key = 'str', header = 'STR', labels = { 'STR' } },
{ key = 'sta', header = 'STA', labels = { 'STA' } },
{ key = 'dex', header = 'DEX', labels = { 'DEX' } },
{ key = 'agi', header = 'AGI', labels = { 'AGI' } },
{ key = 'cha', header = 'CHA', labels = { 'CHA' } },
{ key = 'wis', header = 'WIS', labels = { 'WIS' } },
{ key = 'int', header = 'INT', labels = { 'INT' } },
{ key = 'svmagic', header = 'SV MAG', labels = { 'SV MAGIC', 'SV MAG' } },
{ key = 'svfire', header = 'SV FIRE', labels = { 'SV FIRE' } },
{ key = 'svcold', header = 'SV COLD', labels = { 'SV COLD' } },
{ key = 'svdisease', header = 'SV DIS', labels = { 'SV DISEASE', 'SV DIS' } },
{ key = 'svpoison', header = 'SV POI', labels = { 'SV POISON', 'SV POI' } },
}
local function trim(value)
return mw.text.trim(tostring(value or ''))
end
local function escapePattern(value)
return (
value:gsub(
'([%^%$%(%)%%%.%[%]%*%+%-%?])',
'%%%1'
)
)
end
-- Reads the arguments inside the page's {{Itempage ...}} invocation.
-- It respects nested templates, wiki links, and wiki tables so pipes
-- inside those constructs are not mistaken for Itempage parameters.
local function parseItempageParameters(content)
local startPosition = content:find('{{%s*[Ii]tempage[%s|]')
if not startPosition then
return nil
end
local segments = {}
local buffer = {}
local curlyDepth = 1
local squareDepth = 0
local tableDepth = 0
local position = startPosition + 2
local contentLength = #content
while position <= contentLength do
local pair = content:sub(position, position + 1)
local character = content:sub(position, position)
if pair == '{{' then
curlyDepth = curlyDepth + 1
table.insert(buffer, pair)
position = position + 2
elseif pair == '}}' then
if curlyDepth == 1 then
table.insert(segments, table.concat(buffer))
break
end
curlyDepth = curlyDepth - 1
table.insert(buffer, pair)
position = position + 2
elseif pair == '[[' then
squareDepth = squareDepth + 1
table.insert(buffer, pair)
position = position + 2
elseif pair == ']]' and squareDepth > 0 then
squareDepth = squareDepth - 1
table.insert(buffer, pair)
position = position + 2
elseif pair == '{|' then
tableDepth = tableDepth + 1
table.insert(buffer, pair)
position = position + 2
elseif pair == '|}' and tableDepth > 0 then
tableDepth = tableDepth - 1
table.insert(buffer, pair)
position = position + 2
elseif character == '|'
and curlyDepth == 1
and squareDepth == 0
and tableDepth == 0
then
table.insert(segments, table.concat(buffer))
buffer = {}
position = position + 1
else
table.insert(buffer, character)
position = position + 1
end
end
if #segments < 2 then
return nil
end
local parameters = {}
-- Segment 1 is the template name itself.
for index = 2, #segments do
local parameterName, parameterValue =
segments[index]:match('^%s*([%w_%-]+)%s*=(.*)$')
if parameterName then
parameters[parameterName:lower()] = trim(parameterValue)
end
end
return parameters
end
local function getPageContent(pageName)
local title = mw.title.new(pageName)
-- Follow a few ordinary redirect pages.
for _ = 1, 4 do
if not title then
return nil
end
local content = title:getContent()
if not content then
return nil
end
local redirectTarget = content:match(
'^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*%[%[([^%]|#]+)'
)
if not redirectTarget then
return content
end
title = mw.title.new(trim(redirectTarget))
end
return nil
end
local function normalizeStatsblock(statsblock)
local stats = tostring(statsblock or '')
stats = stats:gsub('\r\n', '\n')
stats = stats:gsub('\r', '\n')
stats = stats:gsub('<!%-%-.-%-%->', '')
stats = stats:gsub('<[bB][rR]%s*/?%s*>', '\n')
stats = stats:gsub(' ', ' ')
stats = stats:gsub('[ \t]+\n', '\n')
return stats
end
local function extractNumber(statsblock, labels)
local uppercaseStats = statsblock:upper()
for _, label in ipairs(labels) do
local pattern =
escapePattern(label:upper()) ..
'%s*:%s*([%+%-]?%d+)'
local value = uppercaseStats:match(pattern)
if value then
return tonumber(value)
end
end
return nil
end
local function extractLine(statsblock, labels)
local uppercaseStats = statsblock:upper()
for _, label in ipairs(labels) do
local pattern =
escapePattern(label:upper()) ..
'%s*:%s*'
local _, valueStart = uppercaseStats:find(pattern)
if valueStart then
local lineEnd =
statsblock:find('\n', valueStart + 1, true) or
(#statsblock + 1)
return trim(
statsblock:sub(valueStart + 1, lineEnd - 1)
)
end
end
return ''
end
local function extractDropMobs(dropsfrom)
local drops = tostring(dropsfrom or '')
drops = drops:gsub('\r\n', '\n')
drops = drops:gsub('\r', '\n')
drops = drops:gsub('<!%-%-.-%-%->', '')
local results = {}
local seen = {}
for line in drops:gmatch('[^\n]+') do
local mob = line:match('^%s*%*+%s*(.-)%s*$')
if mob and mob ~= '' and not seen[mob] then
seen[mob] = true
table.insert(results, mob)
end
end
return table.concat(results, '<br>')
end
local function buildEffect(parameters, statsblock)
local results = {}
local regularEffect = extractLine(
statsblock,
{
'CLICK EFFECT',
'WORN EFFECT',
'EFFECT',
}
)
if regularEffect ~= '' then
table.insert(results, regularEffect)
end
local focusEffect = trim(parameters.focus_effect)
if focusEffect ~= '' then
table.insert(
results,
'Focus: [[' .. focusEffect .. ']]'
)
end
return table.concat(results, '<br>')
end
local function createHoverLink(frame, pageName)
local success, rendered = pcall(
function()
return frame:preprocess('{{:' .. pageName .. '}}')
end
)
if success and trim(rendered) ~= '' then
return rendered
end
return '[[' .. pageName .. ']]'
end
local function readItem(frame, pageName)
local content = getPageContent(pageName)
if not content then
return nil, 'Item page does not exist or could not be read.'
end
local parameters = parseItempageParameters(content)
if not parameters then
return nil, 'No Itempage template was found on the item page.'
end
local statsblock = normalizeStatsblock(parameters.statsblock)
if statsblock == '' then
return nil, 'The item has no statsblock.'
end
local item = {
pageName = pageName,
itemName = trim(parameters.itemname) ~= ''
and trim(parameters.itemname)
or pageName,
hover = createHoverLink(frame, pageName),
slot = extractLine(statsblock, { 'SLOT' }),
effect = buildEffect(parameters, statsblock),
droppedBy = extractDropMobs(parameters.dropsfrom),
}
for _, column in ipairs(NUMERIC_COLUMNS) do
item[column.key] =
extractNumber(statsblock, column.labels)
end
return item
end
local function addCell(row, value, alignLeft)
local cell = row:tag('td')
if alignLeft then
cell:css('text-align', 'left')
end
if value ~= nil and value ~= '' then
cell:wikitext(tostring(value))
end
return cell
end
local function renderTable(frame, args, itemNames)
local output = mw.html.create('table')
output
:addClass('wikitable')
:addClass('sortable')
:css('text-align', 'center')
local caption = trim(args.caption)
if caption == '' then
caption = 'List of items considered part of this set'
end
output:tag('caption'):wikitext(caption)
local groupRow = output:tag('tr')
groupRow
:addClass('sortbottom')
:css('font-weight', 'bold')
groupRow:tag('th')
:attr('colspan', 2)
:wikitext('Item Info')
groupRow:tag('th')
:attr('colspan', 4)
:wikitext('Combat Vitality')
groupRow:tag('th')
:attr('colspan', 7)
:wikitext('Base Attributes')
groupRow:tag('th')
:attr('colspan', 5)
:wikitext('Resistances')
groupRow:tag('th')
:attr('colspan', 2)
:wikitext('Miscellaneous')
local headerRow = output:tag('tr')
headerRow:tag('th'):wikitext('Slot')
headerRow:tag('th'):wikitext('Item Name')
for _, column in ipairs(NUMERIC_COLUMNS) do
headerRow:tag('th'):wikitext(column.header)
end
headerRow:tag('th'):wikitext('Effect')
headerRow:tag('th'):wikitext('Dropped By')
local totals = {}
for _, column in ipairs(NUMERIC_COLUMNS) do
totals[column.key] = 0
end
for _, pageName in ipairs(itemNames) do
local item, errorMessage = readItem(frame, pageName)
if not item then
local errorRow = output:tag('tr')
errorRow:tag('td')
:attr('colspan', 20)
:addClass('error')
:css('text-align', 'left')
:wikitext(
'[[' .. pageName .. ']]: ' .. errorMessage
)
else
local row = output:tag('tr')
addCell(row, item.slot, true)
addCell(row, item.hover, true)
for _, column in ipairs(NUMERIC_COLUMNS) do
local value = item[column.key]
addCell(row, value or '', false)
if value then
totals[column.key] =
totals[column.key] + value
end
end
addCell(row, item.effect, true)
addCell(row, item.droppedBy, true)
end
end
local totalRow = output:tag('tr')
totalRow
:addClass('sortbottom')
:css('font-weight', 'bold')
totalRow:tag('td')
:attr('colspan', 2)
:css('text-align', 'right')
:wikitext('Totals')
for _, column in ipairs(NUMERIC_COLUMNS) do
addCell(
totalRow,
totals[column.key],
false
)
end
addCell(totalRow, '', false)
addCell(totalRow, '', false)
return tostring(output)
end
function p.main(frame)
-- The wrapper template does not need to forward every positional
-- argument. Lua reads them directly from the template invocation.
local parent = frame:getParent()
local args = parent and parent.args or frame.args
local indexedItems = {}
for argumentName, argumentValue in pairs(args) do
local numericIndex = tonumber(argumentName)
local itemName = trim(argumentValue)
if numericIndex and itemName ~= '' then
table.insert(
indexedItems,
{
index = numericIndex,
name = itemName,
}
)
end
end
table.sort(
indexedItems,
function(left, right)
return left.index < right.index
end
)
local itemNames = {}
for _, item in ipairs(indexedItems) do
table.insert(itemNames, item.name)
end
if #itemNames == 0 then
return '<strong class="error">' ..
'Gear Set requires at least one item page.' ..
'</strong>'
end
return renderTable(frame, args, itemNames)
end
return p