Module:TeamPowerRankings

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_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 m_country = require('Module:Country') local m_team = require('Module:Team')

local lang = mw.getLanguage('en')

local COLUMNS = { 'index', 'TeamDisplay', 'Squad', 'Points', 'Roster' }

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) i18n.init('TeamPowerRankings') local listOfTeams = h.makeAndRunTeamsQuery(args) local rosterLookup = h.getRosterLookup(args) h.formatTeamsForOutput(listOfTeams, rosterLookup) util_vars.log(rosterLookup) return h.makeOutput(listOfTeams) end

function h.makeAndRunTeamsQuery(args) return util_cargo.queryAndCast(h.getTeamsQuery(args)) end

function h.getTeamsQuery(args) local ret = { tables = h.getTeamsTables, fields = h.getTeamsFields, join = h.getTeamsJoin, where = h.getTeamsWhere(args), limit = args.limit or 100, orderBy = 'SUM(Res.PRPoints) DESC, Teams.Name ASC', offset = args.offset, groupBy = 'LPC.Team, LPC.Squad', }	return ret end

function h.getTeamsTables return { 'Tournaments=T', 'TournamentResults=Res', 'TournamentResults__RosterLinks=RL', 'PlayerRedirects=PR', 'PlayerRedirects=PR2', 'ListplayerCurrent=LPC', 'Teams', } end

function h.getTeamsJoin return { 'T._pageName = Res.OverviewPage', 'Res._ID=RL._rowID', 'RL._value=PR.AllName', 'PR._pageName=PR2._pageName', 'PR2.AllName=LPC.Link', 'LPC._pageName=Teams._pageName', } end

function h.getTeamsFields return { 'SUM(Res.PRPoints)=Points', 'LPC.Squad', 'LPC._pageName=Team', 'SUM(Res.Gold)=Gold', 'SUM(Res.Silver)=Silver', 'SUM(Res.Bronze)=Bronze', 'SUM(Res.Copper)=Copper', 'CONCAT(Teams._pageName, LPC.Squad)=TeamAndSquad', } end

function h.getTeamsWhere(args) local tbl = { util_pr.QUERIES.current, 'Teams._pageName IS NOT NULL', 'Teams.HasPowerRankings = "1"', 'LPC.Squad IS NOT NULL', args.region and ('Teams.Region="%s"'):format(args.region), args.region and ('T.PrRegion="%s"'):format(args.region), args.country and ('P.Nationality="%s"'):format(m_country.name(args.country)), args.teamsize and ('T.TeamSize="%s"'):format(args.teamsize), args.platform and ('T.Platform="%s"'):format(args.platform), }	return util_cargo.concatWhere(tbl) end

function h.getRosterLookup(args) return util_cargo.getRowDict(h.getRosterLookupQuery(args), 'TeamAndSquad') end

function h.getRosterLookupQuery(args) local query = { tables = { 'Teams=T', 'ListplayerCurrent=LPC', },		join = { 'T._pageName=LPC.Team', },		fields = { 'CONCAT(T._pageName, LPC.Squad)=TeamAndSquad', },		oneToMany = { fields = { Players = { 'LPC.ID' }			},			groupBy = { 'TeamAndSquad' }, },		orderBy = 'Team DESC, Squad ASC', where = h.getRosterLookupWhere(args), }	return query end

function h.getRosterLookupWhere(args) local tbl = { args.region and ('T.Region="%s"'):format(args.region), }	return util_cargo.concatWhere(tbl) end

-- process function h.formatTeamsForOutput(listOfTeams, rosterLookup) util_map.rowsInPlace(listOfTeams, h.formatTeamsRowForOutput, rosterLookup) end

function h.formatTeamsRowForOutput(row, rosterLookup) row.TeamDisplay = m_team.rightmediumlinked(row.Team) if row.TeamAndSquad and rosterLookup[row.TeamAndSquad] then row.Roster = util_table.concat(rosterLookup[row.TeamAndSquad].Players, ' • ', h.getPlayerId) else util_vars.log(row.TeamAndSquad) util_vars.log('***********') end end

function h.getPlayerId(player) return util_esports.playerLinked(player.ID) end

-- output function h.makeOutput(listOfTeams) local output = mw.html.create local tbl = output:tag('table') :addClass('wikitable sortable') h.printHeader(tbl) h.printData(tbl, listOfTeams) return output end

function h.printHeader(tbl) util_html.printHeaderFromI18n(tbl, COLUMNS) end

function h.printData(tbl, listOfTeams) util_html.printRowsByList(tbl, listOfTeams, COLUMNS) end

return p