Benutzer:GregorBungensheim/Spielwiese/Modul:Test

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
-- bitte keine unabgesprochenen Änderungen auf der Spielwiese
-- Syntax-Highlighting verträgt sich aktuell nicht mit der Vorlagenspielwiese!

local function sb_nr(year)
	local nr = tonumber(year - 1965)
	local sb = ''
	local roman = require('Modul:FormatNum').roman
	if nr == 50 then
		sb = '50'
	elseif nr > 0 and nr < 100 then
		local fr = {}
		fr.args = {nr, ''}		
		sb = roman(fr)
	end
	return sb
end

local function is_wdl(score)
	local hst = mw.ustring.match(score,"^%s*(%d+)")
	local ast = mw.ustring.match(score,"(%d+)%**%s*$")
	local res
	if hst == nil or ast == nil then
		res = 'n'
	else
		local hsc, asc = tonumber(hst), tonumber(ast)
		if hsc > asc then res = 'w' elseif hsc < asc then res = 'l' else res = 'd' end
	end
	return res
end

local function get_score(args, hkey, akey)
	local score = nil
	if hkey ~= nil and akey ~= nil and hkey ~= '' and akey ~= '' then 
		score = args[hkey..'-'..akey]
	end
	if score ~= nil and mw.ustring.match(score, "^%s*%**:?%s*%**$") then score = nil end
	return score
end

local function box(seed, name, score, top, bold)
	local ret = {}
	local sty0 = '|rowspan="2" style="background:#'
	local sty1 = '; border-style:solid; border-color:#AAAAAA; '

	table.insert(ret, sty0 .. 'F0F0F0; border-width:' .. top .. ' 0 1px 1px' .. sty1 .. 'text-align:center;"| ' .. seed .. '\n')
	table.insert(ret, sty0 .. 'FAFAFA; border-width:' .. top .. ' 1px 1px 1px' .. sty1 .. 'padding-left:0.4em;'..bold..'"| ' .. name .. '\n')
	table.insert(ret, sty0 .. 'FAFAFA; border-width:' .. top .. ' 1px 1px 0' .. sty1 .. 'text-align:center;'..bold..'"| ' .. score .. '\n')

	return table.concat(ret)
end

local function build_box(key, okey, ttable, score, pattern, top, bold)
	local seed, name = '', ''
	if key ~= nil then
		seed = ttable[key .. 's']
		if seed == nil then seed = '' end
		name = ttable[key .. 'n']
		if name == nil then name = '' end
	end
	score = mw.ustring.match(score, pattern)
	return box(seed, name, score, top, bold)
end

local function insert_box(content, r, c, hk, ak, ttable, args)
	local score = get_score(args, hk, ak)
	if score == nil then score = '' end
	local bold = ''
	if is_wdl(score)=='w' then bold='font-weight:bold;' end
	content[c][r] = build_box(hk, ak, ttable, score, '^%s*(%d*%**)', '1px', bold)
	if is_wdl(score)=='l' then bold='font-weight:bold;' else bold='' end
	content[c][r+2] = build_box(ak, hk, ttable, score, '(%d*%**)%s*$', '0', bold)
	if ak == nil then ak = '' end 
	if hk ~= nil and args[hk..'-'..ak..'_det'] ~= nil then
		content[c][r-3] = '|rowspan="3" colspan="3" style="font-size:92.5%; vertical-align:bottom"| '..args[hk..'-'..ak..'_det']
	end	
end

local p = {}

function p.bracket(frame)
	local keys = {}
	local ttable = {}
	local year = frame.args['jahr']
	if year == nil then year = 0 end

	for j, c in ipairs({'a', 'n'}) do
		local team = {}
		for i = 1, 6 do
			if frame.args['team_'..c..i] ~= nil then
				team[i] = frame.args['team_'..c..i]
				ttable[team[i]..'n'] = frame.args[team[i]]
				if ttable[team[i]..'n'] == nil then
					ttable[team[i]..'n'] = team[i]
				end
			else
				team[i] = 'T'..i
			end
			ttable[team[i]..'s'] = i
		end
		local score = get_score(frame.args, team[4], team[5])
		if score ~= nil then
			if is_wdl(score) == 'l' then
				team[8] = team[5]
			elseif is_wdl(score) == 'w' then
				team[8] = team[4]
			end
		end
		score = get_score(frame.args, team[3], team[6])
		if score ~= nil then
			if is_wdl(score) == 'l' then
				team[3], team[4], team[5], team[6], team[7], team[8] = team[4], team[3], team[6], team[5], team[8], team[6]
			elseif is_wdl(score) == 'w' then
				team[7] = team[3]
			end
		else
			team[8] = nil
		end
		score = get_score(frame.args, team[2], team[7])
		if score ~= nil then
			if is_wdl(score) == 'l' then
				team[10] = team[7]
			elseif is_wdl(score) == 'w' then
				team[10] = team[2]
			end
		end
		score = get_score(frame.args, team[1], team[8])
		if score ~= nil then
			if is_wdl(score) == 'l' then
				team[1], team[2], team[3], team[4], team[5], team[6], team[7], team[8], team[9], team[10] = team[2], team[1], team[4], team[3], team[6], team[5], team[8], team[7], team[10], team[8]
			elseif is_wdl(score) == 'w' then
				team[9] = team[1]
			end
		end
		score = get_score(frame.args, team[9], team[10])
		if score ~= nil then
			if is_wdl(score) == 'l' then
				team[11] = team[10]
			elseif is_wdl(score) == 'w' then
				team[11] = team[9]
			end
		end
		keys[j] = team
	end
	return p.tree(ttable, keys, ttable, frame.args, year)
end

function p.tree(ttable, keys, ttable, args, year)
	local tree = p.init({'Wild Card Round', 'Divisional Round', 'Conference Championships', 'Super Bowl'})
	tree = p.body(tree, keys, ttable, args, year)
	table.insert(tree, '|}')

	return table.concat(tree)
end

function p.init(rounds)
	local r_names = {}
	if type(rounds) == "number" then
		for i = 1, rounds do
			r_names[i] = 'Runde ' .. i
		end
	else
		r_names = rounds
	end

	-- Initialize
	local tab_text = {}
	table.insert(tab_text, '{| border="0" cellpadding="0" cellspacing="0" style="font-size:90%; margin:1em 2em 1em 1em;"\n')	
	

	local style = '| ||colspan="3" style="background:#F0F0F0; border:1px solid #AAAAAA; text-align:center;"'
	local s = '|-\n'
	for i, round in ipairs(r_names) do
		table.insert(tab_text, s .. style .. '| ' .. round)
		s = ' ||\n'
	end
	table.insert(tab_text, '\n|-\n|style="height:0.5ex;"|')
	s = '\n'
	for i in pairs(r_names) do
		table.insert(tab_text, s .. '|style="width:1.7em;"| ||style="width:13em;"| ||style="width:2.5em;"|\n')
		s = '|style="width:0.8em;"| ||style="width:0.8em;"|\n'
	end
	return tab_text
end

function p.body(tree, keys, ttable, args, year)
	local tree_build = tree
	
	local val = {}
	val[1] = {'AFC', 'AA0000', 'dotted', '0 2px', ''} 
	val[2] = {'NFC', '0000AA', 'solid', '1px 0', 'border-top:1px solid;'}
	
	local c = {1, 2}
	if math.fmod(year, 2) == 0 then c = {2, 1} end
	
	local content = {}
	for i = 1, 10 do
		content[i] = {}
	end
	for i = 1, 2 do	
		local d = 1
		if keys[i][7] ~= nil or keys[i][8] ~= nil then d = 2 end

		local j = c[i] * 20 - 18
		content[1][j-1] = '|colspan="3"|'
		content[1][j] = '|colspan="3" rowspan="3"|'
		insert_box(content, 3+j, 1, keys[i][4], keys[i][5], ttable, args)
		content[1][7+j] = '|colspan="3"|'
		content[1][8+j] = '|colspan="3" rowspan="2" style="vertical-align:center; font-weight:bold; color:#' .. val[i][2] .. '"| ' .. val[i][1]
		content[1][10+j] = '|colspan="3" rowspan="3"|'
		insert_box(content, 13+j, 1, keys[i][3], keys[i][6], ttable, args)
		content[1][17+j] = '|colspan="3" rowspan="2"|'

		content[2][j-1] = '|rowspan="6" style="border-bottom:2px ' .. val[d][3] .. '"|'
		content[2][5+j] = '|rowspan="10" style="border-width:' .. val[d][4] .. ' 2px 0; border-style:' .. val[d][3] .. '"|'
		content[2][15+j] = '|rowspan="4" style="' .. val[d][5] .. '"|'

		content[3][j-1] = '|rowspan="6" style="border-bottom:2px solid"|'
		content[3][5+j] = '|rowspan="10" style="' .. val[d][5] .. ' border-bottom:2px solid"|'
		content[3][15+j] = '|rowspan="4" style="' .. val[d][5] .. '"|'

		content[4][j-1] = '|colspan="3" rowspan="3"|'
		insert_box(content, 2+j, 4, keys[i][1], keys[i][8], ttable, args)
		content[4][6+j] = '|colspan="3" rowspan="3"|'
		content[4][9+j] = '|colspan="3" rowspan="3"|'
		insert_box(content, 12+j, 4, keys[i][2], keys[i][7], ttable, args)
		content[4][16+j] = '|colspan="3" rowspan="3"|'

		content[5][j-1] = '|rowspan="5" style="border-bottom:2px solid"|'
		content[5][4+j] = '|rowspan="10" style="border-width:1px 3px 2px 0; border-style:solid"|'
		content[5][14+j] = '|rowspan="5" style="border-top:1px solid"|'

		content[6][j-1] = '|rowspan="5"|'
		content[6][4+j] = '|rowspan="5" style="border-bottom:2px solid"|'
		content[6][9+j] = '|rowspan="5" style="border-top:1px solid"|'
		content[6][14+j] = '|rowspan="5"|'

		content[7][j-1] = '|colspan="3" rowspan="5"|'
		content[7][4+j] = '|colspan="3" rowspan="3"|'
		insert_box(content, 7+j, 7, keys[i][9], keys[i][10], ttable, args)
		content[7][11+j] = '|colspan="3" rowspan="8" style="text-align:center; vertical-align:top; font-weight:bold"| ' .. val[i][1] .. ' Championship'
	end
	content[8][8] = '|rowspan="2" style="border-bottom:2px solid"|'
	content[8][10] = '|rowspan="20" style="border-width:1px 3px 2px 0; border-style:solid"|'
	content[8][30] = '|style="border-top:1px solid"|'

	content[9][10] = '|rowspan="10" style="border-bottom:2px solid"|'
	content[9][20] = '|rowspan="10" style="border-top:1px solid"|'
	
	insert_box(content, 18, 10, keys[c[1]][11], keys[c[2]][11], ttable, args)
	if args['sb_det'] ~= nil then
		content[10][15] = '|colspan="3" rowspan="3" style="vertical-align:bottom;"| '..args['sb_det']
	end
	content[10][22] = '|colspan="3" rowspan="3" style="text-align:center; vertical-align:top; font-weight:bold"| Super Bowl ' .. sb_nr(year)
	
	for y = 1, 41 do
		table.insert(tree_build, '|-\n|style="height:1.6ex;"|\n')
		for x = 1, 10 do
			if content[x][y] ~= nil then
				table.insert(tree_build, content[x][y] .. '\n')
			end
		end
	end

	return tree_build
end

function p.test(frame)
	if(frame.args[1] == 'box') then
		return box(frame.args[2], frame.args[3], frame.args[4], frame.args[5])
	elseif (frame.args[1] == 'build_box') then
		return build_box(frame.args[2], frame.args[3], frame.args[4], frame.args[5])
	elseif (frame.args[1] == 'get_score') then
		return get_score(frame.args, frame.args[1], frame.args[2])
	end

	local ret = {}

	table.insert(ret, 's=' .. frame.args['s'])
	
	for i = 1, mw.ustring.match(frame.args['r0'],"^%s*(%d*)"), 7 do
		table.insert(ret, ' n=' .. frame.args['n'])
	end
	table.insert(ret, ' r0=' .. mw.ustring.match(frame.args['r0'],"^%s*(%d*)") .. ' : ' .. mw.ustring.match(frame.args['r0'],"(%d*)%s*$"))
	table.insert(ret, ' r1=' .. mw.ustring.match(frame.args['r1'],"^%s*(%d*)") .. ' : ' .. mw.ustring.match(frame.args['r1'],"(%d*)%s*$"))
	if frame.args['r2'] then
		table.insert(ret, ' r2 exists')
	end
	if frame.args['r2'] == nil then
		table.insert(ret, ' r2=nil')
	end
	if frame.args['r3'] then
		table.insert(ret, ' r3 exists')
	end
	if frame.args['r3'] == nil then
		table.insert(ret, ' r3=nil')
	end
	
	return table.concat(ret)
end

return p