Module:TournamentList

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_html = require('Module:HtmlUtil') local util_map = require("Module:MapUtil") local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_toggle = require('Module:ToggleUtil') local util_vars = require('Module:VarsUtil')

local i18n = require('Module:i18nUtil') local Sprite = require('Module:Sprite').sprite local m_country = require('Module:Country') local m_league = require('Module:League') local m_region = require('Module:Region') local m_team = require('Module:Team')

local COLUMNS = { 'Start', 'End', 'Tournament', 'Prize' }

util_cargo.setStoreNamespace('')

local s = {} function s.LeagueSprite(id) return Sprite{ id, type = 'League' } end local h = {} local p = {} function p.main(frame) local args = util_args.merge i18n.init('TournamentList') h.castArgs(args) h.setCols(args) local eventsData = h.geteventsData(args) if #eventsData == 0 then return i18n.print('noneFound') end local resultsLookup = h.reformatResultsData(eventsData) h.removeDuplicateEvents(eventsData) h.addResultsToEvents(eventsData, resultsLookup) h.formatData(eventsData) return h.makeOutput(eventsData) end

function h.castArgs(args) args.league = util_map.splitAndConcat(args.league, nil, m_league.leaguename, ',') end

function h.setCols(args) if util_args.castAsBool(args.qualifiers) then COLUMNS[#COLUMNS+1] = 'Qualified' else COLUMNS[#COLUMNS+1] = 'First' COLUMNS[#COLUMNS+1] = 'Second' end if util_args.castAsBool(args.debug) then COLUMNS[#COLUMNS+1] = '_pageName' end end

function h.geteventsData(args) local query = h.getEventQuery(args) return util_cargo.queryAndCast(query) end

function h.getEventQuery(args) local ret = { tables = { 'Tournaments=IT', 'TournamentResults=TR' }, join = 'IT._pageName=TR._pageName', fields = h.getEventFields, where = h.getEventsWhere(args), limit = args.limit, -- groupBy = 'IT._pageName,TR.Place_Number', orderBy = 'COALESCE(IT.Date, IT.DateStart, "3000-01-01" ) DESC, SplitNumber DESC, IsPlayoffs DESC', types = { Place_Number = 'number', Qualified = 'boolean', },	}	return ret end

function h.getEventFields return { 'IT.Name=Name', 'IT._pageName=OverviewPage', -- replace to OverviewPage 'IT.Prizepool=Prize', 'IT.DateStart', 'IT.Date', 'IT.Country', 'IT.Region', 'IT.League', 'COALESCE(IT.LeagueIconKey, IT.League)=LeagueIconKey', 'TR.Team=Team', 'TR.Place=Place', 'TR.Place_Number', 'TR.GroupName', 'IT._pageName', 'TR.Qualified', } end

function h.getEventsWhere(args) local tbl = { util_cargo.whereFromArgList('IT.Organizer="%s"', args.organizer), util_cargo.whereFromArgList('IT.Region="%s"', args.region), util_cargo.whereFromArgList('IT.League="%s"', args.league), '(TournamentLevel != "Showmatch" OR TournamentLevel IS NULL)', h.getQualifierWhereCondition(args) }	return util_cargo.concatWhere(tbl) end

function h.getQualifierWhereCondition(args) if util_args.castAsBool(args.qualifiers) then return ('TR.Qualified="1"') else local tbl = { '(TR.Place_Number="1" OR TR.Place_Number="2" OR TR.Place_Number IS NULL)', '(IT.IsQualifier = "0" OR IT.IsQualifier IS NULL)', }		return util_cargo.concatWhere(tbl) end end

function h.getResultsData(eventsData) local query = h.getResultsQuery(eventsData) return util_cargo.queryAndCast(query) end

function h.reformatResultsData(eventsData) local lookup = {} for _, row in ipairs(eventsData) do		h.addTeamToLookup(lookup, row) end return lookup end

function h.addTeamToLookup(lookup, row) util_table.initTable(lookup, row.OverviewPage) h.pushQualifiedTeam(lookup[row.OverviewPage], row) h.pushPlacedTeam(lookup[row.OverviewPage], row) h.pushGroupTeam(lookup[row.OverviewPage], row) end

function h.pushQualifiedTeam(lookupRow, row) if not row.Qualified then return end util_table.initTable(lookupRow, 'Qualified') lookupRow.Qualified[#lookupRow.Qualified+1] = row.Team end

function h.pushPlacedTeam(lookupRow, row) if row.GroupName or not row.Place_Number then return end lookupRow[row.Place_Number] = row.Team end

function h.pushGroupTeam(lookupRow, row) if not row.GroupName or not row.Place_Number then return end lookupRow.HasGroups = true util_table.initTable(lookupRow, 'Groups') util_table.initDict(lookupRow.Groups, row.GroupName) lookupRow.Groups[row.GroupName][row.Place_Number] = row.Team end

function h.removeDuplicateEvents(eventsData) local hash = {} for i, row in ipairs(eventsData) do		if hash[row.OverviewPage] then eventsData[i] = false else hash[row.OverviewPage] = true end end util_table.removeFalseEntries(eventsData) end

function h.addResultsToEvents(eventsData, resultsLookup) for _, row in ipairs(eventsData) do		h.addResultsToEventRow(row, resultsLookup) end end

function h.addResultsToEventRow(row, resultsLookup) if not resultsLookup[row.OverviewPage] then return end row.Team1 = resultsLookup[row.OverviewPage][1] row.Team2 = resultsLookup[row.OverviewPage][2] row.Qualified = resultsLookup[row.OverviewPage].Qualified row.HasGroups = resultsLookup[row.OverviewPage].HasGroups row.Groups = resultsLookup[row.OverviewPage].Groups end

function h.formatData(eventsData) for _, row in ipairs(eventsData) do		h.formatDataRow(row) end end

function h.formatDataRow(row) row.Start = row.DateStart row.End = row.Date row.Tournament = h.formatTournament(row) row.First = h.formatTeamOrGroupTeams(row, 1) row.Second = h.formatTeamOrGroupTeams(row, 2) row.Qualified = util_table.concat(row.Qualified, ', ', m_team.rightmedium) end

function h.formatTeamOrGroupTeams(row, n)	if not row.HasGroups then return m_team.rightmedium(row['Team' .. n]) end return h.formatGroupTeams(row, n) end

function h.formatGroupTeams(row, n)	local tbl = {} for i, group in ipairs(row.Groups) do		tbl[i] = row.Groups[group][n] end util_table.removeFalseEntries(tbl, 2) return util_table.concat(tbl, ' ', m_team.rightmedium) end

function h.formatTournament(row) local tbl = { row.League and s.LeagueSprite(row.LeagueIconKey), util_args.nilToFalse(row.Country) and m_country.onlyimage(row.Country), util_text.intLink(row.OverviewPage, row.Name), }	return util_table.concat(tbl, ' ') end

function h.makeOutput(eventsData) local output = mw.html.create h.printTable(output, eventsData) return output end

function h.printTable(output, eventsData) local tbl = output:tag('table') :addClass('wikitable') util_html.printHeaderFromI18n(tbl, COLUMNS) util_html.printRowsByList(tbl, eventsData, COLUMNS) end

return p