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_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 HEADINGS = { 'Start', 'End', 'Tournament', 'Prize', 'Location', 'First', 'Second' } local DEFAULT_LIMIT = 9999

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) i18n.initGlobalFromFile('TournamentList') local eventsData = h.geteventsData(args) local resultsLookup = h.reformatResultsData(eventsData) h.removeDuplicateEvents(eventsData) h.addResultsToEvents(eventsData, resultsLookup) h.formatData(eventsData) return h.makeOutput(eventsData) 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 = 'TR._pageName=IT._pageName', fields = h.getEventFields, where = h.getEventsWhere(args), limit = args.limit or DEFAULT_LIMIT, groupBy = 'IT._pageName,TR.Place_Number', orderBy = 'IT.Date DESC', types = { Place_Number = 'number', },	}	return ret end

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

function h.getEventsWhere(args) local tbl = { util_cargo.whereFromArg('IT.Organizer="%s"', args.organizer), util_cargo.whereFromArg('IT.Region="%s"', args.region), '(TR.Place_Number="1" OR TR.Place_Number="2" OR TR.Place_Number IS NULL)' }	return util_cargo.concatWhere(tbl) 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.addRowToLookup(lookup, row) end return lookup end

function h.addRowToLookup(lookup, row) if not row.Place_Number then return end h.initEventInLookup(lookup, row.OverviewPage) lookup[row.OverviewPage][row.Place_Number] = row.Team end

function h.initEventInLookup(lookup, overviewPage) if lookup[overviewPage] then return end lookup[overviewPage] = {} 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.First = resultsLookup[row.OverviewPage][1] row.Second = resultsLookup[row.OverviewPage][2] 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 = util_text.intLink(row.OverviewPage, row.Name) 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.headerFromI18n(tbl, HEADINGS) util_html.printRowsByList(tbl, eventsData, HEADINGS) end

return p