Jump to content

Module:Sports rbr table

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 14:17, 8 August 2018 (starting a module to replace the "fb rbr ..." templates; still under construction). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module implements {{Sports rbr table}}
local p = {}

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end

local args = nil

local color_map = {
		green1='#BBF3BB', green2='#CCF9CC', green3='#DDFCDD', green4='#EEFFEE',
		blue1='#BBF3FF', blue2='#CCF9FF', blue3='#DDFCFF', blue4='#EEFFFF',
		yellow1='#FFFFBB', yellow2='#FFFFCC', yellow3='#FFFFDD', yellow4='#FFFFEE',
		red1='#FFBBBB', red2='#FFCCCC', red3='#FFDDDD', red4='#FFEEEE',
		black1='#BBBBBB', black2='#CCCCCC', black3='#DDDDDD', black4='#EEEEEE',
		gold='#FFD700', silver='#C0C0C0', bronze='#CC9966'
	}

local function get_color(p)
	local c = args['color_' .. p]
	if c then
		return color_map[c] or c
	end
	p = tonumber(p or '0') or 0
	if p <= 0 then
		return nil
	end
	return nil
end	

function p.table(frame)
	args = getArgs(frame, {wrappers = {'Template:Sports rbr table'}})
	local rounds = tonumber(args['rounds'] or '0') or 0
	local firstround = tonumber(args['firstround'] or 1) or 1
	local sortable = args['sortable'] and true or false
	-- Adjust rounds
	rounds = rounds - (firstround - 1)
	
	-- Create the list of teams and positions
	local team_list = {}
	local pos_list = {}
	local teams = 0
	local maxrounds = 0
	while args['team' .. (teams+1)] ~= nil do
		teams = teams + 1
		team_list[teams] = args['team' .. teams]
		pos_list[teams] = mw.text.gsplit(args['pos' .. teams] or '', '%s*[/~]%s*')
		maxrounds = (#result_list[teams] > maxrounds) and #result_list[teams] or maxrounds
	end
	maxrounds = (rounds > maxrounds) and rounds or maxrounds

	local fs = 95
	if ((maxrounds - firstround) > 37 ) then
		fs = fs - 2*(maxrounds - firstround - 37)
	end

	-- Build the table
	local root = mw.html.create('table')
	root:addClass('wikitable')
	root:addClass(sortable and 'sortable' or nil)
	root:css('font-size', fs .. '%')
	root:css('text-align', 'center')
	
	-- Heading row
	local row = root:tag('tr')
	row:tag('th')
		:attr('rowspan', args['sortable'] and 2 or nil)
		:wikitext(args['header'] or 'Team ╲ Round')
	for r=1,maxrounds do
		row:tag('th')
			:css('width', '15px')
			:css('border-bottom', args['sortable'] and 'none' or nil)
			:wikitext(r + (firstround - 1))
	end
	if args['sortable'] then
		for r=1,maxrounds do
			row:tag('th')
				:css('height', '1.2ex')
				:css('border-top', 'none')
		end
	end
	
	-- Team positions
	for t=1,teams do
		row = root:tag('tr')
		row:tag('td'):wikitext(team_list[t])
		local pos = pos_list[t]
		for r=1,maxrounds do
			row:tag('td')
				:css('background-color', get_color(pos[r]))
				:wikitext(pos[r])
		end
	end
	
	return tostring(root)
end