Call of Duty Esports Wiki
Call of Duty Esports Wiki

To edit the documentation or categories for this module, click here. This module has an i18n file. Click here to edit it.

Dependencies: {{FrontpageMatches/Settings}}m


local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_form = require('Module:FormUtil')
local util_html = require('Module:HtmlUtil')
local util_table = require('Module:TableUtil')
local util_time = require('Module:TimeUtil')
local util_text = require("Module:TextUtil")
local util_toggle = require("Module:ToggleUtil")
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')

local SETTINGS = require('Module:FrontpageMatches/Settings')
local SECTION
local DEFAULT_LIMIT = 18

local FORM_INFO = {
	form = 'SpoilerFreeSchedule',
	template = 'SFS'
}

local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge()
	i18n.init('FrontpageMatches')
	SECTION = SETTINGS.sections[tonumber(args[1])]
	local data = h.getData(args)
	return h.togglers(args), h.makeFullOutput(data, args)
end

function p.section(frame)
	local args = util_args.merge()
	i18n.init('FrontpageMatches')
	SECTION = SETTINGS.sections[tonumber(args[1])]
	local data = h.getData(args)
	return h.makeOutput(data, args)
end

-- togglers
function h.togglers(args)
	local output = mw.html.create('div')
		:addClass('titletabs-tabs')
	for i, section in ipairs(SETTINGS.sections) do
		h.printToggler(output, section, i == tonumber(args.default))
	end
	return output
end
function h.printToggler(output, section, isDefault)
	local div = output:tag('div')
		:addClass('titletabs-tab')
		:addClass('fpmatches-togglers')
		:attr('data-replaceid', 'fpmatches-section-' .. section)
		:attr('data-templatecode', ('Project:Matches Section/%s'):format(section))
		:attr('data-toggler-show', 'fpmatches-section-' .. section)
		:attr('data-toggler-hide', 'fpmatches-section')
		:attr('data-toggle-section', 'fpmatches-togglers')
		:wikitext(SETTINGS[section].title)
	if isDefault then
		div:addClass('active')
			:addClass('optionfromlist-toggler')
	else
		div:addClass('optionfromlist-toggler-lazy')
	end
end

function h.getData(args)
	local query = {}
	util_table.merge(query, SETTINGS.cargo, SETTINGS[SECTION].cargo)
	query.limit = h.getLimit(args)
	if SETTINGS[SECTION].min_max_query then
		query.where = util_cargo.makeMinMaxQuery(query, '_pageName', 'DateTime_UTC', 'MIN')
	end
	return util_cargo.queryAndCast(query)
end

function h.getLimit(args)
	return args.limit or DEFAULT_LIMIT
end

function h.makeFullOutput(data, args)
	local output = mw.html.create()
	for _, v in ipairs(SETTINGS.sections) do
		if v == SECTION then
			output:node(h.makeOutput(data, args))
		else
			output:node(h.makeEmptyOutput(v))
		end
	end
	return output
end

function h.makeEmptyOutput(section)
	local output = mw.html.create('div')
		:attr('id', 'fpmatches-section-' .. section)
		:addClass('fpmatches-section-' .. section)
		:addClass('fpmatches-section')
		:addClass('toggle-section-hidden')
	return output
end

function h.makeOutput(data, args)
	local output = mw.html.create('div')
		:addClass('fpmatches-section')
		:attr('id', 'fpmatches-section-' .. SECTION)
		:addClass('fpmatches-section-' .. SECTION)
	h.printSection(output, data, args)
	return output
end

function h.printSection(div, data, args)
	div:wikitext(string.format(SETTINGS[SECTION].sentence or '', h.getLimit(args)))
	local tbl = div:tag('table')
		:addClass('plainlinks')
		:addClass('frontpage-upcoming-matches')
		:addClass('hoverable-rows')
	-- table headings
	tr = tbl:tag('tr')
	for _, field in ipairs(SETTINGS[SECTION].fields) do
		tr:tag('th'):wikitext(i18n.print(field))
	end
	-- rest of the table
	for n, row in ipairs(data) do
		row.N = n
		tbl:node(h.makeRow(row))
	end
end

function h.makeRow(row)
	local formatted = h.parseData(row)
	local tbl = mw.html.create('tr')
	for _, item in ipairs(SETTINGS[SECTION].fields) do
		tbl:tag('td')
			:addClass(SETTINGS.classes[item])
			:wikitext(formatted[item])
	end
	return tbl
end

function h.parseData(row)
	local formatted = {
		tournament = h.formatTournament(row.Tournament, row.ShownName),
		tournamentLinks = h.tournamentLinks(row.Tournament),
		teams = tostring(h.formatTeams(row)),
		winner = tostring(h.formatWinner(row)),
		countdown = tostring(h.formatCountdown(row)),
		vod = h.formatVod(row),
	}
	return formatted
end

function h.formatTournament(tournament, shownname)
	return util_text.intLink(tournament, shownname)
end

function h.tournamentLinks(tournament)
	local tbl = {
		tostring(h.matchListPopup(tournament)),
		util_esports.calendarExtLink(util_form.fullURL(FORM_INFO,{tournament})),
	}
	return util_table.concat(tbl, ' ')
end

function h.matchListPopup(tournament, shownname)
	local button = util_toggle.popupButtonLazy(
		nil,
		'matchlist',
		('MatchListOneTab|page=%s'):format(tournament)
	)
	return tostring(button.tbl)
end

function h.formatTeams(row)
	local tbl = mw.html.create('table')
	if h.hasEnoughInfoToShowTeams(row) then
		tbl:node(util_html.vsAlign(h.getTeamDisplay(row, 1), h.getTeamDisplay(row, 2)))
	else
		tbl:wikitext(h.getRoundDisplay(row.round))
	end
	return tbl
end

function h.getRoundDisplay(round)
	return ("''%s''"):format(round or 'TBD')
end

function h.hasEnoughInfoToShowTeams(row)
	if row.Player1 or row.Player2 then return true end
	if row.Team1:exists() and row.Team1:get() ~= 'TBD' then return true end
	if row.Team2:exists() and row.Team2:get() ~= 'TBD' then return true end
	
end

function h.getTeamDisplay(row, n)
	if not row['Player' .. n] then return row['Team' .. n]:flair{len='short'} end
	return ('<span class="team-object">%s%s</span>'):format(
		row['Team' .. n]:image{len='short'},
		util_esports.playerDisplay(row['Player' .. n])
	)
end

function h.formatWinner(row)
	if not row.Winner then return nil end
	if row.Winner == 0 then
		return'<span style="font-size:80%">DRAW</span>'
	elseif row.Winner == -1 then
		return 'FFs'
	else
		return mw.html.create():wikitext(row['Team' .. row.Winner]:imagelink{size='medium'})
	end
end

function h.formatCountdown(row)
	local lang = mw.getLanguage('en')
	local tbl = mw.html.create()
	if not row.TimeEntered then
		tbl:wikitext(lang:formatDate('d M',row.DateTime))
	else
		tbl:wikitext(util_time.countdown(row.DateTime))
	end
	
	if not row.Stream then
		return tbl
	else
		link = ('[%s %s]'):format(row.Stream, tostring(tbl))
		local linktbl = mw.html.create('span'):addClass('plainlinks'):wikitext(link)
		return linktbl
	end
end

function h.formatVod(row)
	if not row.Vod then return nil end
	return ('[%s VOD]'):format(row.Vod)
end

return p