Module:SpoilerFreeSchedule

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_text = require('Module:TextUtil') local util_time = require('Module:TimeUtil') local util_toggle = require('Module:ToggleUtil') local util_vars = require('Module:VarsUtil') local m_team = require('Module:Team') local date = require('Module:Date') local phaseSettings = { { 		toggle = 'UpcomingNo', hidden = '_tog_object _toggle_inithide', when = '<', title = '[Show/Hide] Completed Matches' },	{		toggle = 'UpcomingYes', hidden = '', when = '>', title = '[Show/Hide] Upcoming Matches' } }

local TOGGLES = { order = { 'me', 'general' }, displays = { me = 'Localized', general = 'Non-Localized' }, }

local TIMEZONES = { 'PST', 'CET', 'KST' }

local COLUMNS = { 'Round', 'Match', 'Date (Local) ', 'YOU ', 'PST', 'CET', 'KST', 'Date 1', 'Date 2', 'PST', 'CET', 'KST','Countdown', 'VODs/Stream' }

local COLUMNS_TOGGLES = { nil, nil, 'me', 'me', 'me', 'me', 'me', 'general', 'general', 'general', 'general', 'general', 'me', nil }

local WIDTHS = { 75, 300, 120, 40, 40, 40, 40, 120, 120, 40, 40, 40, 85, 125 }

local PAGENAME

local h = {}

local p = {} function p.main(frame) local args = util_args.merge PAGENAME = util_esports.getOverviewPage(args[1]) local output = mw.html.create h.printOutputInitialText(output) local tbl = output:tag('table'):addClass('wikitable'):css('text-align','center') for _, phase in ipairs(phaseSettings) do		h.printPhaseStart(tbl, phase) local result = util_cargo.queryAndCast(h.makeQuery(phase.when)) for _, row in ipairs(result) do local tr = tbl:tag('tr'):addClass('_tog_object _toggle ' .. phase.toggle):addClass(phase.hidden) tr:tag('td'):wikitext(row.Tab or row.Round or ''):done :tag('td'):node(util_html.vsAlign(m_team.leftshortlinked(row.Team1), m_team.rightshortlinked(row.Team2))):done h.printDateAndTime(tr, row) h.printVodsOrStream(tr, row) end end return output end

function h.printOutputInitialText(output) output:wikitext(('Click here to view tournament page.'):format(PAGENAME)) util_html.clear(output) h.printCalendarExportFormLink(output) util_html.clear(output) h.printToggler(output) end

function h.printCalendarExportFormLink(output) local url = util_form.fullURL({ template = 'MCE', form = 'MatchCalendarExport' }, { PAGENAME }) output:wikitext(util_text.extLink(url, 'Click here'), ' to create a CSV export of this event to your calendar.') end

function h.printToggler(tbl) local div = tbl:tag('div') :addClass('toggle-button') util_toggle.printOptionFromListTogglers(div, TOGGLES) util_html.clear(tbl) end

function h.printPhaseStart(tbl, phase) tbl:tag('tr') :tag('th') :attr('colspan',#COLUMNS) :addClass('_tog_object _toggler-' .. phase.toggle) :wikitext(phase.title) h.printWidthRow(tbl) local tr = tbl:tag('tr'):addClass('_tog_object _toggle ' .. phase.toggle):addClass(phase.hidden) h.printHeadings(tr) end

function h.printWidthRow(tbl) local tr = tbl:tag('tr'):addClass('empty-width-row') for i, v in ipairs(WIDTHS) do local td = tr:tag('td'):css('width', v .. 'px'):wikitext(' ') if COLUMNS_TOGGLES[i] then util_toggle.oflCellClasses(td, TOGGLES, COLUMNS_TOGGLES[i]) end end end

function h.printHeadings(tr) for i, v in ipairs(COLUMNS) do		local th = tr:tag('th'):wikitext(v) if COLUMNS_TOGGLES[i] then util_toggle.oflCellClasses(th, TOGGLES, COLUMNS_TOGGLES[i]) end end end

function h.makeQuery(when) return { tables = { 'MatchSchedule=MS', 'MatchScheduleGame=MSG' }, join = { 'MS.UniqueMatch=MSG.UniqueMatch' }, fields = { 'MS.Team1', 'MS.Team2', 'MS.Tab', 'MS.Round', 'MS.DateTime_UTC=UTC', 'MS.Stream', 'MS.DST', 'MS.HasTime', 'MS.BestOf', 'MS.UniqueMatch', },		where = ('MS.OverviewPage="%s" AND MS.DateTime_UTC %s NOW'):format(PAGENAME, when), groupBy = '', orderBy = 'MS.DateTime_UTC', types = { HasTime = 'boolean', },		oneToMany = { groupBy = { 'UniqueMatch' }, fields = { VODs = 'COALESCE(MSG.VodPB,MSG.VodGameStart,MSG.Vod)=VOD', }, },	} end

function h.printDateAndTime(tr, row) if not row.UTC or row.UTC == '' then tr:tag('td'):attr('colspan','6'):wikitext('TBD') else local td = tr:tag('td'):wikitext(util_time.dateInLocal(row.UTC)) util_toggle.oflCellClasses(td, TOGGLES, 'me') if not row.HasTime then tr:tag('td'):attr('colspan','4'):wikitext('TBD') else local utc = tr:tag('td'):wikitext(util_time.timeInLocal(row.UTC)) util_toggle.oflCellClasses(utc, TOGGLES, 'me') local tz_data = h.getTZData(row) h.addTZToRow(tr, tz_data) local dates = h.getAbsoluteDates(row.UTC) h.addAbsDates(tr, dates) h.addTZToRowUnlocalized(tr, tz_data, dates) end local cd = tr:tag('td') :wikitext(util_time.countdown(row.UTC)) util_toggle.oflCellClasses(cd, TOGGLES, 'me') end end

function h.getTZData(row) util_time.addTimezonesToRowFromUTC(row) local tbl = {} for _, v in ipairs(TIMEZONES) do		local date = row[v]:match('(%d%d%d%d%-%d%d%-%d%d)') local time = row[v]:match('(%d%d:%d%d)') tbl[v] = { date = date, time = time } end return tbl end

function h.addTZToRow(tr, data) for _, v in ipairs(TIMEZONES) do		local td = tr:tag('td') :wikitext(data[v].time) util_toggle.oflCellClasses(td, TOGGLES, 'me') end end

function h.getAbsoluteDates(str) local utc_time = util_time.strToTime(str) local utc_midnight = mw.clone(utc_time) utc_midnight.hour = 0 utc_midnight.min = 0 utc_midnight.sec = 0 local dif = os.difftime(os.time(utc_time), os.time(utc_midnight)) local dates = {} if dif >= 43200 then -- 12 * 60 * 60 local today = date(utc_time) dates[1] = today:fmt('%Y-%m-%d') local tomorrow = today:adddays(1) dates[2] = tomorrow:fmt('%Y-%m-%d') else local today = date(utc_time) dates[2] = today:fmt('%Y-%m-%d') local yesterday = today:adddays(-1) local y, m, d = yesterday:getdate dates[1] = yesterday:fmt('%Y-%m-%d') end return dates end

function h.addAbsDates(tr, dates) for i, v in ipairs(dates) do		local td = tr:tag('td') :addClass('sfs-date' .. i)			:wikitext(v) util_toggle.oflCellClasses(td, TOGGLES, 'general') end end

function h.addTZToRowUnlocalized(tr, data, dates) for _, v in ipairs(TIMEZONES) do		local td = tr:tag('td') :wikitext(data[v].time) :attr('data-date', data[v].date) util_toggle.oflCellClasses(td, TOGGLES, 'general') if data[v].date == dates[1] then td:addClass('sfs-date1') elseif data[v].date == dates[2] then td:addClass('sfs-date2') end end end

function h.printVodsOrStream(tr, row) if not row.Stream and not next(row.VODs) then tr:tag('td'):wikitext('Stream') return elseif #row.VODs == 0 then tr:tag('td'):wikitext(string.format('[%s Stream]', row.Stream)) return end tr:tag('td') :addClass('spoiler-free-vods') :wikitext(h.getVodsPadded(row.VODs, row.BestOf)) end

function h.getVodsPadded(vods, bestOf) local links = {} for i = 1, bestOf do		links[i] = util_text.ambiguousLink(vods[i] and vods[i].VOD or 'Main Page', i)	end return util_table.concat(links, '') end

return p