public IHttpActionResult Play(PlayRequestModel request)
        {
            if (request == null || !this.ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var currentUserId = this.User.Identity.GetUserId();
            var gameIdAsGuid = new Guid(request.GameId);

            var game = this.gameService.All()
                                        .Where(x => x.Id == gameIdAsGuid)
                                        .FirstOrDefault();

            // check if exist that game
            if (game == null)
            {
                return this.BadRequest(GameMessages.InvalidGameIdMessage);
            }

            // chaeck if current user is part of this game
            if (game.WhitePlayerId != currentUserId && game.BlackPlayerId != currentUserId)
            {
                this.BadRequest(GameMessages.NotPartOfGameMessage);
            }

            //check if game is still playing
            if (game.GameState != GameState.TurnWhitePlayer && game.GameState != GameState.TurnBlackPlayer)
            {
                return this.BadRequest(GameMessages.NotPlayingGameMessage);
            }

            // check if current player must play
            if ((game.GameState == GameState.TurnWhitePlayer && game.WhitePlayerId != currentUserId) ||
                (game.GameState == GameState.TurnBlackPlayer && game.BlackPlayerId != currentUserId))
            {
                return this.BadRequest(GameMessages.NotYourTurnMessage);
            }

            var result = this.resultValidator.GetGameResult(game.Board);

            switch (result)
            {
                case GameResult.NotFinished:
                    break;
                case GameResult.WonByWhitePlayer:
                    game.GameState = GameState.WonWhitePlayer;
                    this.gameService.Update(game);
                    break;
                case GameResult.WonByBlackPlayer:
                    game.GameState = GameState.WonBlackPlayer;
                    this.gameService.Update(game);
                    break;
                case GameResult.Draw:
                    game.GameState = GameState.DrawGame;
                    this.gameService.Update(game);
                    break;
                default:
                    break;
            }

            //TODO:
            // Update board
            // Check position
            // Check if game ends

            return this.Ok();
        }
Beispiel #2
0
        public IHttpActionResult PlayTurn(PlayRequestModel request)
        {
            if (request == null || !this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            var userId     = this.userProvider.GetUserId();
            var gameIdGuid = new Guid(request.GameId);

            Game currentGame = this.gameData.Games.Find(gameIdGuid);

            if (currentGame == null)
            {
                return(BadRequest("Game does not exist."));
            }

            if (currentGame.PlayerOneId != userId && currentGame.PlayerTwoId != userId)
            {
                return(BadRequest("You are not part of this game."));
            }

            if (currentGame.State == GameState.NotStarted)
            {
                return(BadRequest("Game has not started."));
            }

            if (currentGame.State == GameState.WonByPlayerOne ||
                currentGame.State == GameState.WonByPlayerTwo ||
                currentGame.State == GameState.Draw)
            {
                return(BadRequest("Game has already finished."));
            }

            if (currentGame.State == GameState.PlayerOneTurn && userId != currentGame.PlayerOneId ||
                currentGame.State == GameState.PlayerTwoTurn && userId != currentGame.PlayerTwoId)
            {
                return(BadRequest("It is not your turn."));
            }

            int playedPosition = (request.Row - 1) * 3 + request.Column - 1;

            if (currentGame.Board[playedPosition] != '-')
            {
                return(BadRequest("Position is occupied."));
            }

            StringBuilder updatedBoard = new StringBuilder(currentGame.Board);

            updatedBoard[playedPosition] = currentGame.State == GameState.PlayerOneTurn ? 'X' : 'O';

            currentGame.Board = updatedBoard.ToString();
            this.gameData.SaveChanges();

            GameResult result = this.gameLogic.GetResult(currentGame.Board);

            switch (result)
            {
            case GameResult.WonByX:
                currentGame.State = GameState.WonByPlayerOne;
                break;

            case GameResult.WonByO:
                currentGame.State = GameState.WonByPlayerTwo;
                break;

            case GameResult.Draw:
                currentGame.State = GameState.Draw;
                break;

            case GameResult.NotFinished:
                currentGame.State = currentGame.State == GameState.PlayerOneTurn ? GameState.PlayerTwoTurn : GameState.PlayerOneTurn;
                break;
            }

            this.gameData.SaveChanges();

            return(Ok(currentGame.Board));
        }