Module:BaseListTable: Difference between revisions
Appearance
fixed again |
Adds roman numeral support |
||
| (3 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local cargo = mw.ext.cargo | local cargo = mw.ext.cargo | ||
local function romanToInt(roman) | |||
local map = {I=1, V=5, X=10, L=50, C=100, D=500, M=1000} | |||
local total = 0 | |||
local prev = 0 | |||
for i = #roman, 1, -1 do | |||
local curr = map[roman:sub(i, i):upper()] or 0 | |||
if curr < prev then | |||
total = total - curr | |||
else | |||
total = total + curr | |||
end | |||
prev = curr | |||
end | |||
return total | |||
end | |||
local function extractSeasonNumber(seasonStr) | |||
if not seasonStr then return 0 end | |||
local suffix = seasonStr:match("Season%s+(.+)") | |||
if not suffix then return 0 end | |||
local num = tonumber(suffix) | |||
if num then | |||
return num | |||
else | |||
return romanToInt(suffix) | |||
end | |||
end | |||
function p.Main(frame) | function p.Main(frame) | ||
| Line 6: | Line 35: | ||
local fields = 'season,basename,owner,basetype,location,dateestablished' | local fields = 'season,basename,owner,basetype,location,dateestablished' | ||
local args = { | local args = { | ||
limit = 1000 | |||
limit = | |||
} | } | ||
local results = cargo.query(tables, fields, args) | local results = cargo.query(tables, fields, args) | ||
table.sort(results, function(a, b) | |||
local sa = extractSeasonNumber(a.season) | |||
local sb = extractSeasonNumber(b.season) | |||
if sa ~= sb then | |||
return sa > sb -- Descending order | |||
end | |||
return (a.basename or '') < (b.basename or '') | |||
end) | |||
local out = {} | local out = {} | ||
| Line 17: | Line 54: | ||
for _, row in ipairs(results) do | for _, row in ipairs(results) do | ||
local season = '' | local season = row.season or '' | ||
if row. | if row.basename ~= '$1' and row.basename ~= 'Creeper Cove' then | ||
table.insert(out, '|-') | |||
table.insert(out, string.format('| %s || [[%s]] || %s || %s || %s || %s', | |||
season, | |||
row.basename or '', | |||
row.owner or '', | |||
row.basetype or '', | |||
row.location or '', | |||
row.dateestablished or '' | |||
)) | |||
end | |||
end | end | ||
Latest revision as of 14:41, 24 June 2025
Documentation for this module may be created at Module:BaseListTable/doc
local p = {}
local cargo = mw.ext.cargo
local function romanToInt(roman)
local map = {I=1, V=5, X=10, L=50, C=100, D=500, M=1000}
local total = 0
local prev = 0
for i = #roman, 1, -1 do
local curr = map[roman:sub(i, i):upper()] or 0
if curr < prev then
total = total - curr
else
total = total + curr
end
prev = curr
end
return total
end
local function extractSeasonNumber(seasonStr)
if not seasonStr then return 0 end
local suffix = seasonStr:match("Season%s+(.+)")
if not suffix then return 0 end
local num = tonumber(suffix)
if num then
return num
else
return romanToInt(suffix)
end
end
function p.Main(frame)
local tables = 'base_data'
local fields = 'season,basename,owner,basetype,location,dateestablished'
local args = {
limit = 1000
}
local results = cargo.query(tables, fields, args)
table.sort(results, function(a, b)
local sa = extractSeasonNumber(a.season)
local sb = extractSeasonNumber(b.season)
if sa ~= sb then
return sa > sb -- Descending order
end
return (a.basename or '') < (b.basename or '')
end)
local out = {}
table.insert(out, '{| class="wikitable sortable plainlinks"')
table.insert(out, '! Season !! Base !! Owner !! Type !! Location !! Established')
for _, row in ipairs(results) do
local season = row.season or ''
if row.basename ~= '$1' and row.basename ~= 'Creeper Cove' then
table.insert(out, '|-')
table.insert(out, string.format('| %s || [[%s]] || %s || %s || %s || %s',
season,
row.basename or '',
row.owner or '',
row.basetype or '',
row.location or '',
row.dateestablished or ''
))
end
end
table.insert(out, '|}')
return table.concat(out, '\n')
end
return p