Pāriet uz saturu

Modulis:Basketball color

Permanently protected module
Vikipēdijas lapa
Dokumentācijas ikona Moduļa dokumentācija[izveidot]
local p = {}

local data_module = "Module:Basketball color/data"

local function stripwhitespace(text)
	return text:match("^%s*(.-)%s*$")
end

local function get_colors(team, unknown)
	team = stripwhitespace(team or '')
	unknown = unknown or {"DCDCDC", "000000", "000000", "FFFFFF"}
	
	local use_default = {
		[""] = 1,
--		["retired"] = 1,
--		["free agent"] = 1,
	}
	
	local colors = nil
	
	if ( team and use_default[team:lower()] ) then
--		colors = {"DCDCDC", "000000", "000000", "FFFFFF"}
	else
		local all_colors = mw.loadData(data_module)
		colors = all_colors[team]
		if ( colors and type(colors) == 'string' ) then
			colors = all_colors[colors]
		end
	end

	return colors or unknown
end

local function team_color(team, num)
	local colors = get_colors(team, nil)

	-- hack for NBA Eastern Conference and NBA Western Conference
	if (team:match("Eastern Conference") or team:match("Western Conference")) then
		if(num == '') then
			num = '1'
			local track = mw.loadData("Module:Basketball color/tracking")
		end
	end
	
	num = tonumber(num:match('[1-4]') or '0')
	if ( num ) then
		return colors[num]
	else
		return ''
	end
end

local function team_colorcell(team, borderwidth, bg, fg, bd)
	local colors = get_colors(team, nil)
	local border = ''
	borderwidth = borderwidth or ''
	
	if (borderwidth ~= '') then
		border = 'border:' .. borderwidth .. 'px solid #' .. colors[bd] .. ';'
	end
	
	return 'background-color:#' .. colors[bg] .. ';color:#' .. colors[fg] .. ';' .. border
end

--krasu kodu dabusana, pagaidam neveiksmigi
--local function my_func(team, bg)
--	local colors = get_colors(team, nil)
--	return  .. colors[bg] .. 
--end

local function team_check(team, unknown)
	local colors = get_colors(team, unknown)
	if type(colors) == 'table' then
		return 'known'
	else
		return unknown
	end
end

local function team_testtable(args, style, data)
	local teamlist = {}
	local t = ''
	local style = 'font-size:90%; line-height:100%;' .. (style or '')
	if ( data ) then data_module = data end

    -- helper function
	local function table_row(t, c)
		local res = ''
		if( c[1] and c[2] and c[3] and c[4] ) then
			res = res .. '<tr><td>' .. t .. '</td>'
		else
			res = res .. '<tr><td>' .. t .. ' <span class=error>ERROR</span></td>'
		end
		res = res .. '<td style="background:#' .. (c[1] or 'ERROR') .. '"></td>'
		res = res .. '<td style="background:#' .. (c[2] or 'ERROR') .. '"></td>'
		res = res .. '<td style="background:#' .. (c[3] or 'ERROR') .. '"></td>'
		res = res .. '<td style="background:#' .. (c[4] or 'ERROR') .. '"></td>'
		res = res .. '</tr>\n'
		
		return res
	end

    -- list of teams
	if( args and args[1] ) then
		for k, team in pairs(args) do
			if type(k) == 'number' then
				table.insert(teamlist, team)
			end
		end
	else
		local all_colors = mw.loadData(data_module)
		for team, colors in pairs( all_colors ) do
			table.insert(teamlist, team)
		end
		table.insert(teamlist, 'Free agent')
		table.insert(teamlist, 'Retired')
	end
	
	-- build table
	t = t .. '<table class=\"wikitable sortable\" style=\"' ..  style .. '\">\n'
	t = t .. '<tr><th>Team</th><th class="unsortable">1</th><th class="unsortable">2</th>'
	t = t .. '<th class="unsortable">3</th><th class="unsortable">4</th></tr>\n'
	for k, team in pairs( teamlist ) do
		t = t .. table_row(team, get_colors(team))
	end
	t = t .. '</table>'
	
	return t
end

function p.color(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	return team_color(args[1] or '', args[2] or '')
end

function p.colorcell(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	return team_colorcell(args[1] or '', args['border'] or '', 1, 2, 3)
end

function p.colorcell2(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1)
end

function p.check(frame)
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	return team_check(args[1] or '', args[2] or '')
end

function p.testtable(frame)
	return team_testtable((frame.args[1] ~= nil) and frame.args or frame:getParent().args, frame.args['style'] or '', frame.args['data'])
end

return p