Example #1
0
        public async Task <ReasonResult <GameModel> > GetGameByIdAsync(int id, ClaimsUser user)
        {
            var game = await _dbContext.Games.FirstOrDefaultAsync(x => x.Id == id);

            if (game == null)
            {
                return(ReasonResult <GameModel> .NotFound());
            }

            if (game.PlayerOne != user.Id && game.PlayerTwo != user.Id)
            {
                return(ReasonResult <GameModel> .NotFound());
            }

            return(ReasonResult <GameModel> .Success(new GameModel(game)));
        }
Example #2
0
        public async Task <ReasonResult <GameModel> > TakeTurnAsync(int id, int turnX, int turnY, ClaimsUser user)
        {
            var game = await _dbContext.Games
                       .FirstOrDefaultAsync(x => x.Id == id);

            if (game == null)
            {
                return(ReasonResult <GameModel> .NotFound());
            }

            BoardPlayer player;

            if (game.PlayerOne == user.Id)
            {
                player = BoardPlayer.PlayerOne;
            }
            else if (game.PlayerTwo == user.Id)
            {
                player = BoardPlayer.PlayerTwo;
            }
            else
            {
                return(ReasonResult <GameModel> .NotFound());
            }

            if (game.GameState != GameState.InProgress)
            {
                _logger.LogInformation("Player tried to take turn on game with incorrect state, {@details}", new
                {
                    GameId    = game.Id,
                    GameState = game.GameState,
                    UserId    = user.Id,
                });

                return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState }));
            }

            var oldDecompressedBoardState = new DecompressedBoardState(game.BoardHistory);
            var takeTurnResult            = oldDecompressedBoardState.TryTakeTurn(turnX, turnY, player, out var newDecompressedBoardState);

            switch (takeTurnResult)
            {
            case TakeTurnResult.Success:
            {
                _logger.LogInformation("Player took turn, {@details}", new
                    {
                        GameId          = game.Id,
                        UserId          = user.Id,
                        GameState       = game.GameState,
                        X               = turnX,
                        Y               = turnY,
                        OldBoardHistory = oldDecompressedBoardState.BoardHistory,
                        NewBoardHistory = newDecompressedBoardState.BoardHistory,
                        Snapshot        = newDecompressedBoardState.ToString(),
                    });

                UpdateGameUsingDecompressedBoardState(game, newDecompressedBoardState);

                await _dbContext.SaveChangesAsync();

                return(ReasonResult <GameModel> .Success(new GameModel(game)));
            }

            case TakeTurnResult.NotInProgress:
                return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState }));

            case TakeTurnResult.TileAlreadySet:
                return(ReasonResult <GameModel> .BadRequest(new[] { TileAlreadySet }));

            case TakeTurnResult.WrongPlayer:
                return(ReasonResult <GameModel> .BadRequest(new[] { ActingOnWrongTurn }));

            default:
                throw new Exception($"Unknown failure state reached during {nameof(DecompressedBoardState)}.{nameof(DecompressedBoardState.TryTakeTurn)}");
            }
        }