Hello there! We are conducting a survey to better understand the user experience in making a first edit. If you have ever made an edit on Gamepedia, please fill out the survey. Thank you!

Module:ScheduleNavboxTeamHelper

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

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

function p.where(team)
	return string.format(
			'(Team1="%s" OR Team2="%s") AND DateTime_UTC > NOW()',
			team,
			team
		)
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