Esempio n. 1
0
        public IEnumerable<IEvent> Handle(MakeChoiceCommand command)
        {
            var events = new List<IEvent>();
            var playerOneChoice = playerOne.CurrentChoice;
            var playerTwoChoice = playerTwo.CurrentChoice;

            if (state != GameState.Started)
                return events;

            if (IsPlayerOne(command.PlayerId) && playerOneChoice == Choice.None)
                playerOneChoice = PlayerChoice(command, events);
            else if (IsPlayerTwo(command.PlayerId) && playerTwoChoice == Choice.None)
                playerTwoChoice = PlayerChoice(command, events);

            // Decide winner if both has chosen
            if (playerOneChoice != Choice.None && playerTwoChoice != Choice.None)
            {
                // decide round
                var newRound = true;
                Choice winsAgainstOne = winnersAgainst[playerOne.CurrentChoice];

                if (playerTwoChoice == winsAgainstOne)
                    newRound = RoundWonBy(playerTwo, command, events);
                else if (playerOneChoice == playerTwo.CurrentChoice)
                    events.Add(new RoundTiedEvent(command.EntityId, round));
                else
                    newRound = RoundWonBy(playerOne, command, events);

                if (newRound)
                    events.Add(new RoundStartedEvent(command.EntityId, round + 1));
            }
            return events;
        }
Esempio n. 2
0
 private Choice PlayerChoice(MakeChoiceCommand command, List<IEvent> events)
 {
     events.Add(new ChoiceMadeEvent(
                  command.EntityId,
                  this.round,
                  command.PlayerId,
                  command.Choice));
     var playerChoice = command.Choice;
     return playerChoice;
 }
Esempio n. 3
0
 private bool RoundWonBy(GamePlayer player, MakeChoiceCommand command, List<IEvent> events)
 {
     events.Add(new RoundWonEvent(command.EntityId, player.Email, round));
     if (IsWinner(playerTwo))
     {
         GameWonBy(playerTwo, command, events);
         return false;
     }
     return true;
 }
Esempio n. 4
0
 private void GameWonBy(GamePlayer player, MakeChoiceCommand command, List<IEvent> events)
 {
     events.Add(new GameWonEvent(command.EntityId, player.Email));
 }