Module:CurrentTournaments

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_html = require('Module:HTMLUtil') local util_sort = require('Module:SortUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_time = require('Module:TimeUtil') local m_region = require('Module:Region') local Sprite = require('Module:Sprite').sprite local PopupButton = require('Module:PopupButton') local i18n = require('Module:I18nUtil')

local lang = mw.getLanguage('en')

local SECTIONS = { 'upcoming', 'current', 'past' } local COLUMNS = { 'dates', 'tournament', 'button' }

local MAX_LINES = 23

local s = {} function s.LeagueSprite(id) return Sprite{ id, type = 'League' } end local h = {} local p = {} function p.main(frame) i18n.initGlobalFromFile('CurrentTournaments') local args = util_args.merge(true) local result = h.doQuery(args) local processed = h.processResult(result) return h.makeOutput(processed) end

function h.doQuery(args) local query = { tables = 'Tournaments', fields = { 'DateStart', 'Date', 'League', 'Region', '_pageName', 'Name' },		where = h.makeWhere(args), orderBy = 'DateStart DESC', limit = 999 }	return util_cargo.queryAndCast(query) end

function h.makeWhere(args) local tbl = { ('(%s)'):format(util_args.splitMapConcat(args.regions, '%s*,%s*', h.regionWhere, ' OR ')) }	return util_table.concat(tbl, ' AND ') end

function h.regionWhere(str) return ('(Region = "%s")'):format(m_region.medium(str)) end

function h.processResult(result) local processed = h.splitByTime(result) h.sortSectionsForCrop(processed) h.cropSections(processed) h.sortSectionsForDisplay(processed) h.createDisplays(processed) return processed end

function h.splitByTime(result) local processed = { upcoming = {}, current = {}, past = {} } local upcoming = processed.upcoming local current = processed.current local past = processed.past for _, row in ipairs(result) do		local s = row.DateStart and util_time.dateIsInFuture(row.DateStart) local e = row.Date and util_time.dateIsInFutureOrNow(row.Date) if s and row.DateStart then upcoming[#upcoming+1] = row._pageName upcoming[row._pageName] = row elseif (e or not row.Date) and row.DateStart then current[#current+1] = row._pageName current[row._pageName] = row elseif row.Date then past[#past+1] = row._pageName past[row._pageName] = row end end return processed end

function h.sortSectionsForCrop(processed) util_sort.dictByKeys(processed.upcoming, 'DateStart', true) util_sort.dictByKeys(processed.current, 'DateStart') util_sort.dictByKeys(processed.past, 'Date') end

function h.cropSections(processed) local n = MAX_LINES - #processed.current util_table.crop(processed.upcoming, n)	util_table.crop(processed.past, n - #processed.upcoming) end

function h.sortSectionsForDisplay(processed) util_table.reverseInPlace(processed.upcoming) end

function h.createDisplays(processed) for k, section in pairs(processed) do		for i, page in ipairs(section) do			local row = section[page] row.dates = h.formatDates(row.DateStart or row.Date, row.Date) row.tournament = ('%s %s'):format(s.LeagueSprite(row.League), util_text.link(row._pageName, row.Name)) row.button = tostring(PopupButton.standings(nil, row._pageName, true)) end end end

function h.formatDates(s, e)	return ('%s - %s'):format(h.formatDate(s), h.formatDate(e)) end

function h.formatDate(date) if not date then return '??' else return lang:formatDate('M j', date) end end

function h.makeOutput(processed) local tbl = mw.html.create('table') :addClass('current-tournaments') for _, v in ipairs(SECTIONS) do		h.printSection(tbl, v, processed[v]) end return tbl end

function h.printSection(tbl, v, data) if #data > 0 then util_html.makeColspanHeader(tbl, i18n.printForInclusion(v), #COLUMNS, 'current-tournaments-header') end util_html.printRowsByListInDict(tbl, data, COLUMNS) end return p