public void AcceptFromInbox(RealTimeMultiplayerListener listener)
 {
     lock (mSessionLock)
     {
         RoomSession newRoom = new RoomSession(mRealtimeManager, listener);
         if (mCurrentSession.IsActive())
         {
             Logger.e("Received attempt to accept invitation without cleaning up active session.");
             newRoom.LeaveRoom();
         }
         else
         {
             mCurrentSession           = newRoom;
             mCurrentSession.ShowingUI = true;
             mRealtimeManager.ShowRoomInboxUI(delegate(RealtimeManager.RoomInboxUIResponse response)
             {
                 mCurrentSession.ShowingUI = false;
                 if (response.ResponseStatus() != CommonErrorStatus.UIStatus.VALID)
                 {
                     Logger.d("User did not complete invitation screen.");
                     newRoom.LeaveRoom();
                 }
                 else
                 {
                     GooglePlayGames.Native.PInvoke.MultiplayerInvitation invitation   = response.Invitation();
                     GooglePlayGames.Native.PInvoke.RealTimeEventListenerHelper helper = HelperForSession(newRoom);
                     try
                     {
                         Logger.d("About to accept invitation " + invitation.Id());
                         newRoom.StartRoomCreation(mNativeClient.GetUserId(), delegate
                         {
                             mRealtimeManager.AcceptInvitation(invitation, helper, delegate(RealtimeManager.RealTimeRoomResponse acceptResponse)
                             {
                                 using (invitation)
                                 {
                                     newRoom.HandleRoomResponse(acceptResponse);
                                     newRoom.SetInvitation(invitation.AsInvitation());
                                 }
                             });
                         });
                     }
                     finally
                     {
                         if (helper != null)
                         {
                             ((IDisposable)helper).Dispose();
                         }
                     }
                 }
             });
         }
     }
 }
        public void AcceptFromInbox(RealTimeMultiplayerListener listener)
        {
            lock (mSessionLock)
            {
                var newRoom = new RoomSession(mRealtimeManager, listener);
                if (mCurrentSession.IsActive())
                {
                    Logger.e("Received attempt to accept invitation without cleaning up " +
                             "active session.");
                    newRoom.LeaveRoom();
                    return;
                }

                // The user accepted an invitation from the inbox, this is now the current room.
                mCurrentSession = newRoom;

                mRealtimeManager.ShowRoomInboxUI(
                    response =>
                {
                    if (response.ResponseStatus() != Status.UIStatus.VALID)
                    {
                        Logger.d("User did not complete invitation screen.");
                        newRoom.LeaveRoom();
                        return;
                    }

                    // We are not cleaning up the invitation here to workaround a bug in the
                    // C++ SDK where it holds a reference to un-owned memory rather than making a
                    // copy. This is cleaned up after the callback comes back instead.
                    var invitation = response.Invitation();

                    using (var helper = HelperForSession(newRoom))
                    {
                        Logger.d("About to accept invitation " + invitation.Id());
                        newRoom.StartRoomCreation(mNativeClient.GetUserId(),
                                                  () => mRealtimeManager.AcceptInvitation(invitation, helper,
                                                                                          acceptResponse =>
                        {
                            // Clean up the invitation here (see above comment).
                            using (invitation)
                            {
                                newRoom.HandleRoomResponse(acceptResponse);
                            }
                        }));
                    }
                });
            }
        }
示例#3
0
        public void CreateQuickGame(uint minOpponents, uint maxOpponents, uint variant,
                                    RealTimeMultiplayerListener listener)
        {
            lock (mSessionLock) {
                var newSession = new RoomSession(mRealtimeManager, listener);
                if (mCurrentSession.IsActive())
                {
                    Logger.e("Received attempt to create a new room without cleaning up the old one.");
                    newSession.LeaveRoom();
                    return;
                }

                mCurrentSession = newSession;

                // We're holding the session lock, so no other threads could have torn down the session
                // in the meantime.

                using (var configBuilder = RealtimeRoomConfigBuilder.Create()) {
                    var config = configBuilder.SetMinimumAutomatchingPlayers(minOpponents)
                                 .SetMaximumAutomatchingPlayers(maxOpponents)
                                 .SetVariant(variant)
                                 .Build();

                    using (config) {
                        using (var helper = HelperForSession(newSession)) {
                            newSession.StartRoomCreation(mNativeClient.GetUserId(),
                                                         () => mRealtimeManager.CreateRoom(config, helper,
                                                                                           newSession.HandleRoomResponse)
                                                         );
                        }
                    }
                }
            }
        }
        public void AcceptInvitation(string invitationId, RealTimeMultiplayerListener listener)
        {
            lock (mSessionLock)
            {
                var newRoom = new RoomSession(mRealtimeManager, listener);
                if (mCurrentSession.IsActive())
                {
                    Logger.e("Received attempt to accept invitation without cleaning up " +
                             "active session.");
                    newRoom.LeaveRoom();
                    return;
                }

                mCurrentSession = newRoom;

                mRealtimeManager.FetchInvitations(response =>
                {
                    if (!response.RequestSucceeded())
                    {
                        Logger.e("Couldn't load invitations.");
                        newRoom.LeaveRoom();
                        return;
                    }

                    foreach (var invitation in response.Invitations())
                    {
                        using (invitation)
                        {
                            if (invitation.Id().Equals(invitationId))
                            {
                                using (var helper = HelperForSession(newRoom))
                                {
                                    newRoom.StartRoomCreation(mNativeClient.GetUserId(),
                                                              () => mRealtimeManager.AcceptInvitation(
                                                                  invitation, helper, newRoom.HandleRoomResponse));
                                    return;
                                }
                            }
                        }
                    }

                    Logger.e("Room creation failed since we could not find invitation with ID "
                             + invitationId);
                    newRoom.LeaveRoom();
                });
            }
        }
        public void CreateWithInvitationScreen(uint minOpponents, uint maxOppponents, uint variant,
                                               RealTimeMultiplayerListener listener)
        {
            lock (mSessionLock)
            {
                var newRoom = new RoomSession(mRealtimeManager, listener);

                if (mCurrentSession.IsActive())
                {
                    Logger.e("Received attempt to create a new room without cleaning up the old one.");
                    newRoom.LeaveRoom();
                    return;
                }

                // The user attempted to create a room via the invitation screen, this is now the new
                // current room.
                mCurrentSession = newRoom;

                mRealtimeManager.ShowPlayerSelectUI(minOpponents, maxOppponents, true,
                                                    response =>
                {
                    if (response.Status() != Status.UIStatus.VALID)
                    {
                        Logger.d("User did not complete invitation screen.");
                        newRoom.LeaveRoom();
                        return;
                    }

                    using (var configBuilder = RealtimeRoomConfigBuilder.Create())
                    {
                        configBuilder.SetVariant(variant);
                        configBuilder.PopulateFromUIResponse(response);
                        using (var config = configBuilder.Build())
                        {
                            using (var helper = HelperForSession(newRoom))
                            {
                                newRoom.StartRoomCreation(mNativeClient.GetUserId(),
                                                          () => mRealtimeManager.CreateRoom(config, helper,
                                                                                            newRoom.HandleRoomResponse));
                            }
                        }
                    }
                });
            }
        }
 public void CreateQuickGame(uint minOpponents, uint maxOpponents, uint variant, ulong exclusiveBitMask, RealTimeMultiplayerListener listener)
 {
     lock (mSessionLock)
     {
         RoomSession newSession = new RoomSession(mRealtimeManager, listener);
         if (mCurrentSession.IsActive())
         {
             Logger.e("Received attempt to create a new room without cleaning up the old one.");
             newSession.LeaveRoom();
         }
         else
         {
             mCurrentSession = newSession;
             Logger.d("QuickGame: Setting MinPlayersToStart = " + minOpponents);
             mCurrentSession.MinPlayersToStart = minOpponents;
             using (RealtimeRoomConfigBuilder realtimeRoomConfigBuilder = RealtimeRoomConfigBuilder.Create())
             {
                 RealtimeRoomConfig config = realtimeRoomConfigBuilder.SetMinimumAutomatchingPlayers(minOpponents).SetMaximumAutomatchingPlayers(maxOpponents).SetVariant(variant)
                                             .SetExclusiveBitMask(exclusiveBitMask)
                                             .Build();
                 using (config)
                 {
                     GooglePlayGames.Native.PInvoke.RealTimeEventListenerHelper helper = HelperForSession(newSession);
                     try
                     {
                         newSession.StartRoomCreation(mNativeClient.GetUserId(), delegate
                         {
                             mRealtimeManager.CreateRoom(config, helper, newSession.HandleRoomResponse);
                         });
                     }
                     finally
                     {
                         if (helper != null)
                         {
                             ((IDisposable)helper).Dispose();
                         }
                     }
                 }
             }
         }
     }
 }
 public void CreateWithInvitationScreen(uint minOpponents, uint maxOppponents, uint variant, RealTimeMultiplayerListener listener)
 {
     lock (mSessionLock)
     {
         RoomSession newRoom = new RoomSession(mRealtimeManager, listener);
         if (mCurrentSession.IsActive())
         {
             Logger.e("Received attempt to create a new room without cleaning up the old one.");
             newRoom.LeaveRoom();
         }
         else
         {
             mCurrentSession           = newRoom;
             mCurrentSession.ShowingUI = true;
             RealtimeRoomConfig config;
             GooglePlayGames.Native.PInvoke.RealTimeEventListenerHelper helper;
             mRealtimeManager.ShowPlayerSelectUI(minOpponents, maxOppponents, true, delegate(PlayerSelectUIResponse response)
             {
                 mCurrentSession.ShowingUI = false;
                 if (response.Status() != CommonErrorStatus.UIStatus.VALID)
                 {
                     Logger.d("User did not complete invitation screen.");
                     newRoom.LeaveRoom();
                 }
                 else
                 {
                     mCurrentSession.MinPlayersToStart = (uint)((int)response.MinimumAutomatchingPlayers() + response.Count() + 1);
                     using (RealtimeRoomConfigBuilder realtimeRoomConfigBuilder = RealtimeRoomConfigBuilder.Create())
                     {
                         realtimeRoomConfigBuilder.SetVariant(variant);
                         realtimeRoomConfigBuilder.PopulateFromUIResponse(response);
                         config = realtimeRoomConfigBuilder.Build();
                         try
                         {
                             helper = HelperForSession(newRoom);
                             try
                             {
                                 newRoom.StartRoomCreation(mNativeClient.GetUserId(), delegate
                                 {
                                     mRealtimeManager.CreateRoom(config, helper, newRoom.HandleRoomResponse);
                                 });
                             }
                             finally
                             {
                                 if (helper != null)
                                 {
                                     ((IDisposable)helper).Dispose();
                                 }
                             }
                         }
                         finally
                         {
                             if (config != null)
                             {
                                 ((IDisposable)config).Dispose();
                             }
                         }
                     }
                 }
             });
         }
     }
 }
 public void LeaveRoom()
 {
     mCurrentSession.LeaveRoom();
 }
        public void AcceptInvitation(string invitationId, RealTimeMultiplayerListener listener)
        {
            lock (mSessionLock)
            {
                RoomSession newRoom = new RoomSession(mRealtimeManager, listener);
                if (mCurrentSession.IsActive())
                {
                    Logger.e("Received attempt to accept invitation without cleaning up active session.");
                    newRoom.LeaveRoom();
                }
                else
                {
                    mCurrentSession = newRoom;
                    GooglePlayGames.Native.PInvoke.RealTimeEventListenerHelper helper;
                    mRealtimeManager.FetchInvitations(delegate(RealtimeManager.FetchInvitationsResponse response)
                    {
                        if (!response.RequestSucceeded())
                        {
                            Logger.e("Couldn't load invitations.");
                            newRoom.LeaveRoom();
                        }
                        else
                        {
                            using (IEnumerator <GooglePlayGames.Native.PInvoke.MultiplayerInvitation> enumerator = response.Invitations().GetEnumerator())
                            {
                                GooglePlayGames.Native.PInvoke.MultiplayerInvitation invitation;
                                while (enumerator.MoveNext())
                                {
                                    invitation = enumerator.Current;
                                    using (invitation)
                                    {
                                        if (invitation.Id().Equals(invitationId))
                                        {
                                            mCurrentSession.MinPlayersToStart = invitation.AutomatchingSlots() + invitation.ParticipantCount();
                                            Logger.d("Setting MinPlayersToStart with invitation to : " + mCurrentSession.MinPlayersToStart);
                                            helper = HelperForSession(newRoom);
                                            try
                                            {
                                                newRoom.StartRoomCreation(mNativeClient.GetUserId(), delegate
                                                {
                                                    mRealtimeManager.AcceptInvitation(invitation, helper, newRoom.HandleRoomResponse);
                                                });
                                                return;

                                                IL_011e:;
                                            }
                                            finally
                                            {
                                                if (helper != null)
                                                {
                                                    ((IDisposable)helper).Dispose();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Logger.e("Room creation failed since we could not find invitation with ID " + invitationId);
                            newRoom.LeaveRoom();
                        }
                    });
                }
            }
        }