Exemplo n.º 1
0
        public async Task <IActionResult> SetMovementAsync(Guid gameRoomId, [FromBody] MovementPutVM movementPatch)
        {
            var ct       = HttpContext.RequestAborted;
            var gameRoom = await gameRoomAdapter.GetAsync(gameRoomId, ct);

            if (gameRoom == null)
            {
                return(BadRequest("Invalid game room number."));
            }
            var gameManager = await gameManagerFactory.FactoryAsync(gameRoom, ct);

            try
            {
                var playerId = gameRoom.Players[movementPatch.PlayerNumber - 1].GameUserId;
                var movement = new Movement(
                    movementPatch.X, movementPatch.Y, movementPatch.Counter,
                    playerId, gameRoomId, DateTimeOffset.Now
                    );
                await gameManager.MoveAsync(movement, ct);
            }
            catch (GameException ex)
            {
                return(BadRequest(ex.Message));
            }
            return(Ok());
        }
        public async Task <GameRoom> MatchAsync(Player player, CancellationToken cancellationToken)
        {
            var createdGameRoom = await gameRoomAdapter.GetLatestByPlayerAsync(player, cancellationToken);

            if (createdGameRoom != null)
            {
                var gameManager = await gameManagerFactory.FactoryAsync(createdGameRoom, cancellationToken);

                if (!await gameManager.IsEndedAsync(cancellationToken))
                {
                    return(createdGameRoom);
                }
            }

            return(null);
        }