public async Task OnDisconnectedAsync(string connectionId)
        {
            await _gameLock.WaitAsync();

            try
            {
                if (_userHandler.UserExistsAndReady(connectionId))
                {
                    User user = _userHandler.GetUser(connectionId);

                    //It's possible for a controller to disconnect without a ship
                    if (!user.Controller)
                    {
                        user.MyShip.Dispose();
                        user.Connected = false;
                    }
                    else
                    {
                        // Remove me from the ship hosts remote controllers
                        if (user.MyShip != null)
                        {
                            user.MyShip.Host.RemoteControllers.Remove(user);
                            user.MyShip.Host.NotificationManager.Notify("Detached controller.");
                            user.MyShip = null;
                        }

                        _userHandler.RemoveUser(connectionId);
                    }

                    // Leave the leaderboard group just in case user was in it
                    await _gameHub.Groups.RemoveFromGroupAsync(connectionId, Leaderboard.LEADERBOARD_REQUESTEE_GROUP);

                    // Clear controllers
                    foreach (User u in user.RemoteControllers)
                    {
                        u.MyShip = null;
                        await _gameHub.Clients.Client(u.ConnectionID).SendAsync("stopController", "Primary account has been stopped!");
                    }

                    user.RemoteControllers.Clear();
                }
            }
            catch (Exception e)
            {
                //ErrorLog.Instance.Log(e);
            }
            finally
            {
                _gameLock.Release();
            }
        }
Beispiel #2
0
        /// <summary>
        /// On disconnect we need to remove the ship from our list of ships within the gameHandler.
        /// This also means we need to notify clients that the ship has been removed.
        /// </summary>
        public void OnDisconnected(string connectionId)
        {
            lock (_locker)
            {
                try
                {
                    if (_userHandler.UserExistsAndReady(connectionId))
                    {
                        User user = _userHandler.GetUser(connectionId);

                        //It's possible for a controller to disconnect without a ship
                        if (!user.Controller)
                        {
                            user.MyShip.Dispose();
                            user.Connected = false;
                        }
                        else
                        {
                            // Remove me from the ship hosts remote controllers
                            if (user.MyShip != null)
                            {
                                user.MyShip.Host.RemoteControllers.Remove(user);
                                user.MyShip.Host.NotificationManager.Notify("Detached controller.");
                                user.MyShip = null;
                            }

                            _userHandler.RemoveUser(connectionId);
                        }

                        // Leave the leaderboard group just in case user was in it
                        IHubContext context = Game.GetContext();
                        context.Groups.Remove(connectionId, Leaderboard.LEADERBOARD_REQUESTEE_GROUP);

                        // Clear controllers
                        foreach (User u in user.RemoteControllers)
                        {
                            u.MyShip = null;
                            context.Clients.Client(u.ConnectionID).stopController("Primary account has been stopped!");
                        }

                        user.RemoteControllers.Clear();
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Instance.Log(e);
                }
            }
        }