Module:Team appearances list

This is an old revision of this page, as edited by Izkala (talk | contribs) at 19:23, 1 June 2015 (Competition records). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module produces a horizontal list of team appearances at a specified
-- sports competition; each item is in the format:
-- [[<team> at the <year> <competition>|<year]]. The module requires the
-- following parameters: begin_year, interval, team and competition. end_year
-- may be optionally defined if either the competition or the team is now
-- defunct. Additionally, any number of positional parameters may be specified,
-- each of which must be a year (or year range) in which the team was absent
-- from the competition.

local p = {}
local compressSparseArray = require('Module:TableTools').compressSparseArray
local hlist = require('Module:List').horizontal
local valueUnion = require('Module:Set').valueUnion

local COMPETITIONS = {
	["Mediterranean Games"] = {
		["years"] = {1951, 1955, 1959, 1963, 1967, 1971, 1975, 1979, 1983, 1987,
			1991, 1993, 1997, 2001, 2005, 2009, 2013, 2017}
	},
	["Pan American Games"] = {
		["years"] = {1951, 1955, 1959, 1963, 1967, 1971, 1975, 1979, 1983, 1987,
			1991, 1995, 1999, 2003, 2007, 2011, 2015, 2019}
	},}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentOnly=true})
	return p._main(args)
end
 
function p._main(args)
	local begin_year = args.begin_year or 0
	local end_year = args.end_year or 0

	local appearances = {}
	local absences = {}
	for _, v in pairs(compressSparseArray(args)) do
		absences[mw.ustring.sub(v, 1, 4)] = mw.getCurrentFrame():expandTemplate{
			title='Gray', args={v}}
	end

	local function processYear(y)
		y = tostring(y)
		if absences[y] then
			table.insert(appearances, absences[y])
			absences[y] = nil
		else
			table.insert(appearances, mw.ustring.format(
				'[[%s at the %s %s|%s]]', args.team, y, args.competition, y))
		end
	end

	if COMPETITIONS[args.competition] then
		for _, y in pairs(COMPETITIONS[args.competition].years) do
			if y > begin_year and y < end_year then
				processYear(y)
			end
		end
	else
		for y = begin_year, (end_year or os.date('%Y')+args.interval), 
				args.interval do
			processYear(y)
		end
	end
	return hlist(valueUnion(absences, appearances))
end

return p