Module:TournamentPlayerInformation

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_form = require('Module:FormUtil') local util_text = require('Module:TextUtil') local i18n = require('Module:i18nUtil')

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

local COLUMNS = { 'TournamentTeam', 'TournamentRole', 'ID', 'NameFull', 'Team', 'Role', 'Stream', 'Twitter', 'Birthdate', 'Age' }

local p = {} local h = {} function p.main(frame) local args = util_args.merge i18n.init('TournamentPlayerInformation') local overviewPage = util_esports.getOverviewPage(args.page) local result = h.makeAndRunQuery(args, overviewPage) util_map.rowsInPlace(result, h.formatOneRow) return h.makeOutput(result, overviewPage) end

function h.makeAndRunQuery(args, page) local query = { tables = { 'TournamentPlayers=TP', 'PlayerRedirects=PR1', 'Players=P', 'PlayerRedirects=PR2', 'PlayerPronunciationFiles=PPF', 'Alphabets=A', },		join = { 'TP.Link=PR1.AllName', 'PR1.OverviewPage=P.OverviewPage', 'P.OverviewPage=PR2.OverviewPage', 'PR2.AllName=PPF.Player', 'P.NameAlphabet=A.Alphabet', },		fields = { 'P._pageName=_pageName', 'P.ID=ID', 'TP.Link=Link2', 'CONCAT(CASE WHEN A.IsTransliterated="1" THEN P.NameFull ELSE P.Name END)=NameFull', 'P.Team [team]', 'P.TeamLast [team]', 'COALESCE(P.Country,TP.Flag)=Country [Country]', 'P.Role=Role [Role]', 'P.Stream', 'P.Twitter', 'P.Birthdate', 'P.Age', 'TP.Team=TournamentTeam [team]', 'TP.Role=TournamentRole [RoleList]', 'PPF._pageName=PronunciationFile', },		where = h.getWhere(page), orderBy = 'TP.Team, TP.N_PlayerInTeam', limit = 999, groupBy = 'P.OverviewPage', }	return util_cargo.queryAndCast(query) end

function h.getWhere(page) local tbl = { ('TP.OverviewPage="%s"'):format(page), }	return table.concat(tbl, ' AND ') end

function h.formatOneRow(row) local id = util_esports.playerDisplay(row.ID or row.Link2) row.ID = h.getIdDisplay(row) row.ID2 = util_esports.playerDisplay(row.Link2) row.Team = row.Team:flairlink{len='medium'} row.TeamLast = row.TeamLast:flairlink{len='medium'} row.TournamentTeam = row.TournamentTeam:flairlink{len='medium'} row.Stream = row.Stream and string.format('[%s Stream]', row.Stream) row.Twitter = row.Twitter and string.format(		'%s',		row.Twitter,		row.Twitter	) row.Askfm = row.Askfm and string.format(		'%s',		row.Askfm,		row.Askfm	) row.attrs = { Role = { ['data-sort-value'] = row.Role and row.Role:sortnumber },		TournamentRole = { ['data-sort-value'] = row.TournamentRole and row.TournamentRole:sortnumber }	}	row.TournamentRole = row.TournamentRole and row.TournamentRole:images('', { size = 15 }) row.Role = row.Role and row.Role:flair(nil, { size = 15 }) return end

function h.getIdDisplay(row) local tbl = {} -- first thing could be nil tbl[#tbl+1] = row.Country:image tbl[#tbl+1] = util_text.intLink(row._pageName or row.Link2 or id, row.ID or row.ID2) tbl[#tbl+1] = h.makePPFButton(row.PronunciationFile) return table.concat(tbl, ' ') end

function h.makePPFButton(file) if not file then return end return tostring(mw.getCurrentFrame:callParserFunction{			name = '#tag:sm2',			args = { file:gsub('File:', '') }		}) end

function h.makeOutput(result, page) local output = mw.html.create local tournament = h.getTournamentName(page) output:wikitext(i18n.print('introSentence', page, tournament)) h.printTable(output, result) local url = util_form.fullURL(FORM_INFO, { page = page }) output:wikitext(i18n.print('permalink', url)) return output end

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

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

return p