Jump to content

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

Module:BaseListTable

From CrabCraft Wiki

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