Module:QueryTeamMembers

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:QueryTeamMembers/doc

local util_args = require('Module:ArgsUtil')
local util_cargo = require("Module:CargoUtil")
local util_esports = require("Module:EsportsUtil")
local util_map = require("Module:MapUtil")
local util_sort = require("Module:SortUtil")
local util_text = require("Module:TextUtil")

local h = {}

local p = {}
function p.main(team, settings)
	if h.hasAutoTeams(team) then
		return h.getTeamMembers(team, settings)
	end
	return h.getListplayerCurrent(team, settings)
end

function h.hasAutoTeams(teamlink)
	local query = {
		tables = 'TeamsWithAutoRosters',
		fields = 'OverviewPage',
		where = string.format('OverviewPage="%s"',teamlink),
	}
	return util_cargo.getOneResult(query)
end

function h.getTeamMembers(team, settings)
	if not settings then settings = {} end
	local query = {
		tables = {
			'Tenures=Ten',
			'PlayerRedirects=PR',
			'Players=P',
			'RosterChanges=RCJ',
		},
		join = {
			'Ten.Player=PR.AllName',
			'PR.OverviewPage=P._pageName',
			'Ten.RosterChangeIdJoin=RCJ.RosterChangeId',
		},
		fields = {
			'P.Player=Link',
			'P.ID',
			'P.Name',
			'P.NameFull',
			'COALESCE(P.NationalityPrimary, P.Country)=Country [country]',
			'P.Residency [region]',
			'RCJ.Roles',
			'RCJ.RoleModifier',
		},
		complexTypes = {
			RoleList = {
				type = 'RoleList',
				args = {
					'Roles',
					modifier = 'RoleModifier',
				}
			}
		},
		where = {
			'Ten.IsCurrent="1"',
			('Ten.Team="%s"'):format(team),
			settings.hideTrainees and 'RCJ.RoleModifier IS NULL OR RCJ.RoleModifier != "Trainee"',
		},
	}
	local result = util_cargo.queryAndCast(query)
	
	-- should replace this with having a pre-store is_ingame check
	for i = #result, 1, -1 do
		if not result[i].RoleList:hasIngame() then
			table.remove(result, i)
		end
	end
	
	util_map.rowsInPlace(result, h.formatOneTeamMemberRow)
	util_sort.tablesByKeys(result, { 'SortKeyRole', 'SortKeyName' }, { true, true })
	
	return result
end

function h.formatOneTeamMemberRow(row)
	row.SortKeyRole = row.RoleList:sortnumber()
	if row.Link then
		row.SortKeyName = mw.ustring.lower(row.Link)
	end
end

function h.getListplayerCurrent(team, settings)
	if not settings then settings = {} end
	local query = {
		tables = { 'ListplayerCurrent=LPC', '_pageData=_pD' },
		join = 'LPC.Link=_pD._pageName',
		fields = {
			'_pD._pageName=Exists [boolean]',
			'LPC.Link',
			'LPC.ID',
			'LPC.Name',
			'LPC.Country [country]',
			'LPC.Residency [region]',
			'LPC.Role',
			'LPC.IsSubstitute [boolean]',
			'LPC.IsTrainee [boolean]',
		},
		where = h.getLpcWhere(team, settings),
		groupBy = 'LPC.Link',
		orderBy = 'LPC.N',
		complexTypes = {
			RoleList = {
				type = 'RoleList',
				args = {
					'Role',
					
					-- these were previously cast as boolean
					sub = 'IsSubstitute',
					trainee = 'IsTrainee',
				}
			}
		}
	}
	local result = util_cargo.queryAndCast(query)
	util_map.rowsInPlace(result, h.formatOneRowLPC)
	util_sort.tablesByKeys(result, { 'SortKeyRole', 'SortKeyName' }, { true, true })
	return result
end

function h.getLpcWhere(team, settings)
	local where = {
		('LPC._pageName="%s" AND ID IS NOT NULL'):format(team),
	}
	return where
end

function h.formatOneRowLPC(row)
	row.ID = util_esports.playerDisplay(row.ID)
	row.NameFull = row.Name
	h.formatOneTeamMemberRow(row)
end

return p