Exemple #1
0
 internal LeavingRoom(RoomSession session, NativeRealTimeRoom room,
                      Action leavingCompleteCallback)
 {
     mSession                 = Misc.CheckNotNull(session);
     mRoomToLeave             = Misc.CheckNotNull(room);
     mLeavingCompleteCallback = Misc.CheckNotNull(leavingCompleteCallback);
 }
 internal void OnRoomStatusChanged(NativeRealTimeRoom room)
 {
     lock (mLifecycleLock)
     {
         mState.OnRoomStatusChanged(room);
     }
 }
Exemple #3
0
 internal void OnParticipantStatusChanged(NativeRealTimeRoom room,
                                          MultiplayerParticipant participant)
 {
     lock (mLifecycleLock) {
         mState.OnParticipantStatusChanged(room, participant);
     }
 }
 internal void OnConnectedSetChanged(NativeRealTimeRoom room)
 {
     lock (mLifecycleLock)
     {
         mState.OnConnectedSetChanged(room);
     }
 }
 internal void OnParticipantStatusChanged(NativeRealTimeRoom room, GooglePlayGames.Native.PInvoke.MultiplayerParticipant participant)
 {
     lock (mLifecycleLock)
     {
         mState.OnParticipantStatusChanged(room, participant);
     }
 }
            internal override void HandleConnectedSetChanged(NativeRealTimeRoom room)
            {
                List <string> list  = new List <string>();
                List <string> list2 = new List <string>();
                Dictionary <string, GooglePlayGames.Native.PInvoke.MultiplayerParticipant> dictionary = room.Participants().ToDictionary((GooglePlayGames.Native.PInvoke.MultiplayerParticipant p) => p.Id());

                foreach (string key in mNativeParticipants.Keys)
                {
                    GooglePlayGames.Native.PInvoke.MultiplayerParticipant multiplayerParticipant  = dictionary[key];
                    GooglePlayGames.Native.PInvoke.MultiplayerParticipant multiplayerParticipant2 = mNativeParticipants[key];
                    if (!multiplayerParticipant.IsConnectedToRoom())
                    {
                        list2.Add(key);
                    }
                    if (!multiplayerParticipant2.IsConnectedToRoom() && multiplayerParticipant.IsConnectedToRoom())
                    {
                        list.Add(key);
                    }
                }
                foreach (GooglePlayGames.Native.PInvoke.MultiplayerParticipant value in mNativeParticipants.Values)
                {
                    value.Dispose();
                }
                mNativeParticipants = dictionary;
                mParticipants       = (from p in mNativeParticipants.Values
                                       select p.AsParticipant()).ToDictionary((Participant p) => p.ParticipantId);
                Logger.d("Updated participant statuses: " + string.Join(",", (from p in mParticipants.Values
                                                                              select p.ToString()).ToArray()));
                if (list2.Contains(GetSelf().ParticipantId))
                {
                    Logger.w("Player was disconnected from the multiplayer session.");
                }
                string selfId = GetSelf().ParticipantId;

                list = (from peerId in list
                        where !peerId.Equals(selfId)
                        select peerId).ToList();
                list2 = (from peerId in list2
                         where !peerId.Equals(selfId)
                         select peerId).ToList();
                if (list.Count > 0)
                {
                    list.Sort();
                    mSession.OnGameThreadListener().PeersConnected((from peer in list
                                                                    where !peer.Equals(selfId)
                                                                    select peer).ToArray());
                }
                if (list2.Count > 0)
                {
                    list2.Sort();
                    mSession.OnGameThreadListener().PeersDisconnected((from peer in list2
                                                                       where !peer.Equals(selfId)
                                                                       select peer).ToArray());
                }
            }
            internal override void HandleConnectedSetChanged(NativeRealTimeRoom room)
            {
                HashSet <string> newConnectedSet = new HashSet <string>();

                foreach (var participant in room.Participants())
                {
                    using (participant)
                    {
                        if (participant.IsConnectedToRoom())
                        {
                            newConnectedSet.Add(participant.Id());
                        }
                    }
                }

                // If the connected set hasn't actually changed, bail out.
                if (mConnectedParticipants.Equals(newConnectedSet))
                {
                    Logger.w("Received connected set callback with unchanged connected set!");
                    return;
                }

                var noLongerConnected = mConnectedParticipants.Except(newConnectedSet);

                // Check whether a participant that was in the connected set has left it.
                // If so, we will never reach a fully connected state, and should fail room
                // creation.
                if (noLongerConnected.Any())
                {
                    Logger.e("Participants disconnected during room setup, failing. " +
                             "Participants were: " + string.Join(",", noLongerConnected.ToArray()));
                    LeaveRoom();
                    return;
                }

                var newlyConnected = newConnectedSet.Except(mConnectedParticipants);

                Logger.d("New participants connected: " +
                         string.Join(",", newlyConnected.ToArray()));

                // If we're fully connected, transition to the Active state and signal the client.
                if (newConnectedSet.Count() == room.ParticipantCount())
                {
                    Logger.d("Fully connected! Transitioning to active state.");
                    mSession.EnterState(new ActiveState(room, mSession));
                    mSession.OnGameThreadListener().RoomConnected(true);
                    return;
                }

                // Otherwise, we're not fully there. Increment the progress by the appropriate
                // amount and inform the client.
                mPercentComplete      += mPercentPerParticipant * (float)newlyConnected.Count();
                mConnectedParticipants = newConnectedSet;
                mSession.OnGameThreadListener().RoomSetupProgress(mPercentComplete);
            }
 internal override void HandleParticipantStatusChanged(NativeRealTimeRoom room, GooglePlayGames.Native.PInvoke.MultiplayerParticipant participant)
 {
     if (FailedStatuses.Contains(participant.Status()))
     {
         mSession.OnGameThreadListener().ParticipantLeft(participant.AsParticipant());
         if (room.Status() != Types.RealTimeRoomStatus.CONNECTING && room.Status() != Types.RealTimeRoomStatus.AUTO_MATCHING)
         {
             LeaveRoom();
         }
     }
 }
 internal void UpdateCurrentRoom(NativeRealTimeRoom room)
 {
     if (mRoom != null)
     {
         mRoom.Dispose();
     }
     mRoom = Misc.CheckNotNull(room);
     mNativeParticipants = mRoom.Participants().ToDictionary((GooglePlayGames.Native.PInvoke.MultiplayerParticipant p) => p.Id());
     mParticipants       = (from p in mNativeParticipants.Values
                            select p.AsParticipant()).ToDictionary((Participant p) => p.ParticipantId);
 }
Exemple #10
0
            internal override void HandleParticipantStatusChanged(NativeRealTimeRoom room,
                                                                  MultiplayerParticipant participant)
            {
                if (!FailedStatuses.Contains(participant.Status()))
                {
                    return;
                }

                Logger.e(string.Format("Participant {0} changed to status {1}, room will never be" +
                                       "fully connected.", participant.Id(), participant.Status()));
                LeaveRoom();
            }
Exemple #11
0
 internal void UpdateCurrentRoom(NativeRealTimeRoom room)
 {
     if (mRoom != null)
     {
         mRoom.Dispose();
     }
     mRoom = Misc.CheckNotNull(room);
     mNativeParticipants = mRoom.Participants().ToDictionary(p => p.Id());
     mParticipants       = mNativeParticipants.Values
                           .Select(p => p.AsParticipant())
                           .ToDictionary(p => p.ParticipantId);
 }
            internal override void HandleConnectedSetChanged(NativeRealTimeRoom room)
            {
                HashSet <string> hashSet = new HashSet <string>();

                if ((room.Status() == Types.RealTimeRoomStatus.AUTO_MATCHING || room.Status() == Types.RealTimeRoomStatus.CONNECTING) && mSession.MinPlayersToStart <= room.ParticipantCount())
                {
                    mSession.MinPlayersToStart += room.ParticipantCount();
                    mPercentPerParticipant      = 80f / (float)(double)mSession.MinPlayersToStart;
                }
                foreach (GooglePlayGames.Native.PInvoke.MultiplayerParticipant item in room.Participants())
                {
                    using (item)
                    {
                        if (item.IsConnectedToRoom())
                        {
                            hashSet.Add(item.Id());
                        }
                    }
                }
                if (mConnectedParticipants.Equals(hashSet))
                {
                    Logger.w("Received connected set callback with unchanged connected set!");
                }
                else
                {
                    IEnumerable <string> source = mConnectedParticipants.Except(hashSet);
                    if (room.Status() == Types.RealTimeRoomStatus.DELETED)
                    {
                        Logger.e("Participants disconnected during room setup, failing. Participants were: " + string.Join(",", source.ToArray()));
                        mSession.OnGameThreadListener().RoomConnected(false);
                        mSession.EnterState(new ShutdownState(mSession));
                    }
                    else
                    {
                        IEnumerable <string> source2 = hashSet.Except(mConnectedParticipants);
                        Logger.d("New participants connected: " + string.Join(",", source2.ToArray()));
                        if (room.Status() == Types.RealTimeRoomStatus.ACTIVE)
                        {
                            Logger.d("Fully connected! Transitioning to active state.");
                            mSession.EnterState(new ActiveState(room, mSession));
                            mSession.OnGameThreadListener().RoomConnected(true);
                        }
                        else
                        {
                            mPercentComplete      += mPercentPerParticipant * (float)source2.Count();
                            mConnectedParticipants = hashSet;
                            mSession.OnGameThreadListener().RoomSetupProgress(mPercentComplete);
                        }
                    }
                }
            }
Exemple #13
0
 internal override void OnDataReceived(NativeRealTimeRoom room,
                                       MultiplayerParticipant sender, byte[] data, bool isReliable)
 {
     mSession.OnGameThreadListener().RealTimeMessageReceived(isReliable, sender.Id(), data);
 }
Exemple #14
0
 internal virtual void HandleParticipantStatusChanged(NativeRealTimeRoom room,
                                                      MultiplayerParticipant participant)
 {
     // noop
 }
Exemple #15
0
 internal sealed override void OnParticipantStatusChanged(NativeRealTimeRoom room,
                                                          MultiplayerParticipant participant)
 {
     HandleParticipantStatusChanged(room, participant);
     UpdateCurrentRoom(room);
 }
Exemple #16
0
 internal virtual void HandleConnectedSetChanged(NativeRealTimeRoom room)
 {
     // noop
 }
Exemple #17
0
 internal sealed override void OnConnectedSetChanged(NativeRealTimeRoom room)
 {
     HandleConnectedSetChanged(room);
     UpdateCurrentRoom(room);
 }
Exemple #18
0
 internal virtual void HandleRoomStatusChanged(NativeRealTimeRoom room)
 {
     // noop
 }
 internal ConnectingState(NativeRealTimeRoom room, RoomSession session)
     : base(session, room)
 {
     mPercentPerParticipant = 80f / (float)(double)session.MinPlayersToStart;
 }
Exemple #20
0
 internal MessagingEnabledState(RoomSession session, NativeRealTimeRoom room)
 {
     mSession = Misc.CheckNotNull(session);
     UpdateCurrentRoom(room);
 }
Exemple #21
0
 internal virtual void OnDataReceived(NativeRealTimeRoom room, MultiplayerParticipant sender,
                                      byte[] data, bool isReliable)
 {
     Logger.d(this.GetType().Name + ".OnDataReceived: Defaulting to no-op.");
 }
 internal virtual void OnParticipantStatusChanged(NativeRealTimeRoom room, GooglePlayGames.Native.PInvoke.MultiplayerParticipant participant)
 {
     Logger.d(GetType().Name + ".OnParticipantStatusChanged: Defaulting to no-op.");
 }
Exemple #23
0
 internal ActiveState(NativeRealTimeRoom room, RoomSession session) : base(session, room)
 {
 }
Exemple #24
0
 /**
  * Non-Lifecycle methods - these cannot cause state transitions, and thus we do not need to
  * hold any locks. We rely on only accessing volatile fields to ensure consistency instead.
  */
 internal void OnDataReceived(NativeRealTimeRoom room, MultiplayerParticipant sender,
                              byte[] data, bool isReliable)
 {
     mState.OnDataReceived(room, sender, data, isReliable);
 }
 internal virtual void HandleParticipantStatusChanged(NativeRealTimeRoom room, GooglePlayGames.Native.PInvoke.MultiplayerParticipant participant)
 {
 }
Exemple #26
0
 internal ConnectingState(NativeRealTimeRoom room, RoomSession session)
     : base(session, room)
 {
     mPercentPerParticipant =
         (100.0f - InitialPercentComplete) / (float)room.ParticipantCount();
 }
Exemple #27
0
 internal virtual void OnConnectedSetChanged(NativeRealTimeRoom room)
 {
     Logger.d(this.GetType().Name + ".OnConnectedSetChanged: Defaulting to no-op.");
 }
Exemple #28
0
 internal sealed override void OnRoomStatusChanged(NativeRealTimeRoom room)
 {
     HandleRoomStatusChanged(room);
     UpdateCurrentRoom(room);
 }
Exemple #29
0
            internal override void HandleConnectedSetChanged(NativeRealTimeRoom room)
            {
                List <string> newlyConnected = new List <string>();
                List <string> newlyLeft      = new List <string>();

                var updatedParticipants = room.Participants().ToDictionary(p => p.Id());

                foreach (var participantId in mNativeParticipants.Keys)
                {
                    var freshParticipant = updatedParticipants[participantId];
                    var staleParticipant = mNativeParticipants[participantId];

                    if (staleParticipant.IsConnectedToRoom() && !freshParticipant.IsConnectedToRoom())
                    {
                        newlyLeft.Add(participantId);
                    }

                    if (!staleParticipant.IsConnectedToRoom() && freshParticipant.IsConnectedToRoom())
                    {
                        newlyConnected.Add(participantId);
                    }
                }

                // Update the cached participants to reflect the new statuses by cleaning up the old
                // ones and then updating the new values.
                foreach (var participant in mNativeParticipants.Values)
                {
                    participant.Dispose();
                }

                mNativeParticipants = updatedParticipants;
                mParticipants       = mNativeParticipants.Values
                                      .Select(p => p.AsParticipant())
                                      .ToDictionary(p => p.ParticipantId);

                Logger.d("Updated participant statuses: " +
                         string.Join(",", mParticipants.Values.Select(p => p.ToString()).ToArray()));

                // Check whether the current player was disconnected from the room.
                if (newlyLeft.Contains(GetSelf().ParticipantId))
                {
                    Logger.w("Player was disconnected from the multiplayer session.");
                }

                // Strip out the participant ID of the local player - this player is not a "peer".
                var selfId = GetSelf().ParticipantId;

                newlyConnected = newlyConnected.Where(peerId => !peerId.Equals(selfId)).ToList();
                newlyLeft      = newlyLeft.Where(peerId => !peerId.Equals(selfId)).ToList();

                // Otherwise inform the client about changes in room participants, screening out
                // results about the local player.
                if (newlyConnected.Count > 0)
                {
                    newlyConnected.Sort();
                    mSession.OnGameThreadListener()
                    .PeersConnected(newlyConnected.Where(peer => !peer.Equals(selfId)).ToArray());
                }

                if (newlyLeft.Count > 0)
                {
                    newlyLeft.Sort();
                    mSession.OnGameThreadListener()
                    .PeersDisconnected(newlyLeft.Where(peer => !peer.Equals(selfId)).ToArray());
                }
            }
Exemple #30
0
 internal virtual void OnParticipantStatusChanged(NativeRealTimeRoom room,
                                                  MultiplayerParticipant participant)
 {
     Logger.d(this.GetType().Name + ".OnParticipantStatusChanged: Defaulting to no-op.");
 }