Пример #1
0
        public void WhenUserGuessNo_ThenItShouldBeTheWinner()
        {
            const int expectedGuessNo      = 5;
            const int expectedWinnerUserId = 2;
            var       gameFixture          = new GameFixture();
            const int noOfRounds           = 3;
            var       game = gameFixture.CreateGame(expectedGuessNo);

            var usersForGame = new List <int> {
                1, expectedWinnerUserId, 3
            };

            game.Start(noOfRounds, 100, usersForGame);

            var expectedNonWinner = new UserInGame(1, expectedGuessNo);
            var expectedWinner    = new UserInGame(expectedWinnerUserId, expectedGuessNo - 4);
            var usersInGame       = new List <UserInGame>()
            {
                expectedNonWinner,
                expectedWinner,
                new UserInGame(3, expectedGuessNo + 44)
            };

            game.Run(usersInGame);
            expectedNonWinner.Number = expectedGuessNo + 22;
            expectedWinner.Number    = expectedGuessNo;

            game.Run(usersInGame);
            game.Run(usersInGame);

            var actualGameWinner = game.GameWinnerId;

            Assert.Equal(expectedWinner.UserId, actualGameWinner);
        }
Пример #2
0
        public async Task AddUserToGame(UserInGame user)
        {
            var game = await Games.GetById(user.GameId);

            if (game.Participants.Any(p => p.ApplicationUserId.Equals(user.ApplicationUserId)))
            {
                return;
            }
            await Players.Insert(user);
        }
Пример #3
0
        public async Task <IHttpActionResult> Join(Guid?id)
        {
            var user = new UserInGame()
            {
                ApplicationUserId = HttpContext.Current.User.Identity.GetUserId(),
                GameId            = id.Value,
                Quiz = new Quiz()
            };
            await games.AddUserToGame(user);

            return(Ok());
        }
Пример #4
0
        private string GetWinner(User user, UserInGame userInGame, List <Bot> bots, List <BotInGame> botInGames)
        {
            var winner = botInGames?
                         .Where(x => x.CountPoint <= 21)?
                         .MaxBy(x => x.CountPoint)?
                         .FirstOrDefault();

            var bot = bots?.FirstOrDefault(x => x.Id == winner?.BotId);

            var winnerName = (userInGame.CountPoint > winner?.CountPoint && userInGame.CountPoint <= 21) ? user.UserName : bot?.Name;

            return(winnerName);
        }
        public void WhenGameFinishes_ThenShouldSelectTheWinner()
        {
            var expectedGuessNo = 10;

            var gameServiceFixture = new GameServiceFixture();
            var gameService        = gameServiceFixture.CreateGameService();

            var newGameRequest = new NewGameRequest
            {
                MaxGuessNo  = 10,
                NoOfRounds  = 3,
                UsersInGame = new[] { 1, 2, 3 }
            };

            var gameId = gameService.Create(newGameRequest);

            var roundRepository = new RoundRepositoryInMemory();

            var roundOfGame = new RoundOfGame(gameServiceFixture.GameRepository, roundRepository,
                                              gameServiceFixture.UnitOfWork);
            var expectedNonWinner = new UserInGame(1, expectedGuessNo);
            var expectedWinner    = new UserInGame(2, expectedGuessNo - 4);
            var usersInGame       = new List <UserInGame>
            {
                expectedNonWinner,
                expectedWinner,
                new UserInGame(3, expectedGuessNo - 5)
            };

            roundOfGame.RunGame(gameId, usersInGame);
            expectedNonWinner.Number = expectedGuessNo - 5;
            expectedWinner.Number    = expectedGuessNo;

            roundOfGame.RunGame(gameId, usersInGame);
            roundOfGame.RunGame(gameId, usersInGame);

            var gameWinner = gameService.GetGameWinner(gameId);

            Assert.Equal(expectedWinner.UserId, gameWinner);
        }
Пример #6
0
        private void DealCard(Game game, List <Card> deck, List <Card> cardsForRemove, string userId, UserStep stepUser, UserInGame userInGame, List <Bot> bots, List <BotStep> stepBots, List <BotInGame> botInGames)
        {
            var currentCard = deck.FirstOrDefault();

            stepUser.GameId = game.Id;
            stepUser.UserId = userId;
            stepUser.Suit   = currentCard.Suit;
            stepUser.Rank   = currentCard.Rank;

            userInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, userInGame.CountPoint);

            cardsForRemove.Add(currentCard);
            deck.Remove(currentCard);


            bots.ForEach(x =>
            {
                currentCard = deck.FirstOrDefault();

                var pointBot = botInGames.FirstOrDefault(p => p.BotId == x.Id);

                if (IsNeedCard(pointBot))
                {
                    var stepBot = new BotStep
                    {
                        BotId  = x.Id,
                        GameId = game.Id,
                        Rank   = currentCard.Rank,
                        Suit   = currentCard.Suit
                    };
                    stepBots.Add(stepBot);

                    pointBot.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, pointBot.CountPoint);

                    cardsForRemove.Add(currentCard);
                    deck.Remove(currentCard);
                }
            });
        }
Пример #7
0
        public async Task <StartGameResponseView> Start(int countBots, string userId)
        {
            using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userInGame = await _unitOfWork.UserInGames.GetUnfinished(userId);

                var result = new StartGameResponseView();

                if (userInGame != null)
                {
                    result.GameId = userInGame.GameId;
                    return(result);
                }

                var game = new Game
                {
                    CountBots  = countBots,
                    WinnerName = String.Empty
                };



                await _unitOfWork.Games.Create(game);

                var bots = await _unitOfWork.Bots.GetCount(countBots);

                var deck = await CreateDeck(game);

                var userSteps = new List <UserStep>();
                var botSteps  = new List <BotStep>();

                var newUserInGame = new UserInGame
                {
                    GameId     = game.Id,
                    UserId     = userId,
                    CountPoint = 0
                };

                var botInGames = bots.Select(x => new BotInGame
                {
                    BotId      = x.Id,
                    GameId     = game.Id,
                    CountPoint = 0
                }).ToList();
                var cardsForRemove = new List <Card>();


                DealTwoCards(game, deck, cardsForRemove, userId, userSteps, newUserInGame, bots, botSteps, botInGames);

                await _unitOfWork.UserSteps.CreateRange(userSteps);

                await _unitOfWork.BotSteps.CreateRange(botSteps);

                await _unitOfWork.Cards.DeleteRange(cardsForRemove);

                await _unitOfWork.UserInGames.Create(newUserInGame);

                await _unitOfWork.BotInGames.CreateRange(botInGames);

                result.GameId = game.Id;

                transactionScope.Complete();

                return(result);
            }
        }
Пример #8
0
        private void DealTwoCards(Game game, List <Card> deck, List <Card> cardsForRemove, string userId, List <UserStep> stepUsers, UserInGame userInGame, List <Bot> bots, List <BotStep> stepBots, List <BotInGame> botInGames)
        {
            var startCards = 2;

            for (int i = 0; i < startCards; i++)
            {
                var currentCard = deck.FirstOrDefault();
                var stepUser    = new UserStep
                {
                    GameId = game.Id,
                    UserId = userId,
                    Suit   = currentCard.Suit,
                    Rank   = currentCard.Rank
                };
                stepUsers.Add(stepUser);

                userInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, userInGame.CountPoint);


                cardsForRemove.Add(currentCard);
                deck.Remove(currentCard);


                bots.ForEach(x =>
                {
                    currentCard = deck.FirstOrDefault();
                    stepBots.Add(new BotStep
                    {
                        GameId = game.Id,
                        BotId  = x.Id,
                        Suit   = currentCard.Suit,
                        Rank   = currentCard.Rank
                    });
                    var botInGame         = botInGames.FirstOrDefault(b => b.BotId == x.Id);
                    botInGame.CountPoint += _scoreHelper.GetValueCard(currentCard.Rank, botInGame.CountPoint);

                    cardsForRemove.Add(currentCard);
                    deck.Remove(currentCard);
                });
            }
        }