Shko te përmbajtja

Moduli:Sports results

Nga Wikipedia, enciklopedia e lirë
-- Modul për të krijuar tabela kryq të rezultateve sportive
-- Shiko dokumentimin për detaje

require('strict')

local p = {}

-- Funksioni kryesor
function p.main(frame)
	-- Merr argumentet, duke hequr vlerat bosh
	local getArgs = require('Module:Arguments').getArgs
	local Args = getArgs(frame, {parentFirst = true})

	-- Dil nëse përdorim përfshirje seksioni për një seksion tjetër
	local tsection = frame:getParent().args['transcludesection'] or frame:getParent().args['section'] or ''
	local bsection = frame.args['section'] or ''
	if( tsection ~= '' and bsection ~= '' ) then
		if( tsection ~= bsection ) then
			return ''
		end
	end

	-- Deklaro variablat lokale
	local t = {}
	local t_footer = {}
	local t_return = {}
	local team_list = {}
	local notes_exist = false
	local ii, ii_fw, bg_col, team_name, team_code_ii, ii_start, ii_end
	-- Header i personalizuar për ekipet
	local team_header = Args['team_header'] or 'Shtëpi \\ Vizitor'
	-- Numri i ndeshjeve (turne)
	local legs = tonumber(Args['legs']) or 1
	local multirowlegs = (Args['multirowlegs'] or 'no') ~= 'no'

	-- Lidhje editimi nëse kërkohen
	local baselink = frame:getParent():getTitle()
	if mw.title.getCurrentTitle().fullText == baselink then	baselink = '' end
	local template_name = Args['template_name']
		or (baselink ~= '' and (':' .. baselink))
		or ''
	local edit_links = template_name == '' and ''
		or require('Module:Navbar')._navbar({
			template_name,
			mini=1,
			style='float:right'
		})

	-- Merr pikën e fillimit të tabelës (shumica fillojnë nga 1)
	local top_pos = tonumber(Args['highest_pos']) or 1
	-- Merr pikën e mbarimit të tabelës (e pakufizuar nëse bottom_pos < top_pos)
	local bottom_pos = tonumber(Args['lowest_pos']) or 0
	local N_teams = top_pos - 1 -- Default në 0 në fillim

	-- Ngarko modulet e tjera
	local p_sub = require('Module:Sports table/sub')

	-- Sintaksë alternative për listën e ekipeve
	if Args['team_order'] and Args['team_order'] ~= '' then
		local tlist = mw.text.split(Args['team_order'], '%s*[;,]%s*')
		for k, tname in ipairs(tlist) do
			if tname ~= '' then
				Args['team' .. k] = tname
			end
		end
	end

	if Args['team_header_note'] then
		notes_exist=true
		local note_string = frame:expandTemplate{ title = 'efn',
			args = { group='lower-alpha', Args['team_header_note']} }
		team_header = team_header .. note_string
	end

	-- Lexo numrin e ekipeve të njëpasnjëshme
	ii_start = N_teams
	while Args['team'..N_teams+1] ~= nil and (bottom_pos < top_pos or N_teams < bottom_pos) do
		N_teams = N_teams+1
		-- Shto dy herë në team_list, një herë për rangun aktual,
		-- të dytën për kërkim pozite në nëntabela
		team_list[N_teams] = Args['team'..N_teams] -- hyrja i^th është ekipi X
		team_list[Args['team'..N_teams]] = N_teams -- hyrja ekipi X është pozita i
	end
	ii_end = N_teams
	-- Merr ekipin për të shfaqur
	local ii_show = team_list[Args['showteam']] -- nil nëse nuk ekziston

	-- Cakto madhësinë e shkronjave
	local font_size=Args['font_size'] or '100%'

	-- Krijo header
	-- Hap tabelën
	table.insert(t,'{|class="wikitable plainrowheaders" style="text-align:center;font-size:'..font_size..';"\n')
	-- Titulli i tabelës
	if Args['title'] then
		table.insert(t,'|+ ' .. Args['title'] .. '\n')
	end
	-- Kolona e parë
	t_return.count = 0 			-- Parametër dummy
	t_return.tab_text = t		-- Teksti aktual
	t_return = p_sub.colhead(t_return,'auto', edit_links .. ' ' .. team_header)
	-- Kolonat e tjera kalohen në nënfunksion
	t_return = p.header(t_return,Args,p_sub,N_teams,team_list,legs,multirowlegs)
	t = t_return.tab_text

	-- Vlerë e rastësishme për unike
	math.randomseed( os.clock() * 10^8 )
	local rand_val = math.random()

	local note_string, note_id
	local note_id_list = {}

	-- Tani krijo rreshtat individualë
	ii_start = tonumber(Args['highest_row']) and (tonumber(Args['highest_row']) > top_pos) and tonumber(Args['highest_row']) or top_pos
	ii_end = tonumber(Args['lowest_row']) and (tonumber(Args['lowest_row']) < N_teams) and tonumber(Args['lowest_row']) or N_teams
	for ii=ii_start,ii_end do
		-- Merr info ekipi
		team_code_ii = team_list[ii]
		team_name = Args['name_'..team_code_ii] or team_code_ii
		local ii_style = 'text-align:' .. (Args['team_align'] or 'right') .. ';'
			.. (ii and ii == ii_show and 'font-weight:bold;' or '')
			.. (Args['team_nowrap'] and 'white-space:nowrap;' or '')
		local team_note = Args['note_'..team_code_ii]
		if team_note then
			notes_exist = true
			-- Vetëm kur ekziston
		-- Së pari kontrollo nëse ekziston referenca për shënimin
			if not Args['note_'..team_note] then
				-- Është hyrja
				-- Shto fund të rastësishëm për ID unike nëse ka më shumë tabela
				note_id = '"table_note_'..team_code_ii..rand_val..'"'
				note_id_list[team_code_ii] = note_id
				note_string = frame:expandTemplate{ title = 'efn',
                args = { group='lower-alpha', name=note_id, team_note} }
			else
				-- Kontrollo ekzistencën diku tjetër
				local note_local_num = team_list[team_note] or ii_end + 1
				if note_id_list[team_note] or ((note_local_num >= ii_start) and (note_local_num <= ii_end)) then
					-- Ekziston
					note_id = '"table_note_'..team_note..rand_val..'"' -- Identifikues
					note_string = frame:extensionTag{ name = 'ref',
						args = { group = 'lower-alpha', name = note_id} }
				else
					-- Tani përcakto identifikuesin për këtë
					-- Shto fund të rastësishëm për ID unike
					note_id = '"table_note_'..team_note..rand_val..'"'
					note_id_list[team_note] = note_id
					note_string = frame:expandTemplate{ title = 'efn',
						args = { group='lower-alpha', name=note_id, Args['note_'..team_note]} }
				end
			end
			-- Tani shtoje këtë te stringu i emrit të ekipit
			team_name = team_name..note_string
		end
		-- Emrat e ekipeve
		table.insert(t,'|- \n') -- Rresht i ri
		table.insert(t,'! scope="row"'.. (multirowlegs and ' rowspan=' .. legs or '')
			.. 'style="'.. ii_style ..'"| '..team_name..'\n') -- Numri i pozitës

		-- Tani përfshi shënim për rezultatet e ndeshjeve nëse është e nevojshme
		for jj=top_pos,N_teams do
			local team_code_jj = team_list[jj]
			if ii == jj then
				-- Asgjë
			else
				for l=1,legs do
					local m = (legs == 1) and 'match_' or 'match' .. l .. '_'
					local match_note = Args[m ..team_code_ii..'_'..team_code_jj..'_note']
					if match_note then
						notes_exist = true
						-- Vetëm kur ekziston
						-- Së pari kontrollo nëse ekziston referenca për shënimin
						if not (Args['note_'..match_note] or Args[m ..match_note..'_note']) then
							-- Është hyrja
							-- Shto fund të rastësishëm për ID unike
							note_id = '"table_note_'..l.."_"..team_code_ii..'_'..team_code_jj..rand_val..'"'
							note_id_list[team_code_ii..'_'..team_code_jj] = note_id
							note_string = frame:expandTemplate{ title = 'efn',
								args = { group='lower-alpha', name=note_id, match_note} }
						else
							-- Kontrollo ekzistencën diku tjetër
							local note_local_num = team_list[match_note] or ii_end + 1
							if note_id_list[match_note] then
								-- Referencë për një shënim ekzistues
								note_id = note_id_list[match_note] -- Huazo identifikuesin ekzistues
								note_string = frame:extensionTag{ name = 'ref',
									args = { group = 'lower-alpha', name = note_id} }
							elseif (note_local_num >= ii_start) and (note_local_num <= ii_end) then
								-- Referencë për shënim lojtari nga shënim ndeshje
								note_id = '"table_note_'..match_note..rand_val..'"'
								note_string = frame:extensionTag{ name = 'ref',
									args = { group = 'lower-alpha', name = note_id} }
							else
								-- Referencë për shënim tjetër ndeshje para se të përcaktohet
								note_id = '"table_note_'..l.."_"..match_note..rand_val..'"'
								note_id_list[match_note] = note_id
								note_string = frame:expandTemplate{ title = 'efn',
									args = { group='lower-alpha', name=note_id, Args['note_'..match_note]} }
							end
						end
						-- Tani shtoje këtë te stringu i rezultatit të ndeshjes
						Args[m..team_code_ii..'_'..team_code_jj] = (Args[m..team_code_ii..'_'..team_code_jj] or '–')..note_string
					end
				end
			end
		end
		-- Pastaj rezultatet individuale
		t = p.row(t,Args,N_teams,team_list,ii,ii_show,legs,multirowlegs)
	end

	-- Mbyll tabelën
	table.insert(t, '|}\n')

	-- Merr info për footer
	local update = Args['update']
		or 'e panjohur'
	local start_date = Args['start_date']
		or 'e panjohur'
	local source = Args['source']
		or frame:expandTemplate{ title = 'citation needed',
			args = { reason='Nuk është përcaktuar parametri burimi', date=os.date('%B %Y') } }

	-- Krijo tekstin e footer
	-- Përditësimi i datës
	if string.lower(update)=='complete' then
		-- Mos bëj asgjë
	elseif update=='' then
		-- Parametër bosh
		table.insert(t_footer,'Përditësuar për ndeshjet e luajtura më datë të panjohur. ')
	elseif string.lower(update)=='future' then
		-- Datë fillimi e ardhshme
		table.insert(t_footer,'Ndeshja(t) e parë do të luhet: '..start_date..'. ')
	else
		table.insert(t_footer,'Përditësuar për ndeshjet e luajtura më '..update..'. ')
	end
	table.insert(t_footer,'Burimi: '..source)
	if (Args['matches_style'] or '') == 'FBR' then
		table.insert(t_footer, Args['team_header']
			and '<br />Legjenda: Blu = fitore ekipi kolonës majtas; Verdhë = barazim; Kuq = fitore ekipi rreshtit të sipërm.'
			or '<br />Legjenda: Blu = fitore ekipi shtëpi; Verdhë = barazim; Kuq = fitore ekipi vizitor.')
	elseif (Args['matches_style'] or '') == 'BSR' then
		table.insert(t_footer, Args['team_header']
			and '<br />Legjenda: Blu = fitore ekipi kolonës majtas; Kuq = fitore ekipi rreshtit të sipërm.'
			or '<br />Legjenda: Blu = fitore ekipi shtëpi; Kuq = fitore ekipi vizitor.')
	end
	if Args['a_note'] then
		table.insert(t_footer, '<br />Për ndeshjet e ardhshme, "a" tregon se ekziston një artikull për rivalitetin midis dy pjesëmarrësve.')
	end
	if Args['ot_note'] then
		table.insert(t_footer, '<br />Ndeshjet me hijezim më të lehtë të sfondit u vendosën pas kohës së shtuar.')
	end

	local templatestyles = frame:extensionTag{
		name = 'templatestyles', args = { src = 'Module:Sports results/styles.css' }
	}

	-- Shto shënime (nëse zbatohet)
	if notes_exist then
		table.insert(t_footer,'<br>Shënime:')
		t_footer = templatestyles .. '<div class="sports-results-notes">'..table.concat(t_footer)..'</div>'
		t_footer = t_footer..frame:expandTemplate{ title = 'notelist', args = { group='lower-alpha'} }
	else
		t_footer = templatestyles .. '<div class="sports-results-notes">'..table.concat(t_footer)..'</div>'
	end

	-- Shto footer te tabela kryesore e tekstit
	table.insert(t,t_footer)

	-- Rishkruaj lidhjet ankorë
	for k=1,#t do
		if t[k]:match('%[%[#[^%[%]]*%|') then
			t[k] = mw.ustring.gsub(t[k], '(%[%[)(#[^%[%]]*%|)', '%1' .. baselink .. '%2')
		end
	end

	return '<div style="overflow:hidden">'
		.. '<div class="noresize overflowbugx" style="overflow:auto">\n'
		.. table.concat(t) .. '</div></div>'
end

-- Funksione të tjera
local function get_short_name(s, t, n, ss)
	-- kthe emrin e shkurtër nëse është i përcaktuar
	if s and s ~= '' then
		return s
	end
	-- hiq flamurin nëse është e nevojshme
	if ss and n then
		if ss == 'noflag' then
			n = mw.ustring.gsub(n, '%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*%]%]', '')
			n = mw.ustring.gsub(n, '^%s*&nbsp;%s*', '')
		elseif ss == 'flag' then
			n = mw.ustring.gsub(n, '(<span class="flagicon">%s*%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*link=)[^%|%[%]]*(%]%][^<>]*</span>)%s*%[%[([^%[%]%|]*)%|[^%[%]]*%]%]', '%1%3%2')
			n = mw.ustring.gsub(n, '(%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*link=)[^%|%[%]]*(%]%])%s*&nbsp;%s*%[%[([^%[%]%|]*)%|[^%[%]]*%]%]', '%1%3%2')
			n = mw.ustring.gsub(n, '(%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*link=)[^%|%[%]]*(%]%])%s*%[%[([^%[%]%|]*)%|[^%[%]]*%]%]', '%1%3%2')
			n = mw.ustring.gsub(n, '.*(<span class="flagicon">%s*%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*%]%][^<>]*</span>).*', '%1')
			n = mw.ustring.gsub(n, '.*(%[%[[Ff][Ii][Ll][Ee]:[^%[%]]*%]%]).*', '%1')
			n = mw.ustring.gsub(n, '&nbsp;(</span>)', '%1')
		end
	end

	-- zëvendëso tekstin e lidhjes në emër me shkurtesën e ekipit nëse është e mundur
	if n and t and n:match('(%[%[[^%[%]]*%]%])') then
		n = mw.ustring.gsub(n, '(%[%[[^%|%]]*%|)[^%|%]]*(%]%])', '%1' .. t .. '%2')
		n = mw.ustring.gsub(n, '(%[%[[^%|%]]*)(%]%])', '%1|' .. t .. '%2')
		n = mw.ustring.gsub(n, '(%[%[[^%|%]]*%|)([A-Z][A-Z][A-Z])(%]%])&nbsp;<span[^<>]*>%([A-Z][A-Z][A-Z]%)</span>', '%1%2%3')
		return n
	end
	-- asgjë nuk funksionoi, kthe thjesht shkurtesën e ekipit pa lidhje
	return t or ''
end

local function get_score_background(s, c)
	local s1, s2
	-- Përcakto ngjyrosjen
	local wc, lc, tc
	if c == 'level2' then
	wc, lc, tc = '#CCF9FF', '#FCC', '#FFC' -- blu2, kuq2, verdhë2
	elseif c == 'level3' then
	wc, lc, tc = '#DDFCFF', '#FDD', '#FFD' -- blu3, kuq3, verdhë3
	elseif c == 'level4' then
	wc, lc, tc = '#EEFFFF', '#FEE', '#FFE' -- blu4, kuq4, verdhë4
	else
	-- Këto janë ngjyrat e reja të kërkuara
	wc = '#b8c8ff' -- Fitore (Win) (New value)
	tc = '#ffff64' -- Barazim (Draw)
	lc = '#ffc8c8' -- Humbje (Loss)
	end

	-- kontrollo për anashkalim
	if s:match('^%s*<span%s+style%s*=["\'%s]*background[%-colr]*%s*:([^\'";<>]*).-$') then
		local c = mw.ustring.gsub(s,'^%s*<span%s+style%s*=["\'%s]*background[%-colr]*%s*:([^\'";<>]*).-$', '%1')
		return 'background: ' .. c ..';'
	end

	-- hiq lidhjen nëse është e nevojshme
	if s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]') then
		s = s:match('^%s*%[%[[^%[%]]*%|([^%[%]]*)%]%]')
	end
	if s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]') then
		s = s:match('^%s*%[[^%[%]%s]*%s([^%[%]]*)%]')
	end
	if s:match('<span[^<>]*>(.-)</span>') then
		s = s:match('<span[^<>]*>(.-)</span>')
	end

	-- merr rezultatet
	s1 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d%.]+)%s*–%s*([%d%.]+).*', '%1' ) or '') or ''
	s2 = tonumber(mw.ustring.gsub( s or '',
		'^%s*([%d%.]+)%s*–%s*([%d%.]+).*', '%2' ) or '') or ''

	-- kthe ngjyrosjen nëse është e mundur
	if s1 ~= '' and s2 ~= '' then
		return (s1 > s2) and ('background: ' .. wc .. ';')
			or ((s2 > s1) and ('background: ' .. lc .. ';')
				or ('background: ' .. tc .. ';'))
	else
		return ''
	end
end

local function format_score(s)
	s = mw.ustring.gsub(s or '', '^%s*([%d%.]+)%s*[–−—%-]%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*([%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*%-%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[%[[^%[%]]*%|[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	s = mw.ustring.gsub(s, '^%s*(%[[^%[%]%s]*%s+[%d%.]+)%s*&[MmNn][Dd][Aa][Ss][Hh];%s*([%d%.]+)', '%1–%2')
	return s
end

function p.header(tt,Args,p_sub,N_teams,team_list,legs,multirowlegs)
	local ii, team_code_ii, short_name
	legs = legs or 1

	-- Cakto gjerësinë e kolonës së ndeshjeve
	local col_width = Args['match_col_width'] or '28'

	-- Merr disa vlera default në rast se nuk fillon nga 1
	local top_pos = tonumber(Args['highest_pos']) or 1

	for l=1,legs do
		if multirowlegs and l > 1 then
			break
		end
		for ii=top_pos,N_teams do
			team_code_ii = team_list[ii]
			short_name = get_short_name(Args['short_'..team_code_ii],
				team_code_ii, Args['name_'..team_code_ii], Args['short_style'] or '')
			local bl = legs > 1 and ii == top_pos and ' style="border-left:2px solid #aaa;"' or ''
			tt = p_sub.colhead(tt,col_width .. bl,short_name)
		end
	end
	return tt
end

function p.row(tt,Args,N_teams,team_list,ii,ii_show,legs,multirowlegs)
	-- Shënim: ii është numri i rreshtit që po shfaqet
	local jj, fw, bg, result, result_extra, team_code_ii, team_code_jj
	legs = legs or 1

	-- Cakto stilin e qelizës së rezultatit
	local matches_style = Args['matches_style'] or ''

	team_code_ii = team_list[ii]

	-- Merr disa vlera default në rast se nuk fillon nga 1
	local top_pos = tonumber(Args['highest_pos']) or 1
	for l=1,legs do
		if multirowlegs and l > 1 then
			table.insert(tt,'|- \n') -- Rresht i ri
		end
		for jj=top_pos,N_teams do
			team_code_jj = team_list[jj]
			local m = (legs == 1) and 'match_' or 'match' .. l .. '_'
			result = Args[m..team_code_ii..'_'..team_code_jj] or ''
			result_extra = Args['result_'..team_code_ii..'_'..team_code_jj] or ''
			local bl = legs > 1 and jj == top_pos and 'border-left:2px solid #aaa;' or ''

			if ii == jj or result == 'null' then
				-- Qelizë e ngurtë
				fw = 'font-weight:' .. (ii==ii_show and 'bold' or 'normal') .. ';'
				bg = ''

				-- Ngjyrë sfondi gri për qelizë të ngurtë
				if Args['solid_cell'] == 'grey' then
					table.insert(tt,'| style="'..fw..bl..'background:#bbb;" |\n')
				else
					table.insert(tt,'| style="'..fw..bl..bg..'" | &mdash;\n')
				end
			else
				-- Qelizë me përmbajtje
				-- Cakto trashësinë dhe sfondin
				fw = 'font-weight:' .. ((ii==ii_show or jj == ii_show) and 'bold' or 'normal') .. ';'
				bg = ''

				-- Riformato vijat
				if result ~= '' then
					result = format_score(result)
				end
				-- Ngjyrosja e sfondit nëse është e aktivizuar
				if matches_style == 'FBR' and result ~= '' then
					if result_extra == 'OT' then
						bg = get_score_background(result,'level2')
					elseif result_extra == 'PK' then
						bg = get_score_background(result,'level3')
					else
						bg = get_score_background(result,'')
					end
				elseif matches_style == 'BSR' and result ~= '' then
					if result_extra == 'OT' then
						bg = get_score_background(result,'level3')
					elseif result_extra == 'OTL' then
						bg = get_score_background('0–1','level3')
					elseif result_extra == 'OTW' then
						bg = get_score_background('1–0','level3')
					elseif result_extra == 'L' then
						bg = get_score_background('0–1','')
					elseif result_extra == 'W' then
						bg = get_score_background('1–0','')
					else
						bg = get_score_background(result,'')
					end
				end
				table.insert(tt,'| style="white-space:nowrap;'..fw..bl..bg..'" |'..result..'\n')
			end
		end
	end

	return tt
end

return p