Questa pagina è protetta dallo spostamento
Questa pagina è protetta

Modulo:Incontro sportivo: differenze tra le versioni

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
Contenuto cancellato Contenuto aggiunto
m tab
restituisco il valore di team così com'è se non c'è la bandiera
Riga 200: Riga 200:
return n == 1 and (team .. ' ' .. flag) or (flag .. ' ' .. team)
return n == 1 and (team .. ' ' .. flag) or (flag .. ' ' .. team)
end
end
return team
end
end



Versione delle 21:26, 29 ago 2018

Questo modulo implementa le funzionalità dei template {{Incontro di club}} e {{Incontro internazionale}}.

Ha una sottopagina CSS: Modulo:Incontro sportivo/styles.css.


local p = {}
local errorcat = '[[Categoria:Errori di compilazione del template Incontro %s]]'

local int_args = {
	'Codice disciplina', 'Salvezza 1', 'Salvezza 2'
}

local club_args = {
	'Note', 'Singoli 1', 'Singoli 2', 'Doppi 1', 'Doppi 2', 'Tripli 1', 'Tripli 2', 'Penalita 1', 'Penalita 2'
}

local labels = {
	{ Marcatori = 'Marcatori' },
	{ Mete = '[[Meta (rugby)|mt]]' },
	{ Trasformazioni = '[[Meta (rugby)#Trasformazione|tr]]' },
	{ ['Calci piazzati'] = '[[Calcio piazzato#Rugby|c.p.]]' },
	{ Drop = '[[Drop (rugby)|drop]]' },
	{ Mark = '[[Mark (rugby)|mark]]' },
	{ Punti = 'Punti' },
	{ Assist = '[[Assist (pallacanestro)|Assist]]' },
	{ Rimbalzi = '[[Rimbalzo|Rimbalzi]]' },
	{ Valide = '[[Valida|Valide]]' },
	{ Errori = '[[Errore (baseball)|Errori]]' },
	{ Vittoria = '[[Vittoria (baseball)|Vittoria]]' },
	{ Sconfitta = '[[Sconfitta (baseball)|Sconfitta]]' },
	{ Salvezza = '[[Salvezza (baseball)|Salvezza]]' },
	{ Singoli = 'Singoli' },
	{ Doppi = '[[Doppio (baseball)|Doppi]]' },
	{ Tripli = '[[Triplo (baseball)|Tripli]]' },
	{ Fuoricampo = '[[Fuoricampo (baseball)|Fuoricampo]]' },
	{ TiriExtra = {
		rig = '[[Tiri di rigore]]',
		cp = '[[Calcio piazzato#Rugby|Calci piazzati]]',
		so = '[[Shootout]]'
		} },
	{ Penalita = 'Penalità' },
	{ Formazione = 'Formazioni' },
	{ Sostituzioni = 'Sostituzioni' },
	{ Allenatore = 'Allenatori' }
}

-- Restituisce true se trova il valore in una tabella
local function in_array(needle, haystack)
	if needle == nil then return end
	for _, v in ipairs(haystack) do
		if v == needle then return true end
	end
	return
end

-- Racchiude un argomento tra pre e post e converte nil in stringa vuota
local function wrap(arg, pre, post)
	if not arg then return '' end
	return mw.ustring.format('%s%s%s', pre or '', arg, post or '')
end

-- Restituisce un messaggio di errore con relativa categoria
local function set_error(msg)
	return string.format('<span class="error">%s</span>%s', msg, errorcat)
end

-- Carica il valore nel template Bandiera e ne restituisce il risultato
local function loadFlag(naz)
	if not naz then return '' end
	return mw.getCurrentFrame():expandTemplate {
		title = 'Bandiera',
		args = { naz }
	}
end

-- Verifica i punteggi di entrambe le squadre
local function checkPoints(v1, v2)
	local error = set_error('Punteggio mancante')
	return v1 or error, v2 or error
end

-- =============================================================================
--                            Classe Incontro
-- =============================================================================
local Incontro = {}

-- Costruttore della classe Incontro
function Incontro:new(args)
	local self = {}
	setmetatable(self, { __index = Incontro } )
	self.args = args
	return self
end

-- Restituisce i contenuti della colonna a sinistra
function Incontro:_getLeftContent()
	local pre_cit, post_cit, cit, gm, anno, ora, turno
	pre_cit = '[[' .. (self.args['CittàLink'] and (self.args['CittàLink'] .. '|') or '')
	post_cit = ']]' .. (self.args.Giornomese and '<br/>' or '')
	cit = wrap(self.args['Città'], pre_cit, post_cit)
	gm, anno = self.args.Giornomese or '', self.args.Anno or ''
	ora = wrap(self.args.Ora, ', ore ')
	turno = wrap(self.args.Turno, '<br/>')
	return mw.ustring.format('%s%s %s%s%s', cit, gm, anno, ora, turno)
end

-- Restituisce i contenuti della colonna al centro
function Incontro:_getMiddleContent()
	local punt, punt2, supp, cdr, parz, note, ref, ref2
	punt, punt2 = self.args['Punteggio 1'] or errorcat, self.args['Punteggio 2'] or errorcat
	local validate = function(s)
		return s and string.upper(s) == 'X' and '' or s
	end
	supp = wrap(validate(self.args.Supplementari), '<br/>([[Tempi supplementari|d.', 't.s.]])')
	cdr = wrap(validate(self.args['Calci di rigore']), '<br/>([[Tiri di rigore|d.', 'c.r.]])')
	parz = wrap(self.args.Parziali, '<br/>(', ')')
	note = wrap(self.args.Note, '<br/>')
	ref = wrap(self.args.Referto, '<br/>[', ' referto]')
	ref2 = wrap(self.args['Referto 2'], '<br/>[', ' referto 2]')
	return mw.ustring.format("'''%s&nbsp;&ndash;&nbsp;%s'''<small>%s%s%s%s%s%s</small>",
			punt, punt2, supp, cdr, parz, note, ref, ref2)
end

-- Restituisce le righe della colonna a destra elaborando
-- i parametri nominali a gruppi per numero
function Incontro:_renderRightRow(v)
	if not self.args[v] then return nil end
	local msg = {}
	local subargs = {
		Arbitro = { label = 'Arbitr' .. (self.args.Arbitro2 and 'i' or 'o'), naz = 'Nazarbitro', cit = 'Cittarbitro' },
		Cronometrista = { label = 'Crono', naz = 'Nazcronometrista', cit = 'Cittacronometrista' }
	}
	local i = 1
	local getValue = function(t)
		for k, v in pairs(t) do
			t[k] = self.args[v .. (i == 1 and '' or i)]
		end
		return t[1], t[2], t[3]
	end
	while true do
		local main_arg, naz, cit = getValue({ v, subargs[v].naz, subargs[v].cit })
		-- parametri multipli per gli arbitri, uno solo per il cronometrista
		if not main_arg or i == 2 and v == 'Cronometrista' then break end
		naz = loadFlag(naz)
		cit = wrap(cit, '(', ')')
		msg[i] = mw.ustring.format('%s %s %s', naz, main_arg, cit)
		i = i + 1
	end
	local row = mw.html.create('tr')
		:tag('td'):wikitext(subargs[v].label .. ':&nbsp;'):done()
		:tag('td'):wikitext(table.concat(msg, '<br/>')):done()
	return row
end

-- Restituisce i contenuti della colonna a destra
function Incontro:_getRightContent()
	local root, stad, spet
	stad = self.args.Stadio or ''
	spet = wrap(self.args.Spettatori, '&nbsp;<span class="nowrap">(', ' spett.)</span>')
	if self.args.Arbitro then
		root = mw.html.create('table')
			:node(self:_renderRightRow('Arbitro'))
			:node(self:_renderRightRow('Cronometrista'))
	else
		root = ''
	end
	return mw.ustring.format('%s%s%s', stad, spet, tostring(root))
end

-- Formatta e restituisce il primo o il secondo team indicato
function Incontro:_renderTeam(n)
	local team, flag
	if self.args[1] == 'internazionale' then
		team = self.args['Nazionale ' .. n]
		flag = loadFlag(team)
		local altro = self.args['Altro' .. n] or ''
		if self.args['Codice disciplina'] == 'BV' then
			return n == 1 and (altro .. ' ' .. flag) or (flag .. ' ' .. altro)
		else
			local F = self.args.F == 'X' and 'F' or ''
			local sesso = self.args.Sesso or ''
			return mw.getCurrentFrame():expandTemplate{
				title = 'Naz' .. (n == 1 and 'BD' or ''),
				args = { self.args['Codice disciplina'], team, F .. sesso, altro, 'h' }
			}
		end
	end
	local soccer = false
	if self.args['SquadraCalcio 1'] or self.args['SquadraCalcio 2'] then
		soccer = true
		self.args['Squadra ' .. n] = self.args['SquadraCalcio ' .. n]
	end
	team, flag = self.args['Squadra ' .. n], self.args['Bandiera ' .. n]
	if not team then return end
	if soccer then
		local frame = mw.getCurrentFrame()
		-- chiamata protetta per verificare l'esistenza del template
		local success, result = pcall(frame.expandTemplate, frame, {
			title = 'Template:Calcio ' .. team,
			args = { n == 1 and 'BD' or '', anno }
		})
		return success and result or set_error('Squadra di calcio non riconosciuta')
	elseif flag then
		flag = '[[File:' .. flag .. '|border|20px|class=noviewer]]'
		return n == 1 and (team .. ' ' .. flag) or (flag .. ' ' .. team)
	end
	return team
end

-- Restituisce la sottotabella HTML contenente le statistiche
function Incontro:_getStatsTableNode()
	local collapsible = self.args.Collassato and 'mw-collapsible mw-collapsed noprint' or ''
	local root = mw.html.create('table')
		:addClass(collapsible .. ' nowraplinks idc-subtable')
	root:tag('td')
		:attr('colspan', 3)
	local subtable = false
	-- elaborazione ordinata degli argomenti
	for _, t in ipairs(labels) do
		local k, label = next(t) -- ottiene chiave ed etichetta
		local value1, value2 = self.args[k .. ' 1'], self.args[k .. ' 2']
		if k == 'TiriExtra' and self.args.TiriExtra then
			-- quando TiriExtra è compilato, la riga va sempre processata
			value1, value2 = value1 or '', value2 or ''
			if self.args[1] == 'internazionale' then
				local needle = self.args['Codice disciplina']
				local haystacks = {
					{'CA', 'C5', 'FS', 'BS', 'RH'},
					{'R13', 'R15'},
					{'HG', 'SH', 'goalball'}
				}
				self.args.TiriExtra = in_array(needle, haystacks[1]) and 'rig' or
						in_array(needle, haystacks[2]) and 'cp' or
						in_array(needle, haystacks[3]) and 'so' or ''
			end
			label = label[self.args.TiriExtra] or set_error('Codice per TiriExtra non riconosciuto')
			label = mw.ustring.format('%s<br/>%s &ndash; %s',
					label, checkPoints(self.args['PuntiTiriExtra 1'], self.args['PuntiTiriExtra 2']))
		elseif k == 'Allenatore' then
			local all1, all2 = self.args[k .. ' 1'], self.args[k .. ' 2']
			local naz1, naz2 = self.args.NazAll1, self.args.NazAll2
			if all1 and naz1 then
				value1 = all1 .. ' ' .. loadFlag(naz1)
			end
			if all2 and naz2 then
				value2 = loadFlag(naz2) .. ' ' .. all2
			end
		end
		-- controllo su label necessario per via di TiriExtra
		if (value1 or value2) and type(label) == 'string' then
			subtable = true
			local haystack = { 'TiriExtra', 'Formazione', 'Sostituzioni' }
			if in_array(k, haystack) then
				value1 = value1 and '<br/>' .. value1
				value2 = value2 and '<br/>' .. value2
			end
			root:tag('tr')
				:tag('td'):wikitext(value1):done()
				:tag('td'):wikitext(label):done()
				:tag('td'):wikitext(value2):done()
		end
	end
	return subtable and mw.html.create('td'):attr('colspan', 5):node(root) or nil
end

-- Restituisce la tabella HTML principale seguita da un tag hr
function Incontro:getHTML()
	local background = self.args.Sfondo == 'on' and 'idc-white' or 'idc-lavender'
	local root = mw.html.create('table')
		:addClass('idc-maintable ' .. background)
		:attr('id', self.args.id)
		:tag('tr')
			:tag('td'):wikitext(self:_getLeftContent()):done()
			:tag('td'):wikitext(self:_renderTeam(1)):done()
			:tag('td'):wikitext(self:_getMiddleContent()):done()
			:tag('td'):wikitext(self:_renderTeam(2)):done()
			:tag('td'):wikitext(self:_getRightContent()):done()
			:done()
	root
		:node(self:_getStatsTableNode()):done()
	return tostring(root) .. '<hr>'
end

-- Funzione per {{Incontro di club}} e {{Incontro internazionale}}
function p.main(frame)
	local args = { frame.args[1] }
	local styles = 'Modulo:Incontro sportivo/styles.css'
	local current_page = mw.title.getCurrentTitle()
	local current_namespace = current_page.namespace
	errorcat = current_namespace ~= 0 and '' or string.format(errorcat,
			args[1] == 'club' and 'di club' or 'internazionale')
	-- carica argomenti in base alla chiamata del template
	for k, v in pairs(frame:getParent().args) do
		if k ~= 1 and v ~= nil and v ~= '' and
				(args[1] == 'club' and not in_array(k, int_args) or
				 args[1] == 'internazionale' and not in_array(k, club_args)) then
			args[k] = v
		end
	end
	return frame:extensionTag{
			name = 'templatestyles',
			args = {src = styles}
		} .. Incontro:new(args):getHTML()
end

return p