Jump to content

Module:Sports games

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 01:12, 5 March 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Module to build Sports games table 
-- See documentation for details

require('strict')

local p = {}

-- Helper functions
local function get_team_label(team_abbr, team_name)
	if team_name == nil then
		team_name = team_abbr
	end
	local team_text = team_name
	if team_text:match('^%[%[[^%|%[%]]*%|([^%|%[%]]*)%]%]') then
		team_text = team_text:match('^%[%[[^%|%[%]]*%|([^%|%[%]]*)%]%]')
	end
	if team_text:match('^%[%[([^%|%[%]]*)%]%]') then
		team_text = team_text:match('^%[%[([^%|%[%]]*)%]%]')
	end
	local team_label = team_name
	if team_text ~= team_abbr then
		team_label = team_label .. '<br>(' .. team_abbr .. ')'
	end
	return team_label
end

local function get_score_background(s, c)
	local s1, s2
	-- Define the colouring
	local wc, lc, tc
	if c == 'level2' then
	wc, lc, tc = '#CCF9FF', '#FCC', '#FFC' -- blue2, red2, yellow2
	elseif c == 'level3' then
	wc, lc, tc = '#DDFCFF', '#FDD', '#FFD' -- blue3, red3, yellow3
	elseif c == 'level4' then
	wc, lc, tc = '#EEFFFF', '#FEE', '#FFE' -- blue4, red4, yellow4
	else
	wc, lc, tc = '#BBF3FF', '#FBB', '#FFB' -- blue1, red1, yellow1
	end

	-- check for override
	if s:match('^%s*<span%s+style%s*=["\'%s]*background[%-colr]*%s*:([^\'";<>]*).-$') then
		local c = mw.ustring.gsub(s,'^%s*<span%s+style%s*=["\'%s]*background[%-colr]*%s*:([^\'";<>]*).-$', '%1')
		return c
	end

	-- delink if necessary
	if s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]') then
		s = s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]')
	end
	if s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]') then
		s = s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]')
	end
	if s:match('<span[^<>]*>(.-)</span>') then
		s = s:match('<span[^<>]*>(.-)</span>')
	end

	-- get the scores
	s1 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d%.]+)%s*–%s*([%d%.]+).*', '%1' ) or '') or ''
	s2 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d%.]+)%s*–%s*([%d%.]+).*', '%2' ) or '') or ''

	-- return colouring if possible
	if s1 ~= '' and s2 ~= '' then
		return (s1 > s2) and wc or ((s2 > s1) and lc or tc)
	else
		return 'transparent'
	end
end

local function format_score(s)
	s = mw.ustring.gsub(s or '', '^%s*([%d%.]+)%s*[–−—%-]%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*([%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	return s
end

local function is_overtime(s)
	if s:match('%d+–%d+[%s%]]*%*') then
		return true
	end
	return false
end	

-- Main function
local DEBUG = ''
function p.table(frame)
	-- Get the args, stripping out blank values
	local getArgs = require('Module:Arguments').getArgs
	local Args = getArgs(frame, {parentFirst = true})

	-- Exit early if we are using section transclusion for a different section
	local tsection = frame:getParent().args['transcludesection'] or frame:getParent().args['section'] or ''
	local bsection = frame.args['section'] or ''
	if( tsection ~= '' and bsection ~= '' ) then
		if( tsection ~= bsection ) then
			return ''
		end
	end
	
	if Args['team_order'] and Args['team_order'] ~= '' then
		local tlist = mw.text.split(Args['team_order'], '%s*[;,]%s*')
		for k, tname in ipairs(tlist) do
			if tname ~= '' then
				Args['team' .. k] = tname
			end
		end
	end
	
	-- Build team list
	local team_list = {}
	local other_team_table = {}
	local k = 1
	while Args['team' .. k] do
		team_list[k] = Args['team' .. k]
		other_team_table[Args['team' .. k]] = {}
		k = k + 1
	end
	
	-- Store the results
	local total_games = 0
	for k,v in pairs(Args) do
		if v and v ~= '' and k:match('^m%d+_[^_]+_[^_]+$') then
			local g = k:match('^m(%d+)_[^_]+_[^_]+$')
			local t1 = k:match('^m%d+_([^_]+)_[^_]+$')
			local t2 = k:match('^m%d+_[^_]+_([^_]+)$')
			if other_team_table[t1] then
				other_team_table[t1][tonumber(g)] = t2
			else
				DEBUG = DEBUG .. ' ERROR ' .. t1
			end
			total_games = (tonumber(g) > total_games) and tonumber(g) or total_games
		end
	end

	-- Start table
	local t = mw.html.create('table')
		:addClass('wikitable')
		:addClass('plainrowheaders')
		:css('text-align', 'center')
	
	-- Table title
	if Args['title'] and Args['title'] ~= '' then
		t:tag('caption', Args['title'])
	end
	
	-- Header rows
	local row = t:tag('tr')
	row:tag('th')
		:attr('scope', 'col')
		:attr('rowspan', 2)
		:wikitext('Team')
	row:tag('th')
		:attr('scope', 'col')
		:attr('colspan', total_games)
		:wikitext('Game')
	row = t:tag('tr')
	for jj = 1,total_games do
		row:tag('th')
			:attr('scope', 'col')
			:wikitext(jj)
	end
	
	-- Results rows
	for ii=1,#team_list do
		local team_abbr = team_list[ii]
		local team_label = get_team_label(team_abbr, Args['name_' .. team_abbr])
		row = t:tag('tr')
		row:tag('th')
			:attr('scope', 'row')
			:css('background', 'inherit')
			:wikitext(team_label)
		for jj=1,total_games do
			local other_team_abbr = other_team_table[team_abbr][jj] or ''
			local result = ''
			if other_team_abbr ~= '' then
				result = Args['m' .. jj .. '_' .. team_abbr .. '_' .. other_team_abbr] or ''
			end
			local bg = get_score_background(result, is_overtime(result) and 'level3' or 'level1')
			if result ~= '' then
				result = other_team_abbr .. '<br>' .. format_score(result)
			end
			row:tag('td')
				:css('background', bg)
				:wikitext(result)
		end
	end
	
	if Args['source'] then
		t:tag('tr')
			:tag('td')
				:attr('colspan', total_games)
				:wikitext('Source: ' .. Args['source'])
	end

	return tostring(t) .. DEBUG
end

return p