Exemplo n.º 1
0
        public IEnumerable<IEvent> Handle(MakeMove move)
        {
            CanMakeMove(move);

            yield return new MoveMade(move.AggregateId, move.PlayerId, move.Move);

            if (_state == GameState.WaitingForMove)
            {
                if (_latestMove.Item1 == move.Move)
                {
                    yield return new RoundTied(_gameId);
                }
                else
                {
                    var winningPlayer = GetWinning(move);

                    yield return new RoundWon(_gameId, winningPlayer);

                    if (_scores[winningPlayer] + 1 == _firstTo)
                    {
                        yield return new GameWon(_gameId, winningPlayer, OtherPlayer(winningPlayer));
                    }
                }
            }
        }
Exemplo n.º 2
0
        string GetWinning(MakeMove move)
        {
            string winningPlayer = "";

            switch (move.Move)
            {
                case Move.Paper:
                    winningPlayer = _latestMove.Item1 == Move.Scissors ? _latestMove.Item2 : OtherPlayer(_latestMove.Item2);
                    break;
                case Move.Scissors:
                    winningPlayer = _latestMove.Item1 == Move.Rock ? _latestMove.Item2 : OtherPlayer(_latestMove.Item2);
                    break;
                case Move.Rock:
                    winningPlayer = _latestMove.Item1 == Move.Paper ? _latestMove.Item2 : OtherPlayer(_latestMove.Item2);
                    break;
            }
            return winningPlayer;
        }
Exemplo n.º 3
0
        void CanMakeMove(MakeMove move)
        {
            if (_state == GameState.NotCreated || _state == GameState.GameWon)
            {
                throw new InvalidOperationException("Invalid state");
            }

            if (_state == GameState.WaitingForMove)
            {
                if (_latestMove.Item2 == move.PlayerId)
                {
                    throw new InvalidOperationException("Cannot make two moves...");
                }
            }
        }