示例#1
0
        private async Task <string> CheckingCardsOfBots(IEnumerable <Bot> bots, Game game)
        {
            var    amountsCardsOfAllBots = new Dictionary <string, int>();
            string nameOfWonBot          = "";

            var allSteps = await _database.BotSteps.GetAll();

            var createdSteps = new List <BotStep>();

            foreach (var bot in bots)
            {
                var botSteps = allSteps.Select(step => step)
                               .Where(step => step.BotId == bot.Id)
                               .ToList();

                var botRanks = new List <RankType>();
                botRanks = botSteps.Select(step => step.Rank).ToList();

                var totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                while (totalValueOfBotCards <= MidleDraw)
                {
                    var botStep = new BotStep()
                    {
                        Game   = game,
                        GameId = game.Id,
                        Bot    = bot,
                        BotId  = bot.Id,
                        Rank   = (RankType)_random.Next(1, 13),
                        Suite  = (SuiteType)_random.Next(1, 4)
                    };
                    botRanks.Add(botStep.Rank);
                    createdSteps.Add(botStep);
                    totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                }

                totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                amountsCardsOfAllBots.Add(bot.Name, totalValueOfBotCards);
            }
            await _database.BotSteps.Create(createdSteps);

            var maxAmount = 0;

            foreach (var item in amountsCardsOfAllBots)
            {
                if (item.Value == Draw)
                {
                    nameOfWonBot = item.Key;
                }
                if (item.Value < Draw && item.Value > maxAmount)
                {
                    maxAmount    = item.Value;
                    nameOfWonBot = item.Key;
                }
            }
            var result = (nameOfWonBot == "") ? nameOfWonBot = "NOBODY" : nameOfWonBot;

            return(result);
        }
示例#2
0
        private BotStep CreateBotStep(Bot bot, Game game)
        {
            var result = new BotStep()
            {
                Bot    = bot,
                BotId  = bot.Id,
                GameId = game.Id,
                Game   = game,
                Rank   = (RankType)_random.Next(1, 13),
                Suite  = (SuiteType)_random.Next(1, 4)
            };

            return(result);
        }
示例#3
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);
                }
            });
        }
示例#4
0
        private void DealLast(Game game, List <Card> deck, List <Card> cardsForRemove, List <Bot> bots, List <BotStep> stepBots, List <BotInGame> botInGames)
        {
            foreach (var botInGame in botInGames)
            {
                while (IsNeedCard(botInGame))
                {
                    var currentCard = deck.FirstOrDefault();
                    var stepBot     = new BotStep
                    {
                        BotId  = bots.FirstOrDefault(x => x.Id == botInGame.BotId).Id,
                        GameId = game.Id,
                        Rank   = currentCard.Rank,
                        Suit   = currentCard.Suit
                    };
                    stepBots.Add(stepBot);

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

                    cardsForRemove.Add(currentCard);
                    deck.Remove(currentCard);
                }
            }
        }
示例#5
0
        public async Task Stand(string playerId)
        {
            if (string.IsNullOrEmpty(playerId))
            {
                throw new CustomServiceException("Player cannot be null");
            }

            var validPlayerId   = Guid.Empty;
            var isValidPlayerId = Guid.TryParse(playerId, out validPlayerId);

            if (!isValidPlayerId)
            {
                throw new CustomServiceException("Player Id is not valid");
            }

            var player = await _database.Players.Get(validPlayerId);

            if (player == null)
            {
                throw new CustomServiceException("Player does not exist");
            }

            var game = await _database.Games.GetActiveByPlayerId(playerId);

            if (game == null)
            {
                throw new CustomServiceException("Game does not exist");
            }

            var bots = await _database.Bots.GetAllBotsByGameId(game.Id);

            if (game.GameState != GameStateType.Unknown)
            {
                return;
            }

            var allBotSteps = await _database.BotSteps.GetAll();

            var playerSteps = await _database.PlayerSteps.GetAllByPlayerIdAndGameId(playerId, game.Id);

            foreach (var bot in bots)
            {
                var botSteps = allBotSteps.Select(step => step)
                               .Where(step => step.BotId == bot.Id)
                               .ToList();

                var playerRanks = new List <RankType>();

                playerRanks = playerSteps.Select(step => step.Rank).ToList();

                var botRanks = new List <RankType>();

                botRanks = botSteps.Select(step => step.Rank).ToList();

                var createdBotSteps      = new List <BotStep>();
                var totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                while (totalValueOfBotCards <= MidleDraw)
                {
                    var botStep = new BotStep()
                    {
                        Game   = game,
                        GameId = game.Id,
                        Bot    = bot,
                        BotId  = bot.Id,
                        Rank   = (RankType)_random.Next(1, 13),
                        Suite  = (SuiteType)_random.Next(1, 4)
                    };
                    botRanks.Add(botStep.Rank);
                    createdBotSteps.Add(botStep);
                    totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                }
                await _database.BotSteps.Create(createdBotSteps);

                var isTotalValuePlayerMoreThanBot = _ranksHelper.TotalValue(playerRanks) > _ranksHelper.TotalValue(botRanks);
                totalValueOfBotCards = _ranksHelper.TotalValue(botRanks);
                var totalValueOfPlayerCards = _ranksHelper.TotalValue(playerRanks);

                if (totalValueOfBotCards > Draw || isTotalValuePlayerMoreThanBot)
                {
                    player.Balance += player.Bet;
                    game.WonName    = player.UserName;
                    game.GameState  = GameStateType.PlayerWon;
                }
                else if (totalValueOfBotCards == totalValueOfPlayerCards)
                {
                    game.GameState = GameStateType.Draw;
                    game.WonName   = player.UserName;
                }
                else
                {
                    player.Balance -= player.Bet;
                    game.WonName    = player.UserName;
                    game.GameState  = GameStateType.BotWon;
                }
            }

            await _database.Games.Update(game);

            await _database.Players.Update(player);

            await _database.Bots.Update(bots);
        }