Module:PowerRankings

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_html = require('Module:HtmlUtil') local util_map = require('Module:MapUtil') local util_pr = require("Module:PrUtil") local util_sort = require('Module:SortUtil') 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 Country = require('Module:Country') local CountryList = require('Module:CountryList') local m_team = require('Module:Team')

local MAX_SHOWN = 5 local HIDDEN = false local DISABLE_HIDDEN_RESULTS = false

local COLUMNS = { 'index', 'Team', 'Player', 'Points', 'Gold', 'Silver', 'Bronze', 'Copper' }

local TOGGLES = { class = 'PR_toggle%s', }

local h = {} local p = {} function p.byPlayer(frame) local args = util_args.merge(true) i18n.init('PowerRankings') h.castArgs(args) h.setTogglePreferences(args) local data = h.getData(args) util_table.mapRowsInPlace(data, h.formatRow) return h.makeOutput(data, args) end

function h.castArgs(args) args.country = CountryList(args.country) end

function h.setTogglePreferences(args) if not args.country:exists then MAX_SHOWN = -1 end if args.max_shown then MAX_SHOWN = args.max_shown end DISABLE_HIDDEN_RESULTS = args.disable_hidden_results end

function h.getData(args) return util_cargo.queryAndCast(h.getQuery(args)) end

function h.getQuery(args) local ret = { tables = h.getTables(args), fields = h.getFields, join = h.getJoin(args), where = h.getWhere(args), limit = args.limit or 9999, orderBy = 'SUM(Res.PRPoints) DESC', offset = args.offset, groupBy = 'PR._pageName', }	util_cargo.logQuery(ret) return ret end

function h.getTables(args) if not args.country:exists then return { 'Tournaments=T', 'TournamentResults=Res', 'TournamentResults__RosterLinks=RL', 'PlayerRedirects=PR', 'Players=P', }	else return { 'Tournaments=T', 'TournamentResults=Res', 'TournamentResults__RosterLinks=RL', 'PlayerRedirects=PR', 'Players=P', 'Countries=C' }	end end

function h.getJoin(args) if not args.country:exists then return { 'T._pageName = Res.OverviewPage', 'Res._ID=RL._rowID', 'RL._value=PR.AllName', 'PR._pageName=P._pageName', }	else return { 'T._pageName = Res.OverviewPage', 'Res._ID=RL._rowID', 'RL._value=PR.AllName', 'PR._pageName=P._pageName', 'T.PrRegion=C.Region' }	end end

function h.getFields return { 'P.ID=Player', 'PR._pageName=Link', 'SUM(Res.PRPoints)=Points', 'P.Team=Team', --'PR.Squad=Squad', 'P.Nationality=Country[country]', 'SUM(Res.Gold)=Gold', 'SUM(Res.Silver)=Silver', 'SUM(Res.Bronze)=Bronze', 'SUM(Res.Copper)=Copper', 'P.TeamLast=TeamLast', } end

function h.getWhere(args) local tbl = { util_pr.QUERIES[args.tag or 'current'], args.region and ('P.Residency="%s"'):format(args.region), args.region and ('T.Region="%s"'):format(args.region), h.getFreeAgentWhereCondition(args), util_cargo.whereFromCompoundEntity('P.Nationality="%s"', args.country), args.country and util_cargo.whereFromCompoundEntity('C.Country="%s"', args.country), args.teamsize and ('T.TeamSize="%s"'):format(args.teamsize), args.platform and ('T.Platform="%s"'):format(args.platform), h.getRetiredWhereCondition(args.include_retired), '(P.IsInactive="0" OR P.IsInactive IS NULL)', }	util_vars.log(util_cargo.concatWhere(tbl)) return util_cargo.concatWhere(tbl) end

function h.getRetiredWhereCondition(include_retired) -- if util_args.castAsBool(include_retired) then return nil end -- return '(P.IsRetired="No" OR P.IsRetired IS NULL)' end

function h.getFreeAgentWhereCondition(args) -- if args.fa == 'only' then -- 	return ('P.Team IS NULL') -- elseif not util_args.castAsBool(args.fa) then -- 	return ('P.Team IS NOT NULL') -- else -- 	return nil -- end end

-- format row function h.formatRow(row) row.Team = m_team.onlyimagelinked(row.Team) row.Player = h.formatPlayer(row) end

function h.formatPlayer(row) if not row.Country:exists or row.Country.unknown then return h.makePlayerOnly(row) end local image = row.Country:image return image .. ' ' .. h.makePlayerOnly(row) end

function h.makePlayerOnly(row) return util_text.intLink(row.Link, row.Player) end

-- make output function h.makeOutput(data, args) h.initializeToggleData local tbl = mw.html.create('table') :addClass('wikitable sortable') :addClass('hoverable-rows') h.printHeader(tbl, data, args.country:flairs) h.printRows(tbl, data) return tbl end

function h.initializeToggleData local i = util_vars.setGlobalIndex('PR_i') TOGGLES.class = TOGGLES.class:format(i) end

function h.printHeader(tbl, data, countryList) h.printColspanHeader(tbl, data, countryList) util_html.printHeaderFromI18n(tbl, COLUMNS) util_html.printEmptySortRow(tbl, #COLUMNS) end

function h.printColspanHeader(tbl, data, countryList) if not country then return end util_html.printColspanHeader(tbl, countryList, #COLUMNS) end

function h.printRows(tbl, data) for i, row in ipairs(data) do		h.printRow(tbl, row, i == MAX_SHOWN and i < #data) end if HIDDEN then h.endHiddenResults(tbl) end end

function h.printRow(tbl, row, isHidden) if isHidden and DISABLE_HIDDEN_RESULTS then return end local tr = tbl:tag('tr') for _, col in ipairs(COLUMNS) do		tr:tag('td'):wikitext(row[col]) end if HIDDEN then util_toggle.sepCellClasses(tr, TOGGLES) end if isHidden then HIDDEN = true h.printShowButton(tbl) end end

function h.printShowButton(tbl) local tr = tbl:tag('tr') local th = tr:tag('th') :attr('colspan', #COLUMNS) util_toggle.printSepToggler(th, TOGGLES) util_toggle.sepCellClasses(tr, TOGGLES, true) end

function h.endHiddenResults(tbl) local tr = tbl:tag('tr') local th = tr:tag('th') :attr('colspan', #COLUMNS) util_toggle.sepCellClasses(tr, TOGGLES, false) util_toggle.printSepToggler(th, TOGGLES, true) end

return p