Jump to content

Module:Invitational results box

From Wiki - Scioly.org

Documentation for this module may be created at Module:Invitational results box/doc

local p = {}
local getArgs = require("Module:Arguments").getArgs

local divisionArgKey = "div"
local columnArgKey = "col"
local defaultPlacementColCount = 5
local maxColCount = 6

function p.main(frame)
	local args = getArgs(frame)
	local wikitable = mw.html.create("table")
	wikitable:attr("class", "wikitable sortable")
	wikitable:css({
		["text-align"] = "center",
	})
	
	local startingYear = 2000
	local division = args[divisionArgKey]
	
	if division == nil then
		division = "X"
		mw.addWarning("No division was provided. This is a required argument, but not providing it will not cause the template to fail.")
	end
	local comment = mw.html.create("caption")
	comment:wikitext(string.format("Division %s Results", string.upper(division)))
	
	wikitable:node(comment)
	
	local headerRow = mw.html.create("tr")
	
	local placementColumnNames = {
		"Champion",
		"Runner-up",
		"Third Place",
		"Fourth Place",
		"Fifth Place",
		"Sixth Place",
	}
	local headerNames = {
		"Season",
	}
	
	local lastColumns = {
		"Date",
		"Teams",
	}
	
	local placementColumnCount = tonumber(args[columnArgKey]) or defaultPlacementColCount
	if placementColumnCount > maxColCount then
		error(string.format("Column count cannot be higher than %d", maxColumnCount))
	end

	for i = 1, placementColumnCount do
		table.insert(headerNames, placementColumnNames[i])
	end
	for _, v in ipairs(lastColumns) do
		table.insert(headerNames, v)
	end
	
	for _, name in ipairs(headerNames) do
		local headerCell = mw.html.create("th"):tag("big"):wikitext(name)
		headerCell = headerCell:allDone()
		headerRow:node(headerCell)
	end
	
	wikitable:node(headerRow)
	
	-- {
	--     ["year"] = number,
	--     ["html"] = string,
	-- }
	local years = {}
	
	-- {
	--	   ["order"] = number,
	--     ["text"] = string,
	-- }
	local notes = {}
	
	for k, v in pairs(args) do
		local canidateYear = tonumber(k)
		if canidateYear ~= nil then
			if canidateYear >= startingYear then
				table.insert(years, {
					["year"] = canidateYear,
					["html"] = v,
				})
			else
				mw.addWarning(string.format("Non-keyword or numeric key argument is less than %d", startingYear))
			end
		else
			local noteKeyPrefix = "note"
			if string.len(k) > string.len(noteKeyPrefix) then
				if string.sub(k, 1, string.len(noteKeyPrefix)) == noteKeyPrefix then
					local order = tonumber(string.sub(k, string.len(noteKeyPrefix) + 1))
					if order ~= nil then
						table.insert(notes, {
							["order"] = order,
							["text"] = v,
						})
					end
				end
			end
		end
	end
	
	table.sort(
		years,
		function(a, b)
			return a["year"] < b["year"]
		end
	)

	for _, v in ipairs(years) do
		-- TODO: Double check this
		local row = mw.html.create("tr")
		local yearCell = mw.html.create("td"):wikitext(string.format("'''%d'''", v["year"]))
		row:node(yearCell)
		row:wikitext(v["html"])
		wikitable:node(row)
	end
	
	table.sort(
		notes,
		function(a, b)
			return a["order"] < b["order"]
		end
	)
	
	if #notes == 0 then
		return wikitable
	else
		local notesDl = mw.html.create("dl")
		
		for _, v in ipairs(notes) do
			local dd = mw.html.create("dd")
			dd:node(mw.html.create("sup"):wikitext(v["order"]))
			dd:wikitext(v["text"])
			notesDl:node(dd)
		end
		return tostring(wikitable) .. tostring(notesDl)
	end
end
	
return p