Halo Esports Wiki

We are currently performing extensive maintenance to this wiki and as a result there will likely be errors. Please be patient while we work to fix all problems.

READ MORE

Halo Esports Wiki
Advertisement

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_map = require("Module:MapUtil")
local util_sort = require("Module:SortUtil")

local m_role = require('Module:Role')

local h = {}

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

function h.hasAutoTeams(teamlink)
	local query = {
		tables = 'Teams',
		fields = 'HasAutoTeamMembers',
		where = string.format('_pageName="%s"',teamlink),
		types = { HasAutoTeamMembers = 'boolean' },
	}
	return util_cargo.getOneResult(query)
end

function h.getTeamMembers(team)
	local query = {
		tables = { 'Tenures=Ten', 'Players=P', 'RosterChanges=RCJ' },
		join = { 'Ten.Player=P._pageName', 'Ten.RosterChangeIdJoin=RCJ.RosterChangeId' },
		fields = {
			'P._pageName=Link',
			'P.ID',
			'P.Name',
			'COALESCE(P.NationalityPrimary, P.Country)=Country',
			'P.Residency',
			'RCJ.Role',
			'RCJ.RoleModifier',
		},
		where = {
			'Ten.IsCurrent="1"',
			('Ten.Team="%s"'):format(team),
		},
	}
	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 m_role.isplayer(result[i].Role) then
			table.remove(result, i)
		end
	end
	
	util_map.rowsInPlace(result, h.formatOneTeamMemberRow)
	util_sort.tablesByKeys(result, 'SortKeyRole', true)
	
	return result
end

function h.formatOneTeamMemberRow(row)
	row.SortKeyRole = m_role.sortnumber(row.Role, { sub = row.RoleModifier == 'Sub', trainee = row.RoleModifier == 'Trainee' })
end

function h.getListplayerCurrent(team)
	local query = {
		tables = 'ListplayerCurrent',
		fields = {
			'Link',
			'ID',
			'Name',
			'Country',
			'Residency',
			'Role',
			'IsSubstitute'
		},
		where = ('_pageName="%s" AND ID IS NOT NULL'):format(team),
		groupBy = 'Link',
		orderBy = 'N',
		types = { IsSubstitute = 'boolean' },
	}
	local result = util_cargo.queryAndCast(query)
	return result
end

return p
Advertisement