Jump to content

Module:Sports rivalry series table/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 18:16, 12 February 2015. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{sports rivalry series table}}
local p = {}
local root

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function addheader()
	-- create the header row
	local row = root:tag('tr')
	row:tag('th'):wikitext('#')
	row:tag('th'):wikitext('Date')
	row:tag('th'):wikitext('Location')
	row:tag('th'):attr('colspan', 2):wikitext('Winning team')
	row:tag('th'):attr('colspan', 2):wikitext('Losing team')
	row:tag('th'):wikitext('Series')
end
	
function p.table(frame)
	local args = (frame.args[3] ~= nil) and frame.args or frame:getParent().args
	local team1style = args['team1style'] or ''
	local team1name = mw.ustring.gsub(args['team1'] or '', '^%s*(.-)%s*$', '%1')
	local team2style = args['team2style'] or ''
	local team2name = mw.ustring.gsub(args['team2'] or '', '^%s*(.-)%s*$', '%1')
	local team1wins = 0
	local team2wins = 0
	local ties = 0
 
	-- create the root table
	root = mw.html.create('table')
 
	-- add table classes
	root:addClass('wikitable')
	-- add table style
	root:css('text-align', 'center')
	root:cssText(args['style'])
	
	-- compute the maximum cell index
	local cellcount = 0
	for k, v in pairs( args ) do
		if type( k ) == 'number' then
			cellcount = math.max(cellcount, k)
		end
	end
	-- compute the number of rows
	local rows = math.ceil(cellcount / 6)

	-- add the header
	addheader()
	-- build the table content
	for j=1,rows do
		-- start a new row
		row = root:tag('tr')
		row:css('vertical-align', 'top')
		-- Number
		row:tag('td'):wikitext(j)
		-- Date
		row:tag('td'):wikitext(args[6*(j-1)+1] or '')
		-- Location
		row:tag('td'):wikitext(args[6*(j-1)+2] or '')
		-- Team1 / Team2 / Score1 / Score2
		local team1 = mw.ustring.gsub(args[6*(j-1)+3] or '', '^%s*(.-)%s*$', '%1')
		local score1 = mw.ustring.gsub(args[6*(j-1)+4] or '', '^%s*(.-)%s*$', '%1')
		local team2 = mw.ustring.gsub(args[6*(j-1)+5] or '', '^%s*(.-)%s*$', '%1')
		local score2 = mw.ustring.gsub(args[6*(j-1)+6] or '', '^%s*(.-)%s*$', '%1')

		local shade1 = nil
		local bold1 = nil
		local shade2 = nil
		local bold2 = nil
		if ( (tonumber(score1) or 0) > (tonumber(score2) or 0) ) then
			if( team1 == team1name ) then
				shade1 = team1style
				bold1 = 'bold'
				team1wins = team1wins + 1
			elseif (team1 == team2name) then
				shade1 = team2style
				bold2 = 'bold'
				team2wins = team2wins + 1
			end
		elseif ( (tonumber(score2) or 0) > (tonumber(score1) or 0) ) then
			if( team2 == team1name ) then
				shade1 = team1style
				bold1 = 'bold'
				team1wins = team1wins + 1
			elseif (team2 == team2name) then
				shade1 = team2style
				bold2 = 'bold'
				team2wins = team2wins + 1
			end
    	else
    		ties = ties + 1
    	end
    	-- Team 1
		row:tag('td')
			:cssText(shade1)
			:css('font-weight', bold1)
			:wikitext(team1)
		-- Team 1 score
		row:tag('td')
			:cssText(shade1)
			:css('font-weight', bold1)
			:wikitext(score1)
    	-- Team 2
		row:tag('td')
			:cssText(shade2)
			:css('font-weight', bold2)
			:wikitext(team2)
		-- Team 2 score
		row:tag('td')
			:cssText(shade2)
			:css('font-weight', bold2)
			:wikitext(score2)
		-- Series
		local seriescell = row:tag('td')
		if (team1wins > team2wins) then
			seriescell:wikitext(team1 .. ' ' .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '') )
		elseif (team2wins > team1wins) then
			seriescell:wikitext(team2 .. ' ' .. team2wins .. '–' .. team1wins .. ( (ties > 0) and '–' .. ties or '') )
		else
			seriescell:wikitext('Tied ' .. team1wins .. '–' .. team2wins .. ( (ties > 0) and '–' .. ties or '') )
		end
	end
	-- return the root table
	return tostring(root)
end
 
return p