Jump to content

📢 Please make sure to read our Style Guide before editing. For questions, suggestions, or technical issues, please contact Noah. ✖

Module:BaseListTable: Difference between revisions

From CrabCraft Wiki
created base list module
 
Adds roman numeral support
 
(6 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 = {
orderBy = 'season ASC, basename ASC',
limit = 1000
limit = 100 -- Optional; remove or increase if needed
}
}


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
table.insert(out, '|-')
local season = row.season or ''
table.insert(out, string.format('| %s || %s || %s || %s || %s || %s',
if row.basename ~= '$1' and row.basename ~= 'Creeper Cove' then
row.season or '',
table.insert(out, '|-')
row.basename or '',
table.insert(out, string.format('| %s || [[%s]] || %s || %s || %s || %s',
row.owner or '',
season,
row.basetype or '',
row.basename or '',
row.location or '',
row.owner or '',
row.dateestablished 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