Ejemplo n.º 1
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.MySquare.Dispose();
                            user.Connected = false;
                        }
                        else
                        {
                            // Remove me from the ship hosts remote controllers
                            if (user.MySquare != null)
                            {
                                user.MySquare.Host.RemoteControllers.Remove(user);
                                user.MySquare = null;
                            }

                            _userHandler.RemoveUser(connectionId);
                        }

                        IHubContext context = Game.GetContext();

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

                        user.RemoteControllers.Clear();
                    }
                }
                catch (Exception e)
                {
                    //ErrorLog.Instance.Log(e);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves the game's configuration
        /// </summary>
        /// <returns>The game's configuration</returns>
        public object initializeController(string connectionId, RegisteredClient rc)
        {
            if (UserHandler.UserExistsAndReady(connectionId))
            {
                return(null);
            }

            try
            {
                User main = UserHandler.FindUserByIdentity(rc.Identity);

                if (main != null)
                {
                    User controllerUser = new User(connectionId, rc)
                    {
                        Controller = true
                    };

                    controllerUser.MySquare = main.MySquare;

                    UserHandler.AddUser(controllerUser);
                    main.RemoteControllers.Add(controllerUser);

                    return(new
                    {
                        Configuration = Configuration,
                    });
                }
                else
                {
                    return(new
                    {
                        FailureMessage = "Could not find logged in user to control."
                    });
                }
            }
            catch (Exception e)
            {
                //ErrorLog.Instance.Log(e);
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves the game's configuration
        /// </summary>
        /// <returns>The game's configuration</returns>
        public object initializeClient(string connectionId, RegisteredClient rc)
        {
            if (UserHandler.UserExistsAndReady(connectionId))
            {
                return(null);                // user already exists
            }
            // user does not exist. setup client
            try
            {
                lock (_locker)
                {
                    User   user = UserHandler.FindUserByIdentity(rc.Identity);
                    Square square;

                    if (user == null)
                    {
                        // user doesn't exist

                        if (UserHandler.TotalActiveUsers >= RuntimeConfiguration.MaxServerUsers)
                        {
                            // server is full!
                            return(new
                            {
                                ServerFull = true
                            });
                        }
                        else
                        {
                            // server is not full! generate a square for user
                            square      = new Square();
                            square.Name = rc.DisplayName;
                            user        = new User(connectionId, square, rc)
                            {
                                Controller = false
                            };
                            UserHandler.AddUser(user);
                        }
                    }
                    else
                    {
                        // user exists!
                        string previousConnectionID = user.ConnectionID;
                        UserHandler.ReassignUser(connectionId, user);
                        square = user.MySquare;

                        if (user.Connected)
                        {
                            // user already logged in
                            GetContext().Clients.Client(previousConnectionID).controlTransferred();
                        }
                        else
                        {
                            // user was not connected
                            square.Disposed = false;
                            user.Connected  = true;
                        }
                    }

                    GameHandler.AddSquareToGame(square);
                }

                return(new
                {
                    Configuration = Configuration,
                    ServerFull = false,
                    SquareID = UserHandler.GetUserSquare(connectionId).ID,
                    SquareName = UserHandler.GetUserSquare(connectionId).Name,
                    CompressionContracts = new
                    {
                        PayloadContract = _payloadManager.Compressor.PayloadCompressionContract,
                        SquareContract = _payloadManager.Compressor.SquareCompressionContract,
                        CollidableContract = _payloadManager.Compressor.CollidableCompressionContract,
                        EnemyContract = _payloadManager.Compressor.EnemyCompressionContract,
                    }
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                //ErrorLog.Instance.Log(e);
            }

            return(null);
        }