Exemple #1
0
        private async Task CatchEncounter(EncounterResponse encounter, MapPokemon pokemon)
        {
            CatchPokemonResponse caughtPokemonResponse;
            var attemptCounter = 1;

            do
            {
                var probability  = encounter?.CaptureProbability?.CaptureProbability_?.FirstOrDefault();
                var bestPokeball = await GetBestBall(encounter);

                if (bestPokeball == MiscEnums.Item.ITEM_UNKNOWN)
                {
                    Logger.Write($"You don't own any Pokeballs :( - We missed a {pokemon.PokemonId} with CP {encounter?.WildPokemon?.PokemonData?.Cp}", LogLevel.Warning);
                    return;
                }
                var bestBerry = await GetBestBerry(encounter);

                var inventoryBerries = await _inventory.GetItems();

                var berries = inventoryBerries.FirstOrDefault(p => (ItemId)p.Item_ == bestBerry);
                if (bestBerry != ItemId.ItemUnknown && probability.HasValue && probability.Value < 0.35)
                {
                    await _client.UseCaptureItem(pokemon.EncounterId, bestBerry, pokemon.SpawnpointId);

                    berries.Count--;
                    Logger.Write($"{bestBerry} used, remaining: {berries.Count}", LogLevel.Berry);
                }

                var distance = LocationUtils.CalculateDistanceInMeters(_client.CurrentLat, _client.CurrentLng, pokemon.Latitude, pokemon.Longitude);
                caughtPokemonResponse = await _client.CatchPokemon(pokemon.EncounterId, pokemon.SpawnpointId, pokemon.Latitude, pokemon.Longitude, bestPokeball);

                if (caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchSuccess)
                {
                    foreach (var xp in caughtPokemonResponse.Scores.Xp)
                    {
                        _stats.AddExperience(xp);
                    }
                    _stats.IncreasePokemons();
                    var profile = await _client.GetProfile();

                    _stats.GetStardust(profile.Profile.Currency.ToArray()[1].Amount);
                }
                _stats.UpdateConsoleTitle(_client, _inventory);

                if (encounter?.CaptureProbability?.CaptureProbability_ != null)
                {
                    Func <MiscEnums.Item, string> returnRealBallName = a =>
                    {
                        switch (a)
                        {
                        case MiscEnums.Item.ITEM_POKE_BALL:
                            return("Poke");

                        case MiscEnums.Item.ITEM_GREAT_BALL:
                            return("Great");

                        case MiscEnums.Item.ITEM_ULTRA_BALL:
                            return("Ultra");

                        case MiscEnums.Item.ITEM_MASTER_BALL:
                            return("Master");

                        default:
                            return("Unknown");
                        }
                    };
                    var catchStatus = attemptCounter > 1
                        ? $"{caughtPokemonResponse.Status} Attempt #{attemptCounter}"
                        : $"{caughtPokemonResponse.Status}";

                    var receivedXp = caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchSuccess
                        ? $"and received XP {caughtPokemonResponse.Scores.Xp.Sum()}"
                        : $"";

                    Logger.Write($"({catchStatus}) | {pokemon.PokemonId} - Lvl {PokemonInfo.GetLevel(encounter?.WildPokemon?.PokemonData)} [CP {encounter?.WildPokemon?.PokemonData?.Cp}/{PokemonInfo.CalculateMaxCp(encounter?.WildPokemon?.PokemonData)} | IV: {PokemonInfo.CalculatePokemonPerfection(encounter?.WildPokemon?.PokemonData).ToString("0.00")}% perfect] | Chance: {(float)((int)(encounter?.CaptureProbability?.CaptureProbability_.First() * 100)) / 100} | {distance:0.##}m dist | with a {returnRealBallName(bestPokeball)}Ball {receivedXp}", LogLevel.Pokemon);
                }

                attemptCounter++;
            }while (caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchMissed || caughtPokemonResponse.Status == CatchPokemonResponse.Types.CatchStatus.CatchEscape);
        }