Exemplo n.º 1
0
        public async Task <ResponseGetDeallerCardGameView> GetDeallerCard(RequestGetDeallerCardGameView requestGetDeallerCardGameView)
        {
            Game game = await _gameRepository.GetGameWithPlayerGames(requestGetDeallerCardGameView.GameId);

            Hand hand = await _handRepository.GetHandWithCardsByRoundAndPlayerId(requestGetDeallerCardGameView.RoundId, requestGetDeallerCardGameView.PlayerId);

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

                stringBuilder.AppendLine(string.Format("RoundId: {0}", requestGetDeallerCardGameView.RoundId));
                stringBuilder.AppendLine(string.Format("PlayerId: {0}", requestGetDeallerCardGameView.PlayerId));
                stringBuilder.AppendLine(string.Format("GameId: {0}", requestGetDeallerCardGameView.GameId));
                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);

            handCards.Add(card);

            hand.Summary += _gameUtility.GetCardValue(card.Face);

            if (handCards.Count == GameConstants.TWO_CARDS)
            {
                hand.VictoryType = _gameUtility.CheckTypeOfVictory(handCards);

                if (hand.VictoryType == VictoryType.GoldenPoint)
                {
                    hand.Summary = GameConstants.BLACKJACK;
                }
            }

            await _handRepository.Update(hand);

            var handCard = new HandCard();

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

            await _handCardRepository.Create(handCard);

            _deckProvider.SetDeckInMemoryCashe(game.Id, infinityDeck);

            var responseView = new ResponseGetDeallerCardGameView();

            responseView.HandId  = hand.Id;
            responseView.Card    = _mapper.Map <Card, CardGetDeallerCardGameViewItemItem>(card);
            responseView.Summary = hand.Summary;

            return(responseView);
        }
Exemplo n.º 2
0
        private async Task GiveOutTwoCards(int gameId, RequestGetPlayersTwoCardsGameView requestView)
        {
            Deck infinityDeck = await _deckProvider.GetAllDeckFromCache(gameId);

            var hands = new List <Hand>();

            var handCards = new List <HandCard>();

            for (int i = 0; i < requestView.Players.Count; i++)
            {
                Hand hand = await _handRepository.GetHandByRoundAndPlayerId(requestView.RoundId, requestView.Players[i]);

                if (hand == null)
                {
                    continue;
                }

                int summary = 0;

                var cards = new List <Card>();

                for (int y = 0; y < GameConstants.TWO_CARDS; y++)
                {
                    Card card = DeckExtension.GetCard(infinityDeck);

                    cards.Add(card);

                    var handCard = new HandCard();

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

                    handCards.Add(handCard);

                    summary += _gameUtility.GetCardValue(card.Face);
                }

                hand.Summary = summary;

                hand.VictoryType = _gameUtility.CheckTypeOfVictory(cards);

                if (hand.VictoryType == VictoryType.GoldenPoint)
                {
                    hand.Summary = GameConstants.BLACKJACK;
                }

                hands.Add(hand);
            }

            await _handRepository.UpdateMultiple(hands);

            await _handCardRepository.CreateMultiple(handCards);

            _deckProvider.SetDeckInMemoryCashe(gameId, infinityDeck);
        }
Exemplo n.º 3
0
        public async Task <GetWinnersGameView> GetWinners(int roundId)
        {
            Round round = await _roundRepository.GetRoundIncludeHandsAndPlayers(roundId);

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

                stringBuilder.AppendLine(string.Format("RoundId: {0}", roundId));
                stringBuilder.AppendLine(string.Format("Message: {0}", SolutionConstants.BUSINESS_LOGIC_GET_ITEM_EXCEPTION_MESSAGE));

                string message = stringBuilder.ToString();

                throw new BusinessLogicGetItemException(message);
            }

            List <Hand> allHands = round.Hands.ToList();

            Hand deallerHand = allHands.FirstOrDefault(item => item.Player.PlayerRole == PlayerRole.Dealler);

            List <Hand> playersHands = allHands.Where(item => item.Player.PlayerRole != PlayerRole.Dealler).ToList();

            bool checkGameIsFinished = await CheckGameIsFinished(round.GameId);

            foreach (Hand hand in playersHands)
            {
                SetSituation(hand, deallerHand);
            }

            await _handRepository.UpdateMultiple(playersHands);

            await CalculateCashFlow(deallerHand, playersHands);

            Deck infinityDeck = await _deckProvider.GetAllDeckFromCache(round.GameId);

            DeckExtension.AddUsedCardsToHangUp(infinityDeck);

            _deckProvider.SetDeckInMemoryCashe(round.GameId, infinityDeck);

            Hand playerHand = playersHands.FirstOrDefault(item => item.Player.PlayerRole == PlayerRole.Player);

            var responseView = new GetWinnersGameView();

            if (checkGameIsFinished)
            {
                List <GetWinnersGameViewItem> winnersViewItems = await DevideRewards(round.GameId);

                responseView.WinnersOfGame = winnersViewItems;
            }

            SetGetWinnersGameView(playerHand, deallerHand, responseView);

            return(responseView);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public async Task <Deck> GetAllDeckFromCache(int gameId)
        {
            Deck infinityDeck = null;

            if (_memoryCache.Contains(gameId.ToString()))
            {
                object deckObjeck = _memoryCache.Get(gameId.ToString());

                infinityDeck = deckObjeck as Deck;
            }

            if (!_memoryCache.Contains(gameId.ToString()))
            {
                infinityDeck = new Deck();

                var listCards = (await _cardRepository.GetAll()).ToList();

                DeckExtension.SetCards(infinityDeck, listCards);

                _memoryCache.Add(gameId.ToString(), infinityDeck, DateTime.Now.AddHours(GameConstants.HOURS_FOR_SAVE_DECK_IN_MEMORY_CACHE));
            }

            return(infinityDeck);
        }