Module:CurrentTournaments

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') 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 m_region = require('Module:Region') local League = require('Module:League').league local PopupButton = require('Module:PopupButton')

local lang = mw.getLanguage('en')

local SECTIONS = { 'upcoming', 'current', 'past' } local i18n = { upcoming = 'UPCOMING', current = 'CURRENT', past = 'PAST' } local COLUMNS = { 'dates', 'tournament', 'button' }

local MAX_LINES = 23 local END_DIFF = 24 * 60 * 60 -- seconds in future we have to be from the end of the tournament before removing it

local h = {}

function h.doQuery(args) local query = { tables = 'Tournaments', fields = { 'DateStart', 'Date', 'League', 'Region', '_pageName', 'Name' },		where = h.makeWhere(args), orderBy = 'DateStart DESC', limit = 100 }	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.isInFutureDate(row.DateStart) local e = row.Date and util_time.isInFutureDate(row.Date, END_DIFF) 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_cargo.sortByValueInTable(processed.upcoming, 'DateStart', true) util_cargo.sortByValueInTable(processed.current, 'DateStart') util_cargo.sortByValueInTable(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(League{row.League,'onlyimage', size = '20px'}, util_text.printLink(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.printColspanHeader(tbl, i18n[v], #COLUMNS, 'current-tournaments-header') end util_html.printRowsByListInDict(tbl, data, COLUMNS) end

local p = {} function p.main(frame) local args = util_args.merge(true) local result = h.doQuery(args) local processed = h.processResult(result) return h.makeOutput(processed) end return p