public IActionResult Put(int id, [FromBody] D.Stone newStone) { _ = new MoveResult(); MoveResult result; try { result = _gameService.MakeMove(id, newStone); } catch (GameException execption) { return(Problem(execption.Result.ToString(), statusCode: (int)execption.HttpStatusCode, type: ((int)execption.Result).ToString())); } return(Ok(result)); }
public MoveResult MakeMove(int gameId, D.Stone newStone) { MoveResult result = new MoveResult(); Game game = _gameRepository.Get(gameId).ToClient(); if (game is null) { throw new GameException(GameResult.GameNotExist, HttpStatusCode.NotFound, "Game do not exist"); } if (!(game.Result is null)) { throw new GameException(GameResult.GameFinished, HttpStatusCode.BadRequest, "Game already finished"); } result.BlackCapture = game.BlackCapture; result.WhiteCapture = game.WhiteCapture; result.KoInfo = game.KoInfo; if (newStone.Color is true && game.WhiteState != true || newStone.Color is false && game.BlackState != true) { throw new GameException(GameResult.OtherPlayerTurn, HttpStatusCode.BadRequest, "Not your turn"); } game.Rule = _ruleRepository.Get(game.Rule.Id); List <D.Stone> stones = _stoneRepository.Get(gameId).ToList(); Board board = new Board(stones, game.Size, game.Size, game.BlackPlayer, game.WhitePlayer, game.BlackCapture, game.WhiteCapture, game.KoInfo); if (!board.IsValid()) { throw new GameException(GameResult.BoardNotValid, HttpStatusCode.BadRequest, "Board not valid"); } board.SetCaptures(game.BlackPlayer, game.BlackCapture); board.SetCaptures(game.WhitePlayer, game.WhiteCapture); if (!board.Has(newStone) || newStone.Color is null) { throw new GameException(GameResult.InvalidMove, HttpStatusCode.BadRequest, "Invalid move"); } Board move = board.MakeMove(newStone, game.Rule.Suicide, game.Rule.Overwrite, game.Rule.Ko); result = board.Diff(move); foreach (D.Stone stone in result.Stones) { _ = stone.Color is null?_stoneRepository.DeleteStone(gameId, stone) : _stoneRepository.AddStone(gameId, stone); } result.BlackState = !(game.BlackState is true); result.WhiteState = !(game.WhiteState is true); game.BlackCapture = move.GetCaptures(game.BlackPlayer); game.WhiteCapture = move.GetCaptures(game.WhitePlayer); game.BlackState = result.BlackState; game.WhiteState = result.WhiteState; game.KoInfo = move.KoInfo; _gameRepository.Update(gameId, game.ToDal()); return(result); }