Module:InfoboxPlayerUtil

local lang = mw.getLanguage('en') local util_html = require('Module:HtmlUtil') local util_cargo = require('Module:CargoUtil') local util_dpl = require('Module:DPLUtil') local util_table = require('Module:TableUtil') local util_args = require('Module:ArgsUtil')

local h = {}

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 placeResult = h.doPlaceQuery(player, args.show, use_redirect_table, args) local prizeResult = h.doPrizeQuery(player, args.show, use_redirect_table, args)

local p = {}

function h.doPlaceQuery(player, show, use_redirect_table, args) local tbl = { tables = h.getTables(use_redirect_table), join = h.getJoin(use_redirect_table), where = h.getPlaceWhere(player, use_redirect_table, args), fields = h.getPlaceFields(player, use_redirect_table), groupBy = 'TournamentResults.UniqueLine', orderBy = 'TournamentResults.Date DESC', limit = 5000, }	return util_cargo.queryAndCast(tbl) end

function h.doPrizeQuery(player, show, use_redirect_table, args) local tbl = { tables = h.getTables(use_redirect_table), join = h.getJoin(use_redirect_table), where = h.getPrizeWhere(player, use_redirect_table, args), fields = h.getPrizeFields(player, use_redirect_table), groupBy = 'TournamentResults.UniqueLine', orderBy = 'TournamentResults.Date DESC', limit = 5000, }	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._pageName=TournamentResults.RosterPage', 'TournamentResults._pageName=Tournaments._pageName' }	if redirects then tbl[#tbl+1] = 'TournamentRosters.RosterLinks HOLDS PlayerRedirects.AllName' end return util_table.concat(tbl, ', ') end

function h.getPlayerWhereConditions(player, use_redirect_table) if use_redirect_table then return ('PlayerRedirects._pageName="%s"'):format(player) end return ('TournamentRosters.RosterLinks HOLDS "%s"'):format(player) end

function h.getPlaceWhere(player, use_redirect_table, args) local tbl = { 'TournamentRosters.TeamLink = TournamentResults.TeamLink', 'Tournaments.Tier="Premier"' or 'Tournaments.Tier="Major"', 'Tournaments.EventType="Offline"', ('Place_Number <= "1"'), h.getPlayerWhereConditions(player, use_redirect_table), }	return util_table.concat(tbl,' AND ') end

function h.getPrizeWhere(player, use_redirect_table, args) local tbl = { 'TournamentRosters.TeamLink = TournamentResults.TeamLink', h.getPlayerWhereConditions(player, use_redirect_table), }	return util_table.concat(tbl,' AND ') end

function h.getPlaceFields(player, redirects) local tbl = { "TournamentResults.Place=Place", }	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.getPrizeFields(player, redirects) local tbl = { "TournamentResults.Prize=Prize", "TournamentResults.PrizeUnit=PrizeUnit", "Tournaments.Mode=Mode", }	if redirects then tbl[#tbl+1] = "CONCAT(PlayerRedirects.AllName)=ThisName" else tbl[#tbl+1] = ('CONCAT("%s")=ThisName'):format(player) end return tbl end

function p.getMajors local majors = 0 for _, row in ipairs(placeResult) do		majors = majors + 1 return majors end end