Module:ListOfTeams

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_sort = require("Module:SortUtil") local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_vars = require("Module:VarsUtil") local i18n = require('Module:i18nUtil')

local m_country = require('Module:Country') local m_region = require('Module:Region') local m_team = require('Module:Team') local CompoundRole = require('Module:CompoundRole')

local Socials = require('Module:Infobox/Social').makeSocialSection

local COLUMNS = { 'TeamDisplay', 'Socials', 'Roster' }

local h = {}

local p = {} function p.main(frame) local args = util_args.merge i18n.init('ListOfTeams') h.castArgs(args) local data = h.makeAndRunQuery(args) h.formatRows(data) return h.makeOutput(data) end

function h.castArgs(args) args.region = args.region and m_region.medium(args.region) args.country = args.country and m_country.name(args.country) end

function h.makeAndRunQuery(args) return util_cargo.queryAndCast(h.makeQuery(args)) end

function h.makeQuery(args) local query = { tables = { 'Teams=T', 'ListplayerCurrent=LPC', 'Tenures=Ten', 'TeamsWithAutoRosters=TWAR', 'RosterChanges=RCJ', },		join = { 'T._pageName=LPC.Team', 'T._pageName=Ten.Team', 'T._pageName=TWAR._pageName', 'Ten.RosterChangeIdJoin=RCJ.RosterChangeId', },		fields = { 'T._pageName=Team', 'T.Twitter=twitter', -- 'T.Youtube=youtube', 'T.Facebook=facebook', 'T.Instagram=instagram', 'T.Vk=vk', 'TWAR._pageName=hasTWAR', },		where = { args.region and ('T.Region="%s"'):format(args.region), args.country and ('T.Location="%s"'):format(args.country), 'T.IsDisbanded="0"', '(Ten.IsCurrent="1" OR Ten._pageName IS NULL)', -- if they have RC-style then theyre ingame, once LPC is dead then just RolesIngame IS NOT NULL 'RCJ._pageName IS NULL OR RCJ.RolesIngame__FULL IS NOT NULL', 'RCJ.RoleModifier IS NULL OR RCJ.RoleModifier != "Trainee"', },		orderBy = 'T._pageName ASC', groupBy = ('%s, T._pageName'):format(h.getCase('LPC.Link', 'Ten.Player')), oneToMany = { fields = { Players = { ('CONCAT(%s)=Role'):format(h.getCase('LPC.Role', 'RCJ.Role')), 'RCJ.RoleModifier', 'LPC.IsSubstitute', 'LPC.IsTrainee', ('CONCAT(%s)=Player'):format(h.getCase('LPC.Link', 'Ten.Player')), },			},			groupBy = { 'Team' }, },	}	return query end

function h.getCase(lpcVersion, tenVersion) return ('CASE WHEN TWAR._pageName IS NULL THEN %s ELSE %s END'):format(lpcVersion, tenVersion) end

function h.formatRows(data) util_map.rowsInPlace(data, h.formatOneRow) end

function h.formatOneRow(row) util_map.rowsInPlace(row.Players, h.formatOnePlayer, row.hasTWAR) util_sort.tablesByKeys(row.Players, 'SortKeyRole', true) row.Roster = util_table.concat(row.Players, ' • ', h.linkPlayer) row.TeamDisplay = m_team.rightmediumlinked(row.Team) row.Socials = table.concat(Socials(row)) end

function h.formatOnePlayer(playerRow, hasTWAR) if not hasTWAR then playerRow.RoleModifier = h.getRoleModifier(playerRow) end playerRow.roleObj = CompoundRole(playerRow.Role, { modifier = playerRow.RoleModifier }) playerRow.SortKeyRole = playerRow.roleObj:sortnumber end

function h.getRoleModifier(playerRow) if playerRow.IsSubstitute then return 'Sub' end if playerRow.IsTrainee then return 'Trainee' end return nil end

function h.linkPlayer(playerRow) -- return (playerRow.RoleModifier or '') .. playerRow.Player return util_esports.playerWithRole({ player = playerRow.Player, role = playerRow.roleObj }) end

function h.makeOutput(data) local output = mw.html.create local tbl = output:tag('table') :addClass('wikitable') :addClass('sortable') util_html.printHeaderFromI18n(tbl, COLUMNS) util_html.printRowsByList(tbl, data, COLUMNS) return output end

return p