Module:CurrentTournaments

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_html = require('Module:HtmlUtil') local util_map = require('Module:MapUtil') 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 RegionList = require('Module:RegionList') 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.init('CurrentTournaments') local args = util_args.merge h.setConstants(args) h.castArgs(args) local result = h.makeAndRunQuery(args) local processed = h.processResult(result) return h.makeOutput(processed) end

function h.setConstants(args) MAX_LINES = args.max_lines or MAX_LINES end

function h.castArgs(args) args.regions = RegionList(args.regions) end

function h.makeAndRunQuery(args) local query = { tables = 'Tournaments=T', fields = { 'T.DateStart', 'T.Date', 'T.League', 'T.LeagueIconKey', 'T.Region', 'T._pageName', 'T.Name' },		where = { util_cargo.whereFromCompoundEntity('T.Region = "%s"', args.regions) },		orderBy = 'T.DateStart DESC', limit = 999, }	return util_cargo.queryAndCast(query) 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 elseif (e or not row.Date) and row.DateStart then current[#current+1] = row elseif row.Date then past[#past+1] = row end end return processed end

function h.sortSectionsForCrop(processed) util_sort.tablesByKeys(processed.upcoming, 'DateStart', true) util_sort.tablesByKeys(processed.current, 'DateStart') util_sort.tablesByKeys(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, row in ipairs(section) do			row.dates = h.formatDates(row.DateStart or row.Date, row.Date) row.tournament = ('%s %s'):format(s.LeagueSprite(row.LeagueIconKey or row.League), util_text.link(row._pageName, row.Name)) row.button = tostring(PopupButton.standings(nil, row._pageName)) 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') :addClass('hoverable-rows') 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.printForInclusion(v), #COLUMNS, 'current-tournaments-header') end util_html.printRowsByList(tbl, data, COLUMNS) end return p