Module:FrontpageMatches

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 m_team = require('Module:Team') local i18n = require('Module:i18nUtil')

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

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

local h = {} local p = {} function p.main(frame) local args = util_args.merge(true) 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(true) 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)), }	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 not h.hasEnoughInfoToShowTeams(row) then tbl:wikitext(h.getRoundDisplay(row.round)) else tbl:node(util_html.vsAlign(h.getTeam1Display(row), h.getTeam2Display(row))) 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 and row.Team1 ~= 'TBD' then return true end if row.Team2 and row.Team2 ~= 'TBD' then return true end end

function h.getTeam1Display(row) if not row.Player1 then return m_team.leftshortlinked(row.Team1) end return util_esports.playerLinked(row.Player1) .. m_team.onlyimagelinkedshort(row.Team1) end

function h.getTeam2Display(row) if not row.Player2 then return m_team.rightshortlinked(row.Team2) end return m_team.onlyimagelinkedshort(row.Team2) .. util_esports.playerLinked(row.Player2) end

function h.formatWinner(row) if not row.Winner then return nil end if row.Winner == '0' then return' DRAW ' elseif row.Winner == '-1' then return 'FFs' else return mw.html.create:wikitext(m_team.onlyimagelinked(row['Team' .. row.Winner], { size = '48px'})) 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

return p