Ejemplo n.º 1
0
        public async Task <ResponseGameViewModel> DealCards(int gameId)
        {
            var result = await GameResponse(gameId);

            var deckFromCache    = _deckProvider.Get();
            var handCardsToCache = new List <HandCards>();
            var cardToUser       = new List <Card>();
            var move             = new Move();
            var moves            = new List <Move>();
            var game             = await _gameRepository.Get(gameId);

            var userGames = await _userGamesRepository.Get(game);

            var users = await _userRepository.Get(userGames);

            var dealer = users.FirstOrDefault(x => x.UserRole == UserRole.Dealer);
            var rounds = await _roundRepository.Get(game);

            var userRounds = await _userRoundRepository.Get(users);

            for (int i = 0; i < users.Count(); i++)
            {
                if (users.ElementAt(i).UserRole != UserRole.Dealer && users.ElementAt(i).UserRole != UserRole.None)
                {
                    cardToUser = deckFromCache.Cards.Skip(deckFromCache.Cards.Count - 2).ToList();
                    handCardsToCache.Add(new HandCards {
                        Cards = cardToUser, User = users.ElementAt(i)
                    });
                    result.Users.FirstOrDefault(x => x.Nickname == users.ElementAt(i).Nickname).Cards = _mapper.Map <List <ResponseCardViewModel> >(cardToUser);
                    deckFromCache.Cards.RemoveRange(deckFromCache.Cards.Count - 2, 2);
                    move = new Move {
                        RoundId = rounds.Last().Id, UserId = users.ElementAt(i).Id, CardId = cardToUser[0].Id
                    };
                    moves.Add(move);
                    move = new Move {
                        RoundId = rounds.Last().Id, UserId = users.ElementAt(i).Id, CardId = cardToUser[1].Id
                    };                                                                                                         //fix this shit
                    moves.Add(move);
                }
                if (await IsBlackJack(moves) && users.ElementAt(i).UserRole != UserRole.Dealer)
                {
                    userRounds.FirstOrDefault(x => x.UserId == users.ElementAt(i).Id).IsWin = true;
                }
            }

            await _moveRepository.CreateRange(moves);

            moves.RemoveRange(0, moves.Count);
            cardToUser = deckFromCache.Cards.Skip(deckFromCache.Cards.Count - 2).ToList();
            handCardsToCache.Add(new HandCards {
                Cards = cardToUser, User = dealer
            });
            result.Users.FirstOrDefault(x => x.UserRole == UserRole.Dealer).Cards = _mapper.Map <List <ResponseCardViewModel> >(cardToUser);
            deckFromCache.Cards.RemoveRange(deckFromCache.Cards.Count - 2, 2);
            move = new Move {
                RoundId = rounds.Last().Id, UserId = users.FirstOrDefault(x => x.UserRole == UserRole.Dealer).Id, CardId = cardToUser[0].Id
            };
            await _moveRepository.Create(move);

            _deckProvider.Update(new Deck {
                Cards = deckFromCache.Cards, DiscardPile = new List <Card>()
            });
            _handCardsProvider.AddRange(handCardsToCache);

            if (await IsBlackJack(moves))
            {
                result.Rounds.Last().IsOver = true;
                await SetLoserMoves(userRounds);
            }

            return(result);
        }
        public async Task SeedsAsync(int initialIndex)
        {
            var typeLists = _typeRepository.GetAll();
            int moveIndex = initialIndex;

            try {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_apiUrl);
                    for (int i = initialIndex; i <= 728; i++)
                    {
                        moveIndex = i;
                        HttpResponseMessage results;
                        bool responseSuccess = false;
                        int  errorCount      = 0;
                        do
                        {
                            var responseTask = client.GetAsync(string.Concat("move/", i));
                            responseTask.Wait();
                            results         = responseTask.Result;
                            responseSuccess = results.IsSuccessStatusCode;
                            if (!responseSuccess)
                            {
                                errorCount++;
                            }
                            if (errorCount == 3)
                            {
                                throw new PokeApiException();
                            }
                        } while (responseSuccess == false);

                        string apiResponse = await results.Content.ReadAsStringAsync();

                        var json               = JObject.Parse(apiResponse);
                        var name               = json["names"][2]["name"].ToString();
                        var basePowerString    = json["power"].ToString();
                        var moveTypeString     = json["type"]["name"].ToString();
                        var moveCategoryString = json["damage_class"]["name"].ToString();

                        var basePower    = basePowerString.ToDecimal();
                        var moveType     = typeLists.FirstOrDefault(x => x.Name.ToLower() == moveTypeString);
                        var moveCategory = (MoveCategoryEnum)typeof(MoveCategoryEnum).GetEnumByDescription(moveCategoryString);

                        if (moveCategory == MoveCategoryEnum.Status)
                        {
                            continue;
                        }

                        var move = new Moves()
                        {
                            Name          = name,
                            Base_Power    = basePower,
                            Move_Category = moveCategory,
                            Id_Type       = moveType.Id
                        };

                        _moveRepository.Create(move);
                    }
                }
            }
            catch (Exception ex) {
                using (var logger = LoggerDomain.GetLog(_connectionString))
                {
                    logger.Error(string.Format("Error at pokemonSpecies {0} - Exception Message: ", moveIndex, ex.Message));
                    throw ex;
                }
            }
        }