public void HandleAction(AUserAction action)
        {
            var spawned = PlayerDictionary.TryGetValue(action.Username, out var playerModel);

            if (!spawned)
            {
                throw new UnspawnedException("Player must be spawned first!");
            }
            actionQueue.Enqueue(action);
        }
        /// <summary>
        ///     Add action to server action queue
        /// </summary>
        /// <param name="action">Action to be added</param>
        public void RunAction(AUserAction action)
        {
            var spawned = PlayerDictionary.TryGetValue(action.Username, out var playerModel);

            if (!spawned)
            {
                return;
            }
            var playerBody = playerModel.ObjectBody;

            switch (action)
            {
            case MovementAction m:
            {
                var impulse = ConvertMovementActionToVector2(m, playerModel);
                playerBody.ApplyLinearImpulse(impulse, playerBody.WorldCenter);
                break;
            }

            case ShootingAction s:
            {
                PlayerDictionary.TryGetValue(s.Username, out var creator);
                if (creator == null)
                {
                    throw new Exception("Player not found when spawning bullet");
                }
                var bulletSpawn = new BulletSpawnObject(s.Angle, 20f, creator, 10f, .1f, creator.ObjectBody.Position);
                bulletSpawn.InitialMass = .02f;
                spawnQueue.Enqueue(bulletSpawn);

                break;
            }

            default:
                throw new ArgumentException("Unsupported Action Type");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Method to parse actions from the client on a timer. First, it will check if the user passed in is currently authenticated. If it isn't, then an error will be thrown.
        /// Next, it will check if the user has made an action in the last {default=50} milliseconds. If they have, return false. Otherwise, it updates the state of the game and then resets the player's
        /// last action time in memory.
        /// </summary>
        /// <param name="action">A JSON object representing the action</param>
        /// <returns>A boolean representing whether the action was done successfully</returns>
        public bool ParseAction(AUserAction action)
        {
            //Make sure that the user is authenticated
            if (!_ls.VerifyToken(action.Token, action.Username))
            {
                throw new InvalidLoginException("Either the token or the username is invalid.");
            }

            try
            {
                if (action is MovementAction)
                {
                    //If the action's user DNE
                    if (!_movementTimings.ContainsKey(action.Username))
                    {
                        _movementTimings.TryAdd(action.Username, DateTime.Now);
                        //Run relevant method to update game state
                        _gl.HandleAction(action);
                        //
                        return(true);
                    }

                    //fetch last update time
                    DateTime x = _movementTimings[action.Username];

                    var ts = DateTime.Now - x;

                    //It's been at least {MovementInterval} milliseconds since last action
                    if (ts.TotalMilliseconds >= MovementInterval)
                    {
                        _movementTimings[action.Username] = DateTime.Now;
                        //Run relevant method to update game state
                        _gl.HandleAction(action);
                        //
                        return(true);
                    }

                    return(false);
                }
                else if (action is ShootingAction)
                {
                    //If the action's user DNE
                    if (!_shootingTimings.ContainsKey(action.Username))
                    {
                        _shootingTimings.TryAdd(action.Username, DateTime.Now);
                        //Run relevant method to update game state
                        _gl.HandleAction(action);
                        //
                        return(true);
                    }

                    //fetch last update time
                    DateTime x = _shootingTimings[action.Username];

                    var ts = DateTime.Now - x;

                    //It's been at least {MovementInterval} milliseconds since last action
                    if (ts.TotalMilliseconds >= _gl.getFireRate(action.Username))
                    {
                        _shootingTimings[action.Username] = DateTime.Now;
                        //Run relevant method to update game state
                        _gl.HandleAction(action);
                        //
                        return(true);
                    }

                    return(false);
                }
                else
                {
                    return(false);
                }
            }
            catch (UnspawnedException e)
            {
                return(false);
            }
        }