Jump to content

Module:Irb row

From Wiki - Scioly.org

Documentation for this module may be created at Module:Irb row/doc

local p = {}
local getArgs = require("Module:Arguments").getArgs
local common = require("Module:Rb row/common")

local placementArgKeys = {
	{
		name = "first_name",
		points = "first_points",
		note = "first_note",
		override = "first",
	},
	{
		name = "second_name",
		points = "second_points",
		note = "second_note",
		override = "second",
	},
	{
		name = "third_name",
		points = "third_points",
		note = "third_note",
		override = "third",
	},
	{
		name = "fourth_name",
		points = "fourth_points",
		note = "fourth_note",
		override = "fourth",
	},
	{
		name = "fifth_name",
		points = "fifth_points",
		note = "fifth_note",
		override = "fifth",
	},
	{
		name = "sixth_name",
		points = "sixth_points",
		note = "sixth_note",
		override = "sixth",
	},
}

local dateArgKey = "date"
local teamCountArgKey = "teams"

local columnArgKey = "col"
local defaultColumnCount = 5
local maxColumnCount = #placementArgKeys

local conditionArgKey = "condition"
local condReasonArgKey = "condition_reason"
local eventCancelled = "cancelled"
local eventNotHeld = "notheld"

function p.main(frame)
	local args = getArgs(frame)
	
	local cells = {}
	local columnCount = tonumber(args[columnArgKey]) or defaultColumnCount
	
	if columnCount > maxColumnCount then
		error(string.format("Column count cannot be higher than %d", maxColumnCount))
	end
	
	if args[conditionArgKey] ~= nil then
		local conditionType = args[conditionArgKey]
		local conditionCell = common.generateConditionRow(conditionType, columnCount, true, eventCancelled, eventNotHeld, args[condReasonArgKey], nil)
		table.insert(cells, conditionCell)
	else
		for i = 1, columnCount do
			local keys = placementArgKeys[i]
			local cell = mw.html.create("td")
			if args[keys.override] ~= nil then
				cell:wikitext(args[keys.override])
			else
				if args[keys.name] ~= nil then
					cell:wikitext(args[keys.name])
				end
				if args[keys.note] ~= nil then
				    cell:node(mw.html.create("sup"):wikitext(args[keys.note]))
				end
				if args[keys.points] ~= nil then
				    cell:wikitext(string.format(" (%s)", args[keys.points])) -- point value is string so it's treated as such	
				end
			end
			table.insert(cells, cell)
		end
	end
	
	local dateCell = mw.html.create("td")
	
	if args[dateArgKey] ~= nil then
		dateCell:wikitext(args[dateArgKey])	
	end
	
	table.insert(cells, dateCell)
	
	local teamCountCell = mw.html.create("td")
	
	if args[teamCountArgKey] ~= nil then
		teamCountCell:wikitext(args[teamCountArgKey])	
	end
	
	table.insert(cells, teamCountCell)
	
	htmlStr = ""
	
	for _, v in ipairs(cells) do
		htmlStr = htmlStr .. tostring(v)
	end
	
	return htmlStr
end

return p