Jump to content

Module:Sports results/Chess

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by NHammen (talk | contribs) at 04:29, 10 April 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Module to build results cross-tables for standings in Chess
-- See documentation for details

require('Module:No globals')

local p = {}

-- Other functions
local function get_short_name(s, t, n, ss)
	-- return short name if defined
	if s and s ~= '' then
		return s
	end
	-- deflag if necessary
	if ss and n then
		if ss == 'noflag' then
			n = mw.ustring.gsub(n, '%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*%]%]', '')
		elseif ss == 'flag' then
			n = mw.ustring.gsub(n, '(<span class="flagicon">%s*%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*link=)[^%|%[%]]*(%]%][^<>]*</span>)%s*%[%[([^%[%]%|]*)%|[^%[%]]*%]%]', '%1%3%2')
			n = mw.ustring.gsub(n, '(<span class="flagicon">%s*%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*%]%][^<>]*</span>).*', '%1')
			n = mw.ustring.gsub(n, '&nbsp;(</span>)', '%1')
		end
	end
	
	-- replace link text in name with team abbr if possible
	if n and t and n:match('(%[%[[^%[%]]*%]%])') then
		n = mw.ustring.gsub(n, '(%[%[[^%|%]]*%|)[^%|%]]*(%]%])', '%1' .. t .. '%2')
		n = mw.ustring.gsub(n, '(%[%[[^%|%]]*)(%]%])', '%1|' .. t .. '%2')
		return n
	end
	-- nothing worked, so just return the unlinked team abbr
	return t or ''
end

function p.header(tt,Args,p_sub,N_teams,team_list,legs,multirowlegs)
	local ii, team_code_ii, short_name
	legs = tonumber(Args['legs']) or 1

	-- Set match column width
	local col_width = Args['match_col_width'] or '28'
	local cs = multirowlegs and '2' or legs
	col_width = col_width .. ' colspan="' .. cs .. '" style="border-left:2px solid #aaa;"'

	-- Get some default values in case it doesn't start at 1
	local top_pos = tonumber(Args['highest_pos']) or 1

	for ii=top_pos,N_teams do
		team_code_ii = team_list[ii]
		short_name = get_short_name(Args['short_'..team_code_ii],
			team_code_ii, Args['name_'..team_code_ii], Args['short_style'] or '')
		tt = p_sub.colhead(tt,col_width,short_name)
	end
	return tt
end

function p.row(tt,Args,N_teams,team_list,ii,ii_show,legs,multirowlegs)
	-- Note ii is the row number being shown
	local jj, fw, bg, result, result_extra, team_code_ii, team_code_jj
	legs = tonumber(Args['legs']) or 1

	team_code_ii = team_list[ii]

	-- Get some default values in case it doesn't start at 1
	local top_pos = tonumber(Args['highest_pos']) or 1
	for jj=top_pos,N_teams do
		for l=1,legs do
			if multirowlegs and l > 2 and l % 2 == 1 then
				table.insert(tt,'|- \n')  -- New row
			end
			team_code_jj = team_list[jj]
			local m = (legs == 1) and 'match_' or 'match' .. l .. '_'
			result = Args[m..team_code_ii..'_'..team_code_jj] or ''
			result_extra = Args['result_'..team_code_ii..'_'..team_code_jj] or ''
			local bl = (l == 1 or multirowlegs and l % 2 == 1) and 'border-left:2px solid #aaa;' or ''

			if ii == jj or result == 'null' then
				local cs = multirowlegs and '2' or legs
				if ii ~= jj then
					cs = '1'
				end
				-- Solid cell
				fw = 'font-weight:' .. (ii==ii_show and 'bold' or 'normal') .. ';'
				bg = 'background:transparent;'

				-- Grey background color for solid cell
				local solid_cell = Args['solid_cell'] or ''
				if solid_cell ~= 'gray' and solid_cell ~= 'grey' and solid_cell ~= 'lightgray' and solid_cell ~= 'lightgrey' and solid_cell ~= 'silver' then
					solid_cell = mw.ustring.match(solid_cell,'^#%x%x%x+') or ''
				end
				if solid_cell ~= '' then
					table.insert(tt,'| colspan ="'..cs..'" style="'..fw..bl..'background:'..solid_cell..';" |\n')
				else
					table.insert(tt,'| colspan ="'..cs..'" style="'..fw..bl..bg..'" | &mdash;\n')
				end
				if ii == jj then
					break
				end
			else
				-- Content cell
				-- Set bolding and background
				fw = 'font-weight:' .. ((ii==ii_show or jj == ii_show) and 'bold' or 'normal') .. ';'
				bg = 'background:transparent;'

				-- Background coloring if enabled
				if l % 2 == 0 then
					bg = 'background:black; color:white';
				end
				table.insert(tt,'| style="white-space:nowrap;'..fw..bl..bg..'" |'..result..'\n')
			end
		end
	end
	
	return tt
end

return p