Exemplo n.º 1
0
        public async Task <IActionResult> StartAsync(GameControlEvent gameEvent, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Received {Command}", gameEvent.Command);
            switch (gameEvent.Command)
            {
            case GameCommand.Start:
                if (gameEvent.AddressedTo == _playerName)
                {
                    _logger.LogInformation("Starting the game");
                    await _playerStateManager.SetStateAsync(State.MyTurn, cancellationToken).ConfigureAwait(false);
                }

                break;

            case GameCommand.Stop:
                await _playerStateManager.SetStateAsync(State.Ready, cancellationToken).ConfigureAwait(false);

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(new OkResult());
        }
Exemplo n.º 2
0
        public async Task ExecuteIterationAsync(string playerName, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Player {PlayerName} loop iteration started", _configuration.Value.PlayerName);
            var playerState = await _playerStateManager.GetStateAsync(cancellationToken).ConfigureAwait(false);

            switch (playerState.Current)
            {
            case State.Ready:
                _logger.LogInformation("Player {PlayerName} is ready, waiting for any actions",
                                       _configuration.Value.PlayerName);
                break;

            case State.MyTurn:
                await _playerStateManager.SetStateAsync(State.Ready, playerState.Counter + 1, cancellationToken)
                .ConfigureAwait(false);

                if (_playersLuck.ShouldMissCurrentTake(playerState.Counter))
                {
                    _logger.LogWarning("Player {PlayerName} missed their turn", playerName);
                    await _gameEventManager.PublishGameEventAsync(new PlayerLostEvent
                    {
                        PlayerName = playerName
                    }, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await _gameEventManager.PublishGameEventAsync(new GameProcessEvent
                    {
                        Ping        = playerState.Counter,
                        AddressedTo = _configuration.Value.OpponentName
                    }, cancellationToken).ConfigureAwait(false);

                    _logger.LogInformation("Player {PlayerName} has taken their turn", playerName);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }