Ejemplo n.º 1
0
        private async Task <EndGameResponseView> CreateEndGameResponseView(string userId, Game game, UsersPoints usersPoints, List <BotsPoints> botsPoints)
        {
            var user = await _userRepository.GetByIdAsync(userId);

            var userSteps = (await _usersStepRepository.GetStepsByGameIdAsync(game.Id)).ToList();

            var response = new EndGameResponseView();

            response.GameId = game.Id;

            var bots = new List <BotEndGameResponseViewItem>();

            botsPoints.ForEach(
                bp =>
            {
                bots.Add(new BotEndGameResponseViewItem()
                {
                    CardsInHand = bp.CardsInHand,
                    Name        = bp.Bot.Name
                });
            }
                );

            response.Bots = bots;
            var userName = user.UserName;
            var cards    = new List <CardEndGameResponseViewItem>();

            userSteps.ForEach(
                x => cards.Add(new CardEndGameResponseViewItem()
            {
                Rank = (int)x.Rank,
                Suit = (int)x.Suit
            })
                );
            var userGetStatusGameViewItem = new UserEndGameResponseView()
            {
                Name  = userName,
                Cards = cards,
                State = new StateEndGameResponseView()
                {
                    State         = (int)game.State,
                    StateAsString = game.State.ToString()
                }
            };

            response.User = userGetStatusGameViewItem;

            return(response);
        }
Ejemplo n.º 2
0
        public async Task <EndGameResponseView> EndGame(string userId)
        {
            var game = await _gameRepository.GetActiveGameAsync(userId);

            var usersPoints = await _usersPointsRepository.GetPointsByGameIdAsync(game.Id);

            var botsPoints = (await _botsPointsRepository.GetPointsByGameIdAsync(game.Id)).ToList();

            await LastCardsDeal(game, usersPoints, botsPoints);

            int winningPoints = 0;

            botsPoints.ForEach(
                bp =>
            {
                if (bp.Points > winningPoints && bp.Points <= Constants.GameSettings.WinningNumber)
                {
                    winningPoints = bp.Points;
                }
            }
                );

            var resultUsersPoints = await _usersPointsRepository.GetPointsByGameIdAsync(game.Id);

            if (resultUsersPoints.Points >= winningPoints && resultUsersPoints.Points <= Constants.GameSettings.WinningNumber)
            {
                game.State = UserGameStateType.Win;
            }
            else
            {
                game.State = UserGameStateType.Lose;
            }

            await _gameRepository.UpdateAsync(game);

            EndGameResponseView response = await CreateEndGameResponseView(userId, game, usersPoints, botsPoints);

            return(response);
        }