Jump to content

Module:Sports table/Custom/sandbox

From Wikipedia, the free encyclopedia
-- Style for sports tables with custom columns
local pp = {}

function pp.get_col_styles(Args)
	-- Load relevant modules
	local yesno = require('Module:Yesno')

	local col_style = {}
	local num_cols = 1
	-- Default styles
	local default_style = Args['default_style'] or ''
	if default_style:match('^([Ww][Dd]?[Ll])') then
		-- Backwards compatibility with WDL
		if not yesno(Args['hide_played'] or 'no') then
			col_style[num_cols] = 'pld'
			num_cols = num_cols +1
		end
		if yesno(Args['show_win'] or 'yes') then
			col_style[num_cols] = 'w'
			num_cols = num_cols +1
		end
		local loss_first = yesno(Args['loss_before_draw'] or Args['loss_before_tie'] or false)
		if loss_first and yesno(Args['show_loss'] or 'yes') then
			col_style[num_cols] = 'l'
			num_cols = num_cols +1
		end
		if yesno(Args['show_draw'] or default_style:match('^[Ww]([Dd])[Ll]')~=nil) then
			col_style[num_cols] = 'd'
			num_cols = num_cols +1
		end
		if (not loss_first) and yesno(Args['show_loss'] or 'yes') then
			col_style[num_cols] = 'l'
			num_cols = num_cols +1
		end
		local fa_letter = string.lower(Args['for_against_style'] or '')
		if fa_letter ~= '' then
			if fa_letter=='game' or fa_letter=='games' then
				fa_letter = 'game'
			elseif fa_letter == 'none' then
				fa_letter = nil
			else
				fa_letter = fa_letter:match('^(%a).*')
			end
		else
			fa_letter = default_style:match('^[Ww][Dd]?[Ll](%a)fa')
					or default_style:match('^[Ww][Dd]?[Ll](%a)diff')
		end
		if fa_letter ~= nil then
			local fa_word_sing, fa_word_plur
			if fa_letter == 'game' then
				fa_letter = 'g'
				fa_word_sing = 'Game'
				fa_word_plur = 'Games'
			elseif fa_letter == 'f' then
				fa_word_sing = 'Frame'
				fa_word_plur = 'Frames'
			elseif fa_letter == 'm' then
				fa_word_sing = 'Match'
				fa_word_plur = 'Matches'
			elseif fa_letter == 'p' then
				fa_word_sing = 'Point'
				fa_word_plur = 'Points'
			elseif fa_letter == 'r' then
				fa_word_sing = 'Run'
				fa_word_plur = 'Runs'
			elseif fa_letter == 's' then
				fa_word_sing = 'Set'
				fa_word_plur = 'Sets'
			else
				fa_letter = 'g'
				fa_word_sing = 'Goal'
				fa_word_plur = 'Goals'
			end
			col_style[num_cols] = fa_letter..'f'
			col_style[num_cols+1] = fa_letter..'a'
			col_style[num_cols+2] = 'diff_'..fa_letter..'f_'..fa_letter..'a'
			fa_letter = string.upper(fa_letter)
			Args['col_header'..num_cols] = Args['col_header'..num_cols] or
					'<abbr title="'..fa_word_plur..' for">'..fa_letter..'F</abbr>'
			num_cols = num_cols + 1
			Args['col_header'..num_cols] = Args['col_header'..num_cols] or
					'<abbr title="'..fa_word_plur..' against">'..fa_letter..'A</abbr>'
			num_cols = num_cols + 1
			Args['col_header'..num_cols] = Args['col_header'..num_cols] or
					'<abbr title="'..fa_word_sing..' difference">'..fa_letter..'D</abbr>'
			num_cols = num_cols + 1
		end
		col_style[num_cols] = 'pts'
		col_style['ranking_col'] = num_cols
	end
	-- Number of columns and custom column styles
	num_cols = tonumber(Args['num_cols']) or num_cols
	for cc=1,num_cols do
		col_style[cc] = Args['colstyle'..cc] or col_style[cc] or ''
	end
	-- Store num_cols so it may be returned
	col_style['num_cols'] = num_cols
	return col_style
end

function pp.header(t,Args,p_sub,pos_label,group_col,VTE_text,full_table,results_header_txt)
	-- Load relevant modules
	local yesno = require('Module:Yesno')

	-- Create table header
	local sort_text = yesno(Args['sortable_table'] or 'no') and ' sortable' or ''
	table.insert(t,'{| class="wikitable'..sort_text..'" style="text-align:center;"\n')            			-- Open table
	-- Table title
	if Args['title'] or Args['table_header'] then
		table.insert(t,'|+ ' .. (Args['title'] or Args['table_header']) .. '\n')
	end
	-- Pre stuff
	local team_width = Args['teamwidth'] or '190'
	local col_style = pp.get_col_styles(Args)
	local num_cols = col_style['num_cols']
	local ranking_col = tonumber(Args['ranking_col']) or col_style['ranking_col'] or num_cols
	-- Custom header options
	local group_head_text = Args['group_header'] or '<abbr title="Group">Grp</abbr>'
	local team_head_text = Args['team_header'] or 'Team'

	-- Initialize
	local tt = {}
	tt.count = 0 		-- Up by one after every call
	tt.tab_text = t		-- Actual text
	-- Actual headers
	if pos_label ~= nil then
		tt = p_sub.colhead(tt,28,pos_label)									-- Position col
	end
	-- Add group header
	if full_table and group_col then
		tt = p_sub.colhead(tt,28,group_head_text)							-- Group col
	end
	tt = p_sub.colhead(tt,team_width,team_head_text..VTE_text)				-- Team col
	for cc=1,num_cols do
		local stylecc = col_style[cc]
		local col_width = 28
		local always_show = (ranking_col == cc)
		local head_text = '<abbr title="Column '..cc..'">'..cc..'</abbr>'	-- Column cc default
		-- Column style head text
		if stylecc == 'pld' or stylecc == 'played' or stylecc == 'match' or stylecc == 'matches' then
			head_text = Args['pld_header'] or '<abbr title="Played">Pld</abbr>'
			always_show = true
		elseif stylecc == 'w' or stylecc == 'win' or stylecc == 'wins' then
			head_text = '<abbr title="Won">W</abbr>'
		elseif stylecc == 'd' or stylecc == 'draw' or stylecc == 'draws' then
			head_text = Args['draw_header'] or (yesno(Args['use_tie'] or 'no') and '<abbr title="Tied">T</abbr>') or '<abbr title="Drawn">D</abbr>'
		elseif stylecc == 'l' or stylecc == 'loss' or stylecc == 'losses' then
			head_text = '<abbr title="Lost">L</abbr>'
		elseif stylecc == 'rw' then
			head_text = Args['rw_header'] or '<abbr title="Rounds won">RW</abbr>'
		elseif stylecc == 'pts' or stylecc == 'points' then
			head_text = '<abbr title="Points">Pts</abbr>'
			always_show = true
		elseif stylecc == 'pct' or stylecc == 'percentage' then
			head_text = '<abbr title="Win percentage">PCT</abbr>'
			always_show = true
		elseif stylecc == 'ppg' then
			head_text = '<abbr title="Points per game">PPG</abbr>'
			always_show = true
		elseif stylecc == 'ppga' then
			head_text = '<abbr title="Points per game">PPG</abbr>'
			always_show = true
			col_width = 36
		elseif stylecc ~= '' and stylecc:len() <= 3 then
			head_text = string.upper(stylecc)
		end
		if full_table or always_show then
			head_text = Args['col_header'..cc] or head_text
			tt = p_sub.colhead(tt,col_width,head_text)
		end
	end
	if full_table then
		tt.count = tt.count+1
		table.insert(tt.tab_text,results_header_txt)
	end

	return tt
end

function pp.row(frame,t,Args,p_sub,notes_exist,hth_id_list,full_table,rand_val,team_list,team_code_ii,ii_start,ii_end,ii_fw,bg_col,N_teams,ii,ii_show)
	-- Build the inner parts of individual rows
	
	-- Sub-module usage
	local mm = require('Module:Math')
	local yesno = require('Module:Yesno')
	
	-- Get custom/default options for in table
	local col_style = pp.get_col_styles(Args)
	local num_cols = col_style['num_cols']
	local default_style = Args['default_style'] or ''
	local ranking_col = tonumber(Args['ranking_col']) or col_style['ranking_col'] or num_cols
	local win_points = tonumber(Args['winpoints'])			or 3
	local draw_points = tonumber(Args['drawpoints'])		or 1
	local loss_points = tonumber(Args['losspoints'])		or 0
	local rw_points = tonumber(Args['rwpoints'])			or 1
	
	-- Get some input
	local wins = tonumber(Args['win_'..team_code_ii])		or 0
	local draws = tonumber(Args['draw_'..team_code_ii])		or 0
	local losses = tonumber(Args['loss_'..team_code_ii])	or 0
	local rw = tonumber(Args['rw_'..team_code_ii])			or 0
	local hth_local = yesno(Args['show_hth'] or 'yes') and Args['hth_'..team_code_ii] or nil
	-- Then calculate some values
	local matches = wins + draws + losses
	local matches_text = matches
	if tonumber(Args['win_'..team_code_ii]) == nil or tonumber(Args['draw_'..team_code_ii]) == nil or tonumber(Args['loss_'..team_code_ii]) == nil then
		matches = tonumber(Args['matches_'..team_code_ii]) or matches
		matches_text = Args['matches_'..team_code_ii] or matches
	end
	local s_pts = (tonumber(Args['adjust_points_'..team_code_ii]) or 0) + (tonumber(Args['startpoints_'..team_code_ii]) or 0)
	local points = win_points*wins + draw_points*draws + loss_points*losses + rw_points*rw + s_pts
	local pct = '' 
	if matches == 0 then 
		-- Escape for zero matches
		pct = '&mdash;'
	else
		pct = mm._precision_format(wins / matches, 3)
	end
	local ppg = ''
	if matches == 0 then 
		-- Escape for zero matches
		ppg = '&mdash;'
	else
		ppg = mm._precision_format(points / matches, 2)
	end
	-- Use actual values if wins, draws, and losses are not numeric
	if (wins == 0 and draws == 0 and losses == 0) then
		local function numorval(s)
			return s and ((s == '' and 0) or tonumber(s) or s) or 0
		end
		wins = numorval(Args['win_'..team_code_ii])
		draws = numorval(Args['draw_'..team_code_ii])
		losses = numorval(Args['loss_'..team_code_ii])
	end
	local ppga = ''
	if matches == 0 then 
		-- Escape for zero matches
		ppga = '&mdash;'
	else
		ppga = mm._precision_format(points / matches, 3)
	end

	-- Some local vars	
	local hth_string
	local tt_return = p_sub.hth(frame,Args,full_table,hth_id_list,hth_local,notes_exist,team_list,team_code_ii,ii_start,ii_end,rand_val)
	hth_string = tt_return.str
	hth_id_list = tt_return.list
	notes_exist = tt_return.notes_exist

	-- Row building
	for cc=1,num_cols do
		local stylecc = col_style[cc]
		local col_fw = ii_fw
		local always_show = (ranking_col == cc)
		local cell_data = Args['default_value'..cc] or '&nbsp;&nbsp;'
		-- Get data by column style
		if stylecc == 'pld' or stylecc == 'played' or stylecc == 'match' or stylecc == 'matches' then
			cell_data = matches_text
			always_show = true
		elseif stylecc == 'w' or stylecc == 'win' or stylecc == 'wins' then
			cell_data = wins
		elseif stylecc == 'd' or stylecc == 'draw' or stylecc == 'draws' then
			cell_data = draws
		elseif stylecc == 'l' or stylecc == 'loss' or stylecc == 'losses' then
			cell_data = losses
		elseif stylecc == 'rw' then
			cell_data = rw
		elseif stylecc == 'pts' or stylecc == 'points' then
			if points<0 then
				cell_data = '&minus;'..-points
			else
				cell_data = points
			end
			always_show = true
		elseif stylecc == 'pct' or stylecc == 'percentage' then
			cell_data = pct
			always_show = true
		elseif stylecc == 'ppg' then
			cell_data = ppg
			always_show = true
		elseif stylecc == 'ppga' then
			cell_data = ppga
			always_show = true
		elseif default_style:match('^([Ww][Ll])')
			and (stylecc == 'perc' or stylecc == 'percentage' or stylecc == '%') then
				if matches == 0 then
					cell_data = '&mdash;'
				else
					cell_data = mm._precision_format(wins / matches, 3)
					if losses > 0 then
						-- Drop the leading zero (from the string)
						cell_data = string.sub(cell_data,2,string.len(cell_data))
					end
				end
		elseif stylecc:match('^(sum_[^_]*_[^_]*)')
			or stylecc:match('^(diff_[^_]*_[^_]*)')
			or stylecc:match('^(ratio_[^_]*_[^_]*)') then
			local op_str = stylecc:match('^([^_]*)_[^_]*_[^_]*')
			local operands = {}
			operands[1] = stylecc:match(op_str..'_([^_]*)_[^_]*')
			operands[2] = stylecc:match(op_str..'_[^_]*_([^_]*)')
			local flag_format = false
			for opn=1,2 do
				local opstyle = operands[opn]
				if opstyle == 'pld' or opstyle == 'played' or opstyle == 'match' or opstyle == 'matches' then
					operands[opn] = matches
				elseif opstyle == 'w' or opstyle == 'win' or opstyle == 'wins' then
					operands[opn] = wins
				elseif opstyle == 'd' or opstyle == 'draw' or opstyle == 'draws' then
					operands[opn] = draws
				elseif opstyle == 'l' or opstyle == 'loss' or opstyle == 'losses' then
					operands[opn] = losses
				elseif opstyle == 'rw' then
					operands[opn] = rw
				elseif opstyle == 'pts' or opstyle == 'points' then
					operands[opn] = points
				elseif opstyle == 'pct' or opstyle == 'percentage' then
					operands[opn] = pct
				elseif opstyle == 'ppg' then
					operands[opn] = points / matches
					flag_format = true
				elseif opstyle == 'ppga' then
					operands[opn] = points / matches
					flag_format = true
				else
					operands[opn] = tonumber(Args[opstyle..'_'..team_code_ii]) or 0
				end
			end
			if op_str == 'sum' then
				cell_data = operands[1] + operands[2]
			elseif op_str == 'ratio' then
				if operands[2] == 0 then
					cell_data = operands[1] == 0 and '&mdash;' or 'MAX'
				else
					cell_data = mm._precision_format(operands[1] / operands[2], 3)
				end
			else
				cell_data = operands[1] - operands[2]
				if not flag_format then
					if cell_data < 0 then
						cell_data = '&minus;'..-cell_data
					elseif cell_data > 0 and yesno(Args['plus_sign_diffs']) then
						cell_data = '&plus;'..cell_data
					end
				end
			end
			if flag_format then
				cell_data = mm._precision_format(cell_data, 2)
			end
		elseif stylecc ~= '' then
			cell_data = Args[stylecc..'_'..team_code_ii] or cell_data
		end
		cell_data = Args['col'..cc..'_'..team_code_ii] or cell_data
		if full_table or always_show then
			local align_col = Args['aligncol'..cc] or ''
			if ranking_col == cc then
				col_fw = 'font-weight: bold;'
				cell_data = cell_data .. hth_string
			elseif yesno(Args['bold_col'..cc]) then
				col_fw = 'font-weight: bold;'
			end
			-- sanitize the align_col input
			if align_col == 'l' or align_col == 'left' then
				align_col = 'text-align: left; '
			elseif align_col == 'r' or align_col == 'right' then
				align_col = 'text-align: right; '
			elseif align_col == 'c' or align_col == 'center' then
				align_col = 'text-align: center; '
			elseif align_col == 'justify' or align_col == 'initial' or align_col == 'inherit' then
				align_col = 'text-align: '..align_col..'; '
			else
				align_col = ''
			end
			table.insert(t,'| style="'..align_col..col_fw..bg_col..'" |'..cell_data..'\n')
		end
	end
	return {t=t, notes_exist=notes_exist, hth_id_list=hth_id_list}
end

function pp.status(Args)
	-- Declare status options
	-- ------------------------------------------------------------
	-- NOTE: If you add to status_code, also add to status_called and status_letters!!
	-- Or functionality will be compromised
	-- ------------------------------------------------------------
	local status_code, status_called = {}, {}
	status_code = {	A='Advance to a further round', C='Champion', D='Disqualified', 
		E='Eliminated', G='Guest', H='Host', O='Play-off winner', P='Promoted', Q='Qualified to the phase indicated',
		R='Relegated', T='Qualified, but not yet to the particular phase indicated'}
	local status_letters = (Args['status_order'] or '') .. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	
	-- Status position (before or after read and default)
	local stat_pos_val = string.lower(Args['status_pos'] or '')
	local status_position = 'after' 	-- Default location
	if stat_pos_val=='before' then
		status_position = 'before'
	elseif stat_pos_val=='after' then
		status_position = 'after'
	end
	-- Read in custom status options
	for l in mw.text.gsplit(status_letters, '') do
		status_called[l] = false
		status_code[l] = status_code[l] or '?'
		status_letters = mw.ustring.gsub(status_letters, '(' .. l ..'.-)' .. l, '%1')

		if Args['status_text_' .. l] then
			status_code[l] = Args['status_text_' .. l]
		end
	 end
	
	return {code=status_code, called=status_called, letters=status_letters, position=status_position}
end

return pp