Module:Sports table

This is an old revision of this page, as edited by CRwikiCA (talk | contribs) at 19:49, 15 September 2014 (Forgot one). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Module to build tables for standings in football
-- Undocumented sandbox version now

local p = {}
 
-- Main function
function p.main(frame)
	-- Declare locals
	local Args = frame.args
	local N_teams = 0
	local t = {}
	local team_list = {}
	
	-- Read in number of consecutive teams (ignore entries after skipping a spot)
	while Args['team'..N_teams+1] ~= nil do
		N_teams = N_teams+1
		-- Sneakily add it twice to the team_list parameter, once for the actual
		-- ranking, the second for position lookup in sub-tables
		team_list[N_teams] = Args['team'..N_teams] -- i^th entry is team X
		team_list[Args['team'..N_teams]] = N_teams -- team X entry is position i
	end
	
	-- Write column headers
	local team_width = Args['teamwidth'] or '190'
	table.insert(t,'{|class="wikitable" style="text-align:center;"\n')            		-- Open table
	table.insert(t,'! scope="col" width=28|<abbr title="Position">Pos</abbr>\n')		-- Position col
	table.insert(t,'! scope="col" width='..team_width..'|Team\n')						-- Team col
	table.insert(t,'! scope="col" width=28|<abbr title="Played">Pld</abbr>\n')			-- Games played col
	table.insert(t,'! scope="col" width=28|<abbr title="Won">W</abbr>\n')				-- Win col
	table.insert(t,'! scope="col" width=28|<abbr title="Drawn">D</abbr>\n')				-- Draw col
	table.insert(t,'! scope="col" width=28|<abbr title="Lost">L</abbr>\n')				-- Loss col
	table.insert(t,'! scope="col" width=28|<abbr title="Goals for">GF</abbr>\n')		-- Goal for col
	table.insert(t,'! scope="col" width=28|<abbr title="Goals against">GA</abbr>\n')	-- Goal against col
	table.insert(t,'! scope="col" width=28|<abbr title="Goal difference">GD</abbr>\n')	-- Goal difference col
	table.insert(t,'! scope="col" width=28|<abbr title="Points">Pts</abbr>\n')			-- Points col
	
	-- Write rows
	local games, wins, draws, losses, gfor, gaig, gdiff, points = 0
	for ii = 1, N_teams do
		-- First read values
		wins = tonumber(Args['win'..ii]) 	or 0
		draws = tonumber(Args['draw'..ii]) 	or 0
		losses = tonumber(Args['loss'..ii]) or 0
		gfor = tonumber(Args['gf'..ii]) 	or 0
		gaig = tonumber(Args['ga'..ii]) 	or 0
		-- Then calculate some values
		games = wins + draws + losses
		gdiff = gfor - gaig
		points = 3*wins + draws
		
		-- Now build the rows
		table.insert(t,'|-\n')																					-- New row
		table.insert(t,'! scope="row" style="text-align: center; background-color: transparent"|'..ii..'\n')	-- Position number
		table.insert(t,'| style="text-align: left; white-space:nowrap"|'..Args[team_list[ii]]..'\n')			-- Team
		table.insert(t,'|'..games..'\n') 																		-- Played
		table.insert(t,'|'..wins..'\n') 																		-- Won
		table.insert(t,'|'..draws..'\n') 																		-- Drawn
		table.insert(t,'|'..losses..'\n') 																		-- Lost
		table.insert(t,'|'..gfor..'\n') 																		-- GF
		table.insert(t,'|'..gaig..'\n')												 							-- GA
		table.insert(t,'|'..gdiff..'\n') 																		-- GD
		table.insert(t,'| style="font-weight: bold;" |'..points..'\n') 											-- Points
	end
	
	
	-- Close table
	table.insert(t, '|}')
	
	-- Small text, footnotes
	table.insert(t, '<small>RESERVED TEXT</small>\n')
	
	
	-- START TEST CODE
	table.insert(t, team_list[N_teams]..'='..Args[team_list[N_teams]])
	local test1 = Args['test1'] or 'TEST_VAR'
	table.insert(t, '<br>'..test1..'<br>')
	table.insert(t, Args['team1']..'\n')
	table.insert(t, 'Test line just before return statement')
	-- END TEST CODE
	return table.concat(t)
end
 
return p