Module:PlayerResults

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_html = require('Module:HTMLUtil') local util_table = require('Module:TableUtil') local util_tournament = require('Module:TournamentUtil')

i18n = require('Module:I18nUtil')

local sep = '%s*;;%s*'

local m_team = require('Module:Team')

local PopupButton = require('Module:PopupButton') local Sprite = require('Module:Sprite').sprite local placement = require('Module:Placement')

local COLUMNS = { 'Date', 'Place', 'Event', 'LastResult', 'Team', 'Roster' }

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

function s.LeagueSprite(id) return Sprite{ id, type = 'League' } end

function h.doQuery(player, show, use_redirect_table, args) -- use_redirect_table will be false in self namespace local tbl = { tables = h.getTables(use_redirect_table), join = h.getJoin(use_redirect_table), where = h.getWhere(player, use_redirect_table, args), fields = h.getFields(player, use_redirect_table), groupBy = 'TournamentResults.UniqueLine', orderBy = 'TournamentResults.Date DESC', limit = h.getLimit(show) }	return util_cargo.queryAndCast(tbl) end

function h.getTables(redirects) local tbl = { 'TournamentRosters', 'TournamentResults', 'Tournaments' } if redirects then tbl[#tbl+1] = 'PlayerRedirects' end return tbl end

function h.getJoin(redirects) local tbl = { 'TournamentRosters.PageAndTeam = TournamentResults.PageAndTeam', 'TournamentResults._pageName=Tournaments._pageName' }	if redirects then tbl[#tbl+1] = 'TournamentRosters.RosterLinks HOLDS PlayerRedirects.AllName' end return util_table.concat(tbl, ', ') end

function h.getWhere(player, use_redirect_table, args) local tbl = { 'TournamentResults.PageAndTeam IS NOT NULL', 'TournamentRosters.PageAndTeam IS NOT NULL', use_redirect_table and ('PlayerRedirects._pageName="%s"'):format(player), use_redirect_table and 'TournamentRosters.RosterLinks HOLDS BINARY PlayerRedirects.AllName', not use_redirect_table and ('TournamentRosters.RosterLinks HOLDS BINARY "%s"'):format(player), args.minplacement and ('Place_Number <= "%s"'):format(args.minplacement) }	return util_table.concat(tbl,' AND ') end

function h.getFields(player, redirects) local tbl = { "TournamentResults.Team=Team", "TournamentResults.Prize_Markup=PrizeMarkup", "TournamentResults.Date=Date", "TournamentResults.Place=Place", "TournamentResults.Event=Event", "TournamentResults.Phase=Phase", "CONCAT(TournamentResults._pageName)=EventLink", "TournamentRosters.RosterLinks__full=RosterLinks", "TournamentRosters.Roster=RosterNames", "TournamentRosters.Roles=Roles", "TournamentResults.LastResult=LastResult", "TournamentResults.LastOpponent_Markup=LastOpponent", "Tournaments.League=League", }	if redirects then tbl[#tbl+1] = "CONCAT(PlayerRedirects.AllName)=ThisName" else tbl[#tbl+1] = ('CONCAT("%s")=ThisName'):format(player) end return tbl end

function h.getLimit(show) if show == 'overviewpage' then return 10 elseif show == 'everything' then return 500 end return nil end

function h.addLastResult(tr, row, team) local td = tr:tag('td'):addClass('achievements-lastresult-cell') local div = td:tag('div'):addClass('achievements-lastresult-outer') div:tag('span'):addClass('achievements-lastresult-inner'):wikitext(row.LastResult) div:wikitext(row.LastOpponent) local div_right = div:tag('div'):addClass('achievements-lastresult-right') PopupButton.tth(div_right, row.EventLink, row.Event, team) end

function h.getTeammates(namesstr, linksstr, rolesstr, this) local teammates = {} local names = mw.text.split(namesstr or '',sep) local links = mw.text.split(linksstr or '',sep) local roles =rolesstr and mw.text.split(rolesstr,sep) or '' for i,link in ipairs(links) do		local thisrole = util_args.splitMapConcat(roles[i] or '', nil, s.RoleSprite, nil) if mw.ustring.lower(link) == mw.ustring.lower(this) then teammates[#teammates+1] = ("%s %s"):format(thisrole, names[i]) else teammates[#teammates+1] = ("%s %s"):format(thisrole, link, names[i]) end end return table.concat(teammates, ", ") end

function h.intro(args) if args.show == 'overviewpage' then return "''" .. i18n.print('seeall', mw.title.getCurrentTitle.rootText) .. "''"	elseif args.show == 'everything' and not args.minplacement then return mw.getCurrentFrame:callParserFunction{ name = '#widget', args = { 'FilterTournamentResults' } }	end return '' end

local p = {}

function p.playerResults( frame ) i18n.initGlobalFromFile('PlayerResults') local args = util_args.merge(true) local player = args[1] or mw.title.getCurrentTitle.baseText local use_redirect_table = not util_args.castAsBool(args.noredirects) local result = h.doQuery(player, args.show, use_redirect_table, args) local output = mw.html.create('div') if args.show == 'everything' then output = output:tag('div') output:attr('id','trf-content') end local tbl = output:tag('table') tbl:addClass("wikitable sortable hoverable-rows"):css("font-size","90%") util_html.makeColspanHeader(tbl, i18n.print('TournamentResults', player), #COLUMNS) util_html.headerFromI18n(tbl, COLUMNS) for _,row in ipairs(result) do		tr = tbl:tag("tr") tr:tag("td"):addClass('achievements-date'):wikitext(row.Date):done :node(placement.makeNode(row.Place)) tdEvent = tr:tag("td"):wikitext(s.LeagueSprite(row.League) .. " " .. (row.Event or ''))		if row.Phase then			tdEvent:wikitext(" - " .. row.Phase)		end 		tdEvent:wikitext("") h.addLastResult(tr, row, row.Team) tr:tag("td"):wikitext(m_team.rightshortlinked(row.Team)) tr:tag("td"):wikitext(h.getTeammates(row.RosterNames, row.RosterLinks, row.Roles, row.ThisName)) end frame:callParserFunction{name="#vardefine:hasnewresults",args={"yes"}}

return h.intro(args), output end

return p