Esempio n. 1
0
        /// <summary>Switch that player's team to the one you assign.</summary>
        /// <remarks>Internally checks if this player is in that team already or not. Only team switches are actually sent.</remarks>
        /// <param name="player"></param>
        /// <param name="team"></param>
        public static bool SwitchTeam(this Player player, PhotonTeam team)
        {
            if (team == null)
            {
                Debug.LogWarning("SwitchTeam failed: PhotonTeam provided is null");
                return(false);
            }
            PhotonTeam currentTeam = player.GetPhotonTeam();

            if (currentTeam == null)
            {
                Debug.LogWarningFormat("SwitchTeam failed: player ({0}) was not joined to any team, call JoinTeam instead", player);
                return(false);
            }
            if (currentTeam.Code == team.Code)
            {
                Debug.LogWarningFormat("SwitchTeam failed: player ({0}) is already joined to the same team {1}", player, team);
                return(false);
            }
            return(player.SetCustomProperties(new Hashtable {
                { PhotonTeamsManager.TeamPlayerProp, team.Code }
            },
                                              new Hashtable {
                { PhotonTeamsManager.TeamPlayerProp, currentTeam.Code }
            }));
        }
Esempio n. 2
0
        void IInRoomCallbacks.OnPlayerEnteredRoom(Player newPlayer)
        {
            PhotonTeam team = newPlayer.GetPhotonTeam();

            if (team == null)
            {
                return;
            }
            if (playersPerTeam[team.Code].Contains(newPlayer))
            {
                // player rejoined w/ same team
                return;
            }
            // check if player rejoined w/ different team, remove from previous team
            foreach (var key in teamsByCode.Keys)
            {
                if (playersPerTeam[key].Remove(newPlayer))
                {
                    break;
                }
            }
            if (!playersPerTeam[team.Code].Add(newPlayer))
            {
                Debug.LogWarningFormat("Unexpected situation while adding player {0} who joined to team {1}, updating teams for all", newPlayer, team);
                // revert to 'brute force' in case of unexpected situation
                this.UpdateTeams();
            }
        }
        /// <summary>
        /// Leave the current team if any.
        /// </summary>
        /// <param name="player"></param>
        /// <returns>If the leaving team request is queued to be sent to the server or done in case offline or not joined to a room yet.</returns>
        public static bool LeaveCurrentTeam(this Player player)
        {
            PhotonTeam currentTeam = player.GetPhotonTeam();

            if (currentTeam == null)
            {
                Debug.LogWarningFormat("LeaveCurrentTeam failed: player ({0}) was not joined to any team", player);
                return(false);
            }
            if (PhotonNetwork.InRoom && !PhotonNetwork.OfflineMode)
            {
                return(PhotonNetwork.NetworkingClient.OpSetCustomPropertiesOfActor(player.ActorNumber,
                                                                                   new Hashtable {
                    { PhotonTeamsManager.TeamPlayerProp, null }
                }));
            }
            if (PhotonNetwork.IsConnectedAndReady)
            {
                player.SetCustomProperties(new Hashtable {
                    { PhotonTeamsManager.TeamPlayerProp, null }
                });
                return(true);
            }
            Debug.LogWarningFormat("LeaveCurrentTeam failed: method was called while the client is not ready, networking client state: {0}", PhotonNetwork.NetworkClientState);
            return(false);
        }
        /// <summary>
        /// Join a team.
        /// </summary>
        /// <param name="player">The player who will join a team.</param>
        /// <param name="team">The team to be joined.</param>
        /// <returns></returns>
        public static bool JoinTeam(this Player player, PhotonTeam team)
        {
            if (team == null)
            {
                Debug.LogWarning("JoinTeam failed: PhotonTeam provided is null");
                return(false);
            }
            PhotonTeam currentTeam = player.GetPhotonTeam();

            if (currentTeam != null)
            {
                Debug.LogWarningFormat("JoinTeam failed: player ({0}) is already joined to a team ({1}), call SwitchTeam instead", player, team);
                return(false);
            }
            if (PhotonNetwork.InRoom && !PhotonNetwork.OfflineMode)
            {
                return(PhotonNetwork.NetworkingClient.OpSetCustomPropertiesOfActor(player.ActorNumber,
                                                                                   new Hashtable {
                    { PhotonTeamsManager.TeamPlayerProp, team.Code }
                }));
            }
            if (PhotonNetwork.IsConnectedAndReady)
            {
                player.SetCustomProperties(new Hashtable {
                    { PhotonTeamsManager.TeamPlayerProp, team.Code }
                });
                return(true);
            }
            Debug.LogWarningFormat("JoinTeam failed: method was called while the client is not ready, networking client state: {0}", PhotonNetwork.NetworkClientState);
            return(false);
        }
Esempio n. 5
0
        void IInRoomCallbacks.OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
        {
            object temp;

            if (changedProps.TryGetValue(TeamPlayerProp, out temp))
            {
                if (temp == null)
                {
                    foreach (byte code in playersPerTeam.Keys)
                    {
                        if (playersPerTeam[code].Remove(targetPlayer))
                        {
                            if (PlayerLeftTeam != null)
                            {
                                PlayerLeftTeam(targetPlayer, teamsByCode[code]);
                            }
                            break;
                        }
                    }
                }
                else if (temp is byte)
                {
                    byte teamCode = (byte)temp;
                    // check if player switched teams, remove from previous team
                    foreach (byte code in playersPerTeam.Keys)
                    {
                        if (code == teamCode)
                        {
                            continue;
                        }
                        if (playersPerTeam[code].Remove(targetPlayer))
                        {
                            if (PlayerLeftTeam != null)
                            {
                                PlayerLeftTeam(targetPlayer, teamsByCode[code]);
                            }
                            break;
                        }
                    }
                    PhotonTeam team = teamsByCode[teamCode];
                    if (!playersPerTeam[teamCode].Add(targetPlayer))
                    {
                        Debug.LogWarningFormat("Unexpected situation while setting team {0} for player {1}, updating teams for all", team, targetPlayer);
                        this.UpdateTeams();
                    }
                    if (PlayerJoinedTeam != null)
                    {
                        PlayerJoinedTeam(targetPlayer, team);
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Unexpected: custom property key {0} should have of type byte, instead we got {1} of type {2}. Player: {3}",
                                         TeamPlayerProp, temp, temp.GetType(), targetPlayer);
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Gets all players joined to a team.
 /// </summary>
 /// <param name="team">The team which will be used to find players.</param>
 /// <param name="members">The array of players to be filled.</param>
 /// <returns>If successful or not.</returns>
 public bool TryGetTeamMembers(PhotonTeam team, out Player[] members)
 {
     members = null;
     if (team != null)
     {
         return(this.TryGetTeamMembers(team.Code, out members));
     }
     return(false);
 }
 /// <summary>
 /// Find a PhotonTeam using a team code.
 /// </summary>
 /// <param name="code">The team code.</param>
 /// <param name="team">The team to be assigned if found.</param>
 /// <returns>If successful or not.</returns>
 public bool TryGetTeamByCode(byte code, out PhotonTeam team)
 {
     team = null;
     if (teamsByCode.ContainsKey(code))
     {
         team = teamsByCode[code];
         return(true);
     }
     return(false);
 }
Esempio n. 8
0
        /// <summary>
        /// Gets the number of players in a team.
        /// </summary>
        /// <param name="team">The team you want to know the size of</param>
        /// <returns>Number of players joined to the team.</returns>
        public int GetTeamMembersCount(PhotonTeam team)
        {
            HashSet <Player> players;

            if (team != null && this.playersPerTeam.TryGetValue(team.Code, out players) && players != null)
            {
                return(players.Count);
            }
            return(0);
        }
 /// <summary>
 /// Find a PhotonTeam using a team name.
 /// </summary>
 /// <param name="teamName">The team name.</param>
 /// <param name="team">The team to be assigned if found.</param>
 /// <returns>If successful or not.</returns>
 public bool TryGetTeamByName(string teamName, out PhotonTeam team)
 {
     team = null;
     if (teamsByName.ContainsKey(teamName))
     {
         team = teamsByName[teamName];
         return(true);
     }
     return(false);
 }
Esempio n. 10
0
 private void UpdateTeams()
 {
     this.ClearTeams();
     for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
     {
         Player     player     = PhotonNetwork.PlayerList[i];
         PhotonTeam playerTeam = player.GetPhotonTeam();
         if (playerTeam != null)
         {
             playersPerTeam[playerTeam.Code].Add(player);
         }
     }
 }
Esempio n. 11
0
        void IInRoomCallbacks.OnPlayerLeftRoom(Player otherPlayer)
        {
            if (otherPlayer.IsInactive)
            {
                return;
            }
            PhotonTeam team = otherPlayer.GetPhotonTeam();

            if (team != null && !playersPerTeam[team.Code].Remove(otherPlayer))
            {
                Debug.LogWarningFormat("Unexpected situation while removing player {0} who left from team {1}, updating teams for all", otherPlayer, team);
                // revert to 'brute force' in case of unexpected situation
                this.UpdateTeams();
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Leave the current team if any.
        /// </summary>
        /// <param name="player"></param>
        /// <returns>If the leaving team request is queued to be sent to the server or done in case offline or not joined to a room yet.</returns>
        public static bool LeaveCurrentTeam(this Player player)
        {
            PhotonTeam currentTeam = player.GetPhotonTeam();

            if (currentTeam == null)
            {
                Debug.LogWarningFormat("LeaveCurrentTeam failed: player ({0}) was not joined to any team", player);
                return(false);
            }
            return(player.SetCustomProperties(new Hashtable {
                { PhotonTeamsManager.TeamPlayerProp, null }
            }, new Hashtable {
                { PhotonTeamsManager.TeamPlayerProp, currentTeam.Code }
            }));
        }
Esempio n. 13
0
        /// <summary>
        /// Gets all team mates of a player.
        /// </summary>
        /// <param name="player">The player whose team mates will be searched.</param>
        /// <param name="teamMates">The array of players to be filled.</param>
        /// <returns>If successful or not.</returns>
        public bool TryGetTeamMatesOfPlayer(Player player, out Player[] teamMates)
        {
            teamMates = null;
            if (player == null)
            {
                return(false);
            }

            PhotonTeam team = player.GetPhotonTeam();

            if (team == null)
            {
                return(false);
            }

            HashSet <Player> players;

            if (this.playersPerTeam.TryGetValue(team.Code, out players))
            {
                if (!players.Contains(player))
                {
                    Debug.LogWarningFormat(
                        "Unexpected situation while getting team mates of player {0} who is joined to team {1}, updating teams for all",
                        player, team);
                    // revert to 'brute force' in case of unexpected situation
                    this.UpdateTeams();
                }

                teamMates = new Player[players.Count - 1];
                int i = 0;
                foreach (var p in players)
                {
                    if (p.Equals(player))
                    {
                        continue;
                    }

                    teamMates[i] = p;
                    i++;
                }

                return(true);
            }

            return(false);
        }
        }                                    // 0x000000018127BB20-0x000000018127BC80

        public bool TryGetTeamByCode(byte code, out PhotonTeam team)
        {
            team = default;
            return(default);
Esempio n. 15
0
 public static PhotonTeam GetPhotonTeam(this Photon.Realtime.Player player) => default;         // 0x000000018127AEE0-0x000000018127AFF0
 public static bool JoinTeam(this Photon.Realtime.Player player, PhotonTeam team) => default;   // 0x000000018127B130-0x000000018127B350
Esempio n. 16
0
 public static bool JoinTeam(this Photon.Realtime.Player player, string teamName) => default;   // 0x000000018127AFF0-0x000000018127B090
 public static bool SwitchTeam(this Photon.Realtime.Player player, PhotonTeam team) => default; // 0x000000018127B660-0x000000018127B990
Esempio n. 17
0
 /// <summary>
 /// Find a PhotonTeam using a team name.
 /// </summary>
 /// <param name="teamName">The team name.</param>
 /// <param name="team">The team to be assigned if found.</param>
 /// <returns>If successful or not.</returns>
 public bool TryGetTeamByName(string teamName, out PhotonTeam team)
 {
     return(teamsByName.TryGetValue(teamName, out team));
 }
Esempio n. 18
0
 /// <summary>
 /// Find a PhotonTeam using a team code.
 /// </summary>
 /// <param name="code">The team code.</param>
 /// <param name="team">The team to be assigned if found.</param>
 /// <returns>If successful or not.</returns>
 public bool TryGetTeamByCode(byte code, out PhotonTeam team)
 {
     return(teamsByCode.TryGetValue(code, out team));
 }