Module:TournamentPlayerInformation

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_form = require('Module:FormUtil') local util_text = require('Module:TextUtil') local m_team = require('Module:Team') local m_country = require('Module:Country')

local Sprite = require('Module:Sprite').sprite

local sep = '%s*,%s*'

local FORM_INFO = { form = 'TournamentPlayerInformation', template = 'TPI' }

local DEFAULTS = { tables = 'Players', groupBy = '_pageName', orderBy='ID', limit = '999' }

local FIELDS = { 'Players._pageName=_pageName', 'Players.ID=ID', 'TournamentPlayers.Name=ID2', 'TournamentPlayers.Link=Link2', 'Players.NameFull=NameFull', 'Players.Team=Team', 'Players.TeamLast=TeamLast', 'Players.Country=Country', 'TournamentPlayers.Flag=Flag2', 'Players.Role=Role', 'Players.Stream=Stream', 'Players.Twitter=Twitter', 'Players.Birthdate=Birthdate', 'Players.Age=Age', 'TournamentPlayers.TeamLink=TournamentTeam', 'TournamentPlayers.Role=TournamentRole' } local FIELDORDER = { 'TournamentTeam', 'TournamentRole', 'ID', 'NameFull', 'Team', 'Role', 'Stream', 'Twitter', 'Birthdate', 'Age' } local DISPLAY = { TeamLast = 'Last Team', NameFull = 'Name', Birthdate = 'Birthday', Askfm = 'AskFM', TournamentTeam = 'Team', Team = 'Current Team', TournamentRole = 'Role', }

local h = {}

function h.RoleSprite(id, text) return Sprite{ id, size = '15', type = 'Role', notext = not text, nolink = 'Yes', } end

function h.doQuery(args, page) local query = { tables = 'TournamentPlayers, PlayerRedirects=PR1, PlayerRedirects=PR2, Players', fields = FIELDS, where = h.getWhere(page), join = h.getJoin, orderBy = 'TournamentPlayers.TeamLink, TournamentPlayers.N', limit = 999, }	return util_cargo.queryAndCast(query) end

function h.getWhere(page) local tbl = { ('TournamentPlayers._pageName="%s"'):format(page), '(PR1.AllName=BINARY PR2.AllName OR PR1.AllName IS NULL)', '(PR1.AllName=BINARY TournamentPlayers.Link OR PR1.AllName IS NULL)' }	return table.concat(tbl, ' AND ') end

function h.getJoin local tbl = { 'TournamentPlayers.Link=PR1.AllName', 'PR1.AllName=PR2.AllName', 'PR2._pageName=Players._pageName' }	return table.concat(tbl,',') end

function h.formatResults(result) for i, row in ipairs(result) do		h.formatResult(row) end return end

function h.formatResult(row) local id = row.ID or row.ID2 local country = row.Country or row.Country2 row.ID = id and ('%s%s'):format(		country and (m_country.onlyimage(country) .. ' ') or '',		row._pageName or row.Link2 or id,		id	) row.Team = row.Team and m_team.rightmediumlinked(row.Team) row.TeamLast = row.TeamLast and m_team.rightmediumlinked(row.TeamLast) row.TournamentTeam = m_team.rightmediumlinked(row.TournamentTeam) row.Stream = row.Stream and string.format('[%s Stream]', row.Stream) row.TournamentRole = row.TournamentRole and util_args.splitMapConcat(row.TournamentRole, ',', h.RoleSprite, '') row.Role = row.Role and h.RoleSprite(row.Role, true) row.Twitter = row.Twitter and string.format(		'%s',		row.Twitter,		row.Twitter	) row.Askfm = row.Askfm and string.format(		'%s',		row.Askfm,		row.Askfm	) return end

function h.makeOutput(result, page) local output = mw.html.create local tournament = h.getTournamentName(page) output:wikitext(('The following chart shows the current status of players who participated in the following event: %s'):format(page, tournament)) h.printTable(output, result) local url = util_form.fullURL(FORM_INFO, { page = page }) output:wikitext(('Permalink to this query: %s'):format(url)) return output end

function h.getTournamentName(page) local query = { tables = 'InfoboxTournament', fields = 'Name', where = ('_pageName="%s"'):format(page) }	return util_cargo.getOneResult(query) end

function h.printTable(output, result) local tbl = output:tag('table') :addClass('cargoTable') :addClass('sortable') h.addHeading(tbl, FIELDORDER) for _, row in ipairs(result) do		local tr = tbl:tag('tr') for _, v in ipairs(FIELDORDER) do			tr:tag('td') :addClass('field_' .. v)				:wikitext(row[v]) end end end

function h.addHeading(tbl) local tr = tbl:tag('tr') for _, v in ipairs(FIELDORDER) do		tr:tag('th'):wikitext(DISPLAY[v] or v)	end return end

local p = {} function p.main(frame) local args = util_args.merge(true) local overviewPage = util_esports.getOverviewPage(args.page) local result = h.doQuery(args, overviewPage) h.formatResults(result) return h.makeOutput(result, overviewPage) end return p