예제 #1
0
파일: RealPlayer.cs 프로젝트: Frazi1/Chess
 public void MakeMove()
 {
     PrepareMove();
     if (MakeMoveCommand != null)
     {
         MakeMoveCommand.Execute();
         OnMove();
     }
 }
        public async Task <MoveResult> HandleAsync(MakeMoveCommand command, CancellationToken cancellationToken)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            using (var session = _documentStore.OpenAsyncSession())
            {
                // TODO: Concurrency protection
                var gameId = command.GameId;
                var game   = await session.LoadGameAsync(gameId, cancellationToken).ConfigureAwait(false);

                var currentPlayerId     = GetPlayerIdByPlayerOrder(game, game.NextPlayer);
                var currentVisibleTable = EnumArrayCloner.Clone(game.VisibleTable);
                var mineCountBeforeMove = GetRemainingMineCount(game);

                var player1PointsBeforeMove = game.Player1.Points;
                var player2PointsBeforeMove = game.Player2.Points;

                var movementResult = _gameDriver.MakeMove(game, command.PlayerId, command.Row, command.Column);

                if (movementResult == MoveResultType.Success || movementResult == MoveResultType.GameOver)
                {
                    await session.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    var mineCountAfterMove = GetRemainingMineCount(game);
                    var nextPlayerId       = GetPlayerIdByPlayerOrder(game, game.NextPlayer);

                    await PublishTableUpdatedAsync(gameId, currentVisibleTable, game.VisibleTable, cancellationToken).ConfigureAwait(false);;
                    await PublishPlayersTurnIfChangedAsync(gameId, currentPlayerId, nextPlayerId, cancellationToken).ConfigureAwait(false);
                    await PublishRemainingMinesIfChangedAsync(gameId, mineCountBeforeMove, mineCountAfterMove, cancellationToken).ConfigureAwait(false);
                    await PublishPointsIfChangedAsync(
                        gameId,
                        game.Player1.PlayerId,
                        game.Player2.PlayerId,
                        player1PointsBeforeMove,
                        game.Player1.Points,
                        player2PointsBeforeMove,
                        game.Player2.Points,
                        cancellationToken).ConfigureAwait(false);

                    if (movementResult == MoveResultType.GameOver)
                    {
                        await PublishWinnerAsync(gameId, game.Winner, game.Player1.PlayerId, game.Player2.PlayerId, cancellationToken).ConfigureAwait(false);
                    }
                }

                return(new MoveResult {
                    MoveResultType = movementResult
                });
            }
        }
예제 #3
0
        public async Task <ActionResult> MakeMove([FromBody] MakeMoveCommand command)
        {
            if (PlayerId == null)
            {
                return(Forbid());
            }

            Game game = _gameRepository.GetGame(GameId, PlayerId);

            if (game == null)
            {
                return(NotFound());
            }

            game.MakeMove(PlayerId, command.Amount);

            await EmitGameStateAsync(game);

            return(Ok());
        }
예제 #4
0
파일: RealPlayer.cs 프로젝트: Frazi1/Chess
        public void PrepareMove()
        {
            var move = Strategy.PrepareMove();

            MakeMoveCommand = new MakeMoveCommand(this, move.Item1, move.Item2, _game);
        }