//changes the local team and informs all other team managers of the change
    public void ChangeTeam(Team team)
    {
        //store the new local team
        localTeam = team;

        client.gamePlayers[client._ClientID].team = team;

        //create the changeteamrequest object
        ChangeTeamRequest teamRequest = new ChangeTeamRequest(client._ClientID, team);

        SendRequest(teamRequest, (int)TeamSystemRequest.ChangeTeamRequest);

        //update the playerlist if it exists
        if (playerListUI)
        {
            playerListUI.AddPlayerToTeamList(client.gamePlayers[client._ClientID].AsPlayerData(), team);
        }
    }
    public override void HandleMessage(GameSystemData data)
    {
        //get the request type from the first byte in the array
        TeamSystemRequest request = (TeamSystemRequest)data.D[0];

        //then remove that byte from the message to leave it readable
        data.D = data.D.AsMemory().Slice(1).ToArray();

        switch (request)
        {
        //we're receiving the state of the teams
        case TeamSystemRequest.TeamAllocationData:
            TeamAllocationData teamData = MessagePackSerializer.Deserialize <TeamAllocationData>(data.D);
            //store the new team setup
            ClientTeams = teamData.ClientTeams();

            //iterate through all of the clients we have data about
            foreach (int key in ClientTeams.Keys)
            {
                //change the team
                client.gamePlayers[key].team = ClientTeams[key];
                if (playerListUI)
                {
                    playerListUI.AddPlayerToTeamList(client.gamePlayers[key].AsPlayerData(), ClientTeams[key]);
                }
            }
            break;

        //someone wants to change teams
        case TeamSystemRequest.ChangeTeamRequest:
            ChangeTeamRequest teamRequest = MessagePackSerializer.Deserialize <ChangeTeamRequest>(data.D);

            //see if they have a team entry, if not, give them one, if yes, change the entry
            if (ClientTeams.TryGetValue(teamRequest.C, out _))
            {
                ClientTeams[teamRequest.C] = teamRequest.R;
            }
            else
            {
                ClientTeams.Add(teamRequest.C, teamRequest.R);
            }

            //change the player's team too
            client.gamePlayers[teamRequest.C].team = teamRequest.R;
            //and tell the player list ui to do the same
            if (playerListUI)
            {
                playerListUI.AddPlayerToTeamList(client.gamePlayers[teamRequest.C].AsPlayerData(), teamRequest.R);
            }

            //sync the system if we can
            SyncSystem();

            break;

        //we've been asked to select a team, so we need to ask the player
        case TeamSystemRequest.SelectTeamRequest:
            //spawn the correct select team ui depending on the input method
            LevelSelectUI levelUI;
            if (client.inputMethod == InputMethod.VR)
            {
                Instantiate(SelectTeamPhysicalUI);
            }
            else if (levelUI = FindObjectOfType <LevelSelectUI>())
            {
                levelUI.OpenNonVRSelectTeamUI(this);
            }
            break;
        }
    }