Example #1
0
        public async Task <ResponseGetExtraCardGameView> GetExtraCard(RequestGetExtraCardGameView requestGetExtraCardGameView)
        {
            Game game = await _gameRepository.GetGameWithPlayerGames(requestGetExtraCardGameView.GameId);

            Hand hand = await _handRepository.GetWithCards(requestGetExtraCardGameView.HandId);

            if (game == null || hand == null)
            {
                var stringBuilder = new StringBuilder();

                stringBuilder.AppendLine(string.Format("GameId: {0}", requestGetExtraCardGameView.GameId));
                stringBuilder.AppendLine(string.Format("HandId: {0}", requestGetExtraCardGameView.HandId));
                stringBuilder.AppendLine(string.Format("Message: {0}", SolutionConstants.BUSINESS_LOGIC_GET_ITEM_EXCEPTION_MESSAGE));

                string message = stringBuilder.ToString();

                throw new BusinessLogicGetItemException(message);
            }

            Deck infinityDeck = await _deckProvider.GetAllDeckFromCache(game.Id);

            List <Card> handCards = hand.HandCards.Select(item => item.Card).ToList();

            Card card = DeckExtension.GetCard(infinityDeck);

            _deckProvider.SetDeckInMemoryCashe(game.Id, infinityDeck);

            handCards.Add(card);

            hand.Summary = _gameUtility.CalculateCardsSumm(handCards);

            hand.VictoryType = _gameUtility.CheckTypeOfVictory(handCards);

            var handCard = new HandCard();

            handCard.CardId = card.Id;
            handCard.HandId = hand.Id;

            await _handCardRepository.Create(handCard);

            await _handRepository.Update(hand);

            ResponseGetExtraCardGameViewItem responseViewItem = _mapper.Map <Card, ResponseGetExtraCardGameViewItem>(card);

            var responseView = new ResponseGetExtraCardGameView();

            responseView.Card    = responseViewItem;
            responseView.Summary = hand.Summary;

            return(responseView);
        }
Example #2
0
        public async Task <ResponseGetDeallerExtraCardGameView> GetDeallerExtraCard(RequestGetDeallerExtraCardGameView requestGetDeallerExtraCardGameView)
        {
            Hand hand = await _handRepository.GetWithCards(requestGetDeallerExtraCardGameView.HandId);

            if (hand == null)
            {
                var stringBuilder = new StringBuilder();

                stringBuilder.AppendLine(string.Format("GameId: {0}", requestGetDeallerExtraCardGameView.GameId));
                stringBuilder.AppendLine(string.Format("HandId: {0}", requestGetDeallerExtraCardGameView.HandId));
                stringBuilder.AppendLine(string.Format("Message: {0}", SolutionConstants.BUSINESS_LOGIC_GET_ITEM_EXCEPTION_MESSAGE));

                string message = stringBuilder.ToString();

                throw new BusinessLogicGetItemException(message);
            }

            int playerPoints = hand.Summary;

            var extraCards = new List <ResponseGetDeallerExtraCardGameViewItem>();

            var responseView = new ResponseGetDeallerExtraCardGameView();

            if (playerPoints >= GameConstants.DESIRED_SUMM_CARDS_BOT_AND_DEALLER)
            {
                responseView.ExtraCards = extraCards;

                return(responseView);
            }

            RequestGetExtraCardGameView requestGetExtraCardView = _mapper.Map <RequestGetDeallerExtraCardGameView, RequestGetExtraCardGameView>(requestGetDeallerExtraCardGameView);

            for (int i = playerPoints; i < GameConstants.DESIRED_SUMM_CARDS_BOT_AND_DEALLER;)
            {
                ResponseGetExtraCardGameView extraCardView = await GetExtraCard(requestGetExtraCardView);

                ResponseGetDeallerExtraCardGameViewItem extraCardViewItem = _mapper.Map <ResponseGetExtraCardGameView, ResponseGetDeallerExtraCardGameViewItem>(extraCardView);

                extraCards.Add(extraCardViewItem);

                hand = await _handRepository.GetWithCards(hand.Id);

                i = _gameUtility.CalculateCardsSumm(hand.HandCards.Select(item => item.Card).ToList());
            }

            responseView.ExtraCards = extraCards;

            return(responseView);
        }
Example #3
0
        public async Task <ResponseGetBotExtraCardGameView> GetBotExtraCard(RequestGetBotExtraCardGameView requestGetBotExtraCardGameView)
        {
            Hand hand = await _handRepository.GetWithCards(requestGetBotExtraCardGameView.HandId);

            if (hand == null)
            {
                var stringBuilder = new StringBuilder();

                stringBuilder.AppendLine(string.Format("GameId: {0}", requestGetBotExtraCardGameView.GameId));
                stringBuilder.AppendLine(string.Format("HandId: {0}", requestGetBotExtraCardGameView.HandId));
                stringBuilder.AppendLine(string.Format("Message: {0}", SolutionConstants.BUSINESS_LOGIC_GET_ITEM_EXCEPTION_MESSAGE));

                string message = stringBuilder.ToString();

                throw new BusinessLogicGetItemException(message);
            }

            RequestGetExtraCardGameView requestExtraCardView = _mapper.Map <RequestGetBotExtraCardGameView, RequestGetExtraCardGameView>(requestGetBotExtraCardGameView);

            int chance = await CountChanceToGetNeededExtraCardForBot(requestGetBotExtraCardGameView.GameId);

            var extraCards = new List <ResponseGetBotExtraCardGameViewItem>();

            for (int i = hand.Summary; i < GameConstants.BLACKJACK;)
            {
                i = _gameUtility.CalculateCardsSumm(hand.HandCards.Select(item => item.Card).ToList());

                if (i <= GameConstants.REQUIRED_SUMM_CARDS_BOT)
                {
                    ResponseGetExtraCardGameView extraCardView = await GetExtraCard(requestExtraCardView);

                    ResponseGetBotExtraCardGameViewItem extraCardViewItem = _mapper.Map <ResponseGetExtraCardGameView, ResponseGetBotExtraCardGameViewItem>(extraCardView);

                    extraCards.Add(extraCardViewItem);

                    chance = await CountChanceToGetNeededExtraCardForBot(requestGetBotExtraCardGameView.GameId);

                    hand = await _handRepository.GetWithCards(hand.Id);

                    continue;
                }

                if (i <= GameConstants.DESIRED_SUMM_CARDS_BOT_AND_DEALLER && chance < 0)
                {
                    ResponseGetExtraCardGameView extraCardView = await GetExtraCard(requestExtraCardView);

                    ResponseGetBotExtraCardGameViewItem extraCardViewItem = _mapper.Map <ResponseGetExtraCardGameView, ResponseGetBotExtraCardGameViewItem>(extraCardView);

                    extraCards.Add(extraCardViewItem);

                    chance = await CountChanceToGetNeededExtraCardForBot(requestGetBotExtraCardGameView.GameId);

                    hand = await _handRepository.GetWithCards(hand.Id);

                    continue;
                }

                if (i <= GameConstants.DESIRED_SUMM_CARDS_BOT_AND_DEALLER && chance >= 0)
                {
                    var random = new Random();

                    int randValue = random.Next(GameConstants.DECISION_TO_TAKE_CARD, GameConstants.DECISION_NOT_TO_TAKE_CARD);

                    if (randValue.Equals(GameConstants.DECISION_TO_TAKE_CARD))
                    {
                        ResponseGetExtraCardGameView extraCardView = await GetExtraCard(requestExtraCardView);

                        ResponseGetBotExtraCardGameViewItem extraCardViewItem = _mapper.Map <ResponseGetExtraCardGameView, ResponseGetBotExtraCardGameViewItem>(extraCardView);

                        extraCards.Add(extraCardViewItem);

                        chance = await CountChanceToGetNeededExtraCardForBot(requestGetBotExtraCardGameView.GameId);

                        hand = await _handRepository.GetWithCards(hand.Id);

                        continue;
                    }

                    if (randValue.Equals(GameConstants.DECISION_NOT_TO_TAKE_CARD))
                    {
                        break;
                    }
                }

                break;
            }

            var responseView = new ResponseGetBotExtraCardGameView();

            responseView.ExtraCards = extraCards;

            return(responseView);
        }
Example #4
0
        public async Task <ResponseGetExtraCardGameView> GetExtraCard([FromBody] RequestGetExtraCardGameView requestGetExtraCardView)
        {
            ResponseGetExtraCardGameView responseGetExtraCardView = await _gameLogicService.GetExtraCard(requestGetExtraCardView);

            return(responseGetExtraCardView);
        }