Halo Esports Wiki

We are currently performing extensive maintenance to this wiki and as a result there will likely be errors. Please be patient while we work to fix all problems.

READ MORE

Halo Esports Wiki
Advertisement

To edit the documentation or categories for this module, click here.


local util_args = require('Module:ArgsUtil')
local util_infobox = require('Module:InfoboxUtil')
local util_html = require('Module:HtmlUtil')
local util_cargo = require('Module:CargoUtil')
local util_table = require('Module:TableUtil')
local util_title = require('Module:TitleUtil')
local util_vars = require('Module:VarsUtil')

local Country = require('Module:Country')
local Region = require('Module:Region')
local m_team = require('Module:Team')

local IntroSentence = require('Module:IntroSentence/Team').main

local NOIMAGE = 'Unknown Infobox Image - Team.png'

local Infobox = require('Module:Infobox'):extends()

Infobox.LAYOUT = {
	tabs = 'TeamTabsHeader',
	sections = { 'Team Information', 'History', 'Social Media & Links', 'Current Roster' },
	contents = {
		{ 'Org Location', 'Team Location', 'FoundedIn', 'Region', 'Owner', 'HeadCoach', 'Sponsor', 'Partner', 'Affiliated' },
		{ 'history', history = 'wide' },
		{ 'social', social = 'wide' },
		{ 'rosterimage', rosterimage = 'wide' }
	},
}
Infobox.type = 'Team'

local SPECIAL = {
	allstar = { cat = 'All-Star Regional Teams', nonav = true, nocargo = true },
	aprilfools = { nonav = true, nocargo = true },
	city = { nonav = true, nocargo = true },
	clubmasters = { nonav = true, nocargo = true },
	collegiate = { cat = 'Collegiate Teams', nocargo = false },
	iesf = { cat = 'IESF Teams', nonav = true, nocargo = true },
	nasg = { nonav = true },
	national = { cat = 'National Teams', nonav = true, nocargo = true },
}

local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge()
	return Infobox(args):run()
end

function Infobox:castArgs(args)
	args.region = Region(args.region)
	
	args.special = args.special and args.special:lower()
	args._title = args.name
	
	args.isdisbanded = util_args.castAsBool(args.isdisbanded)
	args.neworg = util_args.norm(args.neworg)
	args.isrenamed = util_args.norm(args.isrenamed)
	args.isinactive = util_args.castAsBool(args.isinactive)
	
	args._isDisbanded = args.isdisbanded or args.neworg or args.isrenamed or args.isinactive
	args._special = args.special and SPECIAL[args.special] or {}
	
	args.orgcountry = Country(args.orgcountry)
	args.country = Country(args.country)
	args.foundedcountry = Country(args.foundedcountry)
	args.effective_country = args.country:_or(args.orgcountry)
	
	args.suppress_roster_photo = util_args.castAsBool(args.suppress_roster_photo)
end

function Infobox:validateArgs(args)
	if args.special and not SPECIAL[args.special] then
		error('Invalid special specification - perhaps you need to add new preload at Module:Infobox/Team?')
	end
end

function Infobox:getProcessed(args)
	local processed = self:super('getProcessed', args)
	processed.image = h.getImage(args.image, processed.pagename)
	return processed
end

function Infobox:getDisplay(args, processed)
	local display = self:super('getDisplay', args, processed)
	local rosterPhoto = h.queryForRosterPhoto(args)
	local rosterImage = rosterPhoto and ('[[File:%s|center|320px]]'):format(rosterPhoto)
	local tbl = {
		title = args.name or processed.pagename,
		image = not processed.isLowContent and processed.image,
		notice = h.makeNotice(args),

		['Org Location'] = args.orgcountry:flair(),
		['Team Location'] = args.country:flair(),
		FoundedIn = args.foundedcountry:flair(),
		Region = args.region:flair(),
		Owner = args.owner,
		HeadCoach = args.headcoach,
		Sponsor = args.sponsor,
		Partner = args.partner,
		Affiliated = h.getAffiliated(args),
		
		history = h.makeHistory(args),
		
		rosterimage = args.rosterphoto and ('[[File:%s|center|320px]]'):format(args.rosterphoto) or rosterImage,
		
		class = h.getClass(args, processed)

	}
	return Infobox.mergeDisplay(display, tbl)
end

function h.getAffiliated(args)
	if not args.affiliated and not args.academy then
	elseif not args.academy then
		return m_team.plainlinked(args.affiliated)
	elseif not args.affiliated then
		return m_team.plainlinked(args.academy)
	else
		local links = { 
			m_team.plainlinked(args.affiliated),
			m_team.plainlinked(args.academy),
		}
		return util_table.concat(links, '<br>')
	end
end

-- might need to rewrite this to depend on the teamLink variable instead of the actual pagename
function h.getImage(image, pagename)
	if image and mw.title.makeTitle('Media', image).exists then
		return image
	end
	local profile = ('%slogo profile.png'):format(pagename)
	if mw.title.makeTitle('Media', profile).exists then
		return profile
	end
	local square = ('%slogo square.png'):format(pagename)
	if mw.title.makeTitle('Media', square).exists then
		return square
	end
	return NOIMAGE
end

function h.queryForRosterPhoto(args)
	if args._isDisbanded then return nil end
	if args.suppress_roster_photo then return nil end
	local query = {
		tables = {
			'TeamRedirects=TR',
			'TeamRosterPhotos=TP',
			'Tournaments=T',
		},
		join = {
			'TR.AllName=TP.Team',
			'TP.Tournament=T.OverviewPage',
		},
		where = h.getRosterPhotoWhere(args),
		fields = { 'TP.FileName', 'TP.Hide[boolean]' },
		orderBy = 'COALESCE(TP.SortDate, T.DateStartFuzzy, T.Date) DESC',
	}
    return h.getRosterPhotoIfVisible(util_cargo.getOneRow(query) or {})
end

function h.getRosterPhotoIfVisible(row)
    if row.Hide then return nil end
    return row.FileName
end

function h.getRosterPhotoWhere(args)
	local where = {
		('TR._pageName="%s"'):format(mw.title.getCurrentTitle().text),
		('TP._pageName IS NOT NULL'),
	}
	return where
end

function h.makeNotice(args)
	if args.isdisbanded then
		return 'Team has disbanded.'
	elseif args.neworg then
		return ('[[%s|Roster has joined a new organization.]]'):format(args.neworg)
	elseif args.isrenamed then
		return ('[[%s|Team has renamed.]]'):format(args.isrenamed)
	elseif args.isinactive then
		return 'Team is inactive.'
	else
		return false
	end
end

function h.makeHistory(args)
	if not args.created then return false end
	local tbl = mw.html.create('table')
		:addClass('infobox-subtable')
	h.makeHistoryLine(tbl, 'Created', args.created, args.created2 and 1)
	h.makeHistoryLine(tbl, 'Disbanded', args.disbanded, args.created2 and 1)
	local i = 2
	while args['created' .. i] do
		h.makeHistoryLine(tbl, 'Created', args['created' .. i], i)
		h.makeHistoryLine(tbl, 'Disbanded', args['disbanded' .. i], i)
		i = i + 1
	end
	return tostring(tbl)
end

function h.makeHistoryLine(tbl, label, content, i)
	if not content then return end
	tbl:tag('tr')
		:tag('th')
			:addClass('teamdate')
			:wikitext(label .. (i and (' (%s)'):format(i) or ''))
		:done()
		:tag('td')
			:wikitext(content)
		:done()
	:done()
	return
end

function h.getClass(args, processed)
	local listOfClasses = {
		processed.isLowContent and 'infobox-low-content',
	}
	return util_table.concat(listOfClasses, ' ')
end

function Infobox:getCargo(args, processed)
	local cargo = self:super('getCargo', args, processed)
	if args._special.nocargo then
		return cargo
	end
	cargo[#cargo+1] = {
		'',
		_table = 'Teams',
		Name = args.name,
		OverviewPage = mw.title.getCurrentTitle().text,
		Short = m_team.short(mw.title.getCurrentTitle().text),
		OrganizationPage = args.organization,
		Location = args.orgcountry,
		TeamLocation = args.country,
		Region = args.region,
		Image = args.image,
		Twitter = args.twitter,
		Website = args.website,
		Youtube = args.youtube,
		Facebook = args.facebook,
		Instagram = args.instagram,
		Discord = args.discord,
		Snapchat = args.snapchat,
		Vk = args.vk,
		Subreddit = args.subreddit,
		RosterPhoto = args.rosterphoto,
		IsDisbanded = args._isDisbanded,
		IsLowercase = processed.lc,
		RenamedTo = h.renamedTo(args.isrenamed),
	}
	return cargo
end

function h.renamedTo(isrenamed)
	if not isrenamed then return nil end
	return m_team.teamlinkname(isrenamed)
end

function Infobox:getVariables(args, processed)
	local variables = self:super('getVariables', args, processed)
	local tbl = {
		suppressorgnavbox = args._special.nonav,
		introSentence = h.getIntroSentence(args, processed),
		isdisbanded = args.isdisbanded or args.neworg or args.isrenamed or args.isinactive
	}
	return util_table.merge(variables, tbl)
end

function h.getIntroSentence(args, processed)
	local data = mw.clone(args)
	data.name = args.name or processed.pagename
	return IntroSentence(data)
end

function Infobox:getCategories(args, processed)
	local categories = self:super('getCategories', args, processed)
	local tbl = {
		args.region:exists() and (args.region:get('adjective') .. ' Teams') or 'Teams Without Region In Infobox',
		'Teams',
		args._isDisbanded and 'Inactive Teams' or 'Active Teams',
		args.effective_country:exists() and ('%s Teams'):format(args.effective_country:get('adjective')),
		args._special.cat,
	}
	return util_table.mergeArrays(categories, tbl)
end

return p
Advertisement