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

Documentation for this module may be created at Module:ScheduleNavboxTeamHelper/doc

local util_cargo = require("Module:CargoUtil")
local util_time = require('Module:TimeUtil')
local lang = mw.getLanguage('en')
local p = {}

function p.where(team)
	local tbl = {
		('(Team1="%s" OR Team2="%s") AND DateTime_UTC > NOW()'):format(team, team),
		'Player1 IS NULL',
		'Player2 IS NULL',
	}
	return util_cargo.concatWhere(tbl)
end

function p.makeNavboxArgs(args, results, team, n)
	local events = p.makeNavboxList(results, team)
	if not events then
		return nil
	end
	args.attr = string.format('data-expiration="%s"',events.time)
	for k, v in ipairs(events) do
		args['group' .. k + n] = events[v].name .. ' Matches'
		args['list' .. k + n] = p.makeNavboxRow(events[v].data)
		args['tr' .. k + n .. 'class'] = 'upcoming-matches schedule-navbox-row'
		args['tr' .. k + n .. 'attr'] = string.format('data-expiration="%s"',events[v].time)
	end
	return true
end

function p.makeNavboxRow(row)
	local tbl = mw.html.create('div'):addClass('hlist hlist-tall')
	for _, v in ipairs(row) do
		tbl:tag('li')
			:addClass('upcoming-matches')
			:attr('data-expiration',v.attr)
			:wikitext(v.wikitext)
	end
	return tostring(tbl)
end

function p.makeNavboxList(results, team)
	local resultsExist = false
	local events = {}
	local limit = 999
	local time
	for k, row in ipairs(results) do
		event = row.Tournament
		if events[event] then
			-- nothing
		else
			events[#events+1] = event
			events[event] = { name = row.EventLinked, data = {} }
		end
		local e = events[event] -- alias for convenience
		e.data[#e.data+1] = p.formatListEntry(row, team)
		time = util_time.unix(row.DateTime)
		e.time = time
		resultsExist = resultsExist or util_time.isInFuture(row.DateTime)
		if k == limit then
			break
		end
	end
	events.time = time -- we are going in order so this will be the biggest value
	return resultsExist and events
end

-- this just makes one item in the flatlist
function p.formatListEntry(row, team)
	local tbl = {
		p.otherTeam(row, team),
		util_time.countdown(row.DateTime)
	}
	local div = mw.html.create('div')
		:css({ display = "table-cell", ['text-align'] = 'center'})
		:wikitext(table.concat(tbl,'<br>'))
	return {
		wikitext = tostring(div),
		attr = util_time.unix(row.DateTime)
	}
end

-- get the other team
function p.otherTeam(row, team)
	local key
	if row.Team1 == team then
		key = 'Team2'
	else
		key = 'Team1'
	end
	if row[key] == '' or not row[key] or lang:lc(row[key]) == 'tbd' then
		return 'TBD'
	end
	return row[key .. 'Formatted']
end

return p
Advertisement