public static async Task Execute(ISession session, ulong pokemonId, CancellationToken cancellationToken, bool toMax = false)
        {
            try
            {
                if (!await CheckBotStateTask.Execute(session, cancellationToken))
                {
                    return;
                }
            }
            catch (TaskCanceledException)
            {
                //ignore
            }
            catch (Exception ex)
            {
                Logger.Write($"[MANUAL TASAK FAIL] ERROR: {ex.Message}");
            }
            var prevState = session.State;

            session.State = BotState.LevelPoke;

            var all = await session.Inventory.GetPokemons();

            var pokemon = all.FirstOrDefault(p => p.Id == pokemonId);

            if (pokemon == null)
            {
                session.State = prevState;
                session.EventDispatcher.Send(new PokemonActionDoneEvent {
                    Uid = pokemonId
                });
                return;
            }


            if (!string.IsNullOrEmpty(pokemon.DeployedFortId))
            {
                session.EventDispatcher.Send(new WarnEvent
                {
                    Message = session.Translation.GetTranslation(TranslationString.PokeInGym, string.IsNullOrEmpty(pokemon.Nickname) ? pokemon.PokemonId.ToString() : pokemon.Nickname)
                });
                session.State = prevState;
                session.EventDispatcher.Send(new PokemonActionDoneEvent {
                    Uid = pokemonId
                });
                return;
            }


            if (pokemon.GetLevel() >= session.Runtime.CurrentLevel + 1.5)
            {
                session.State = prevState;
                session.EventDispatcher.Send(new PokemonActionDoneEvent {
                    Uid = pokemonId
                });
                return;
            }

            bool success;
            var  max = false;
            UpgradePokemonResponse latestSuccessResponse = null;

            do
            {
                success = false;
                var upgradeResult = await session.Inventory.UpgradePokemon(pokemon.Id);

                await DelayingUtils.Delay(session.LogicSettings.DelayBetweenPlayerActions, 2000);

                switch (upgradeResult.Result)
                {
                case UpgradePokemonResponse.Types.Result.Success:
                    success = true;
                    latestSuccessResponse = upgradeResult;
                    session.EventDispatcher.Send(new NoticeEvent
                    {
                        Message =
                            session.Translation.GetTranslation(TranslationString.PokemonUpgradeSuccess,
                                                               session.Translation.GetPokemonName(upgradeResult.UpgradedPokemon.PokemonId),
                                                               upgradeResult.UpgradedPokemon.Cp)
                    });
                    if (upgradeResult.UpgradedPokemon.GetLevel() >= session.Runtime.CurrentLevel + 1.5)
                    {
                        max = true;
                    }
                    break;

                case UpgradePokemonResponse.Types.Result.ErrorInsufficientResources:
                    session.EventDispatcher.Send(new NoticeEvent
                    {
                        Message = session.Translation.GetTranslation(TranslationString.PokemonUpgradeFailed)
                    });
                    break;

                case UpgradePokemonResponse.Types.Result.ErrorUpgradeNotAvailable:
                    session.EventDispatcher.Send(new NoticeEvent
                    {
                        Message =
                            session.Translation.GetTranslation(TranslationString.PokemonUpgradeUnavailable,
                                                               session.Translation.GetPokemonName(pokemon.PokemonId), pokemon.Cp,
                                                               PokemonInfo.CalculateMaxCp(pokemon))
                    });
                    break;

                default:
                    session.EventDispatcher.Send(new NoticeEvent
                    {
                        Message =
                            session.Translation.GetTranslation(TranslationString.PokemonUpgradeFailedError,
                                                               session.Translation.GetPokemonName(pokemon.PokemonId))
                    });
                    break;
                }
            } while (success && toMax && !cancellationToken.IsCancellationRequested && !max);

            if (latestSuccessResponse != null && !cancellationToken.IsCancellationRequested)
            {
                var mon             = latestSuccessResponse.UpgradedPokemon;
                var pokemonFamilies = await session.Inventory.GetPokemonFamilies();

                var pokemonSettings = (await session.Inventory.GetPokemonSettings()).ToList();
                var setting         = pokemonSettings.Single(q => q.PokemonId == pokemon.PokemonId);
                var family          = pokemonFamilies.First(q => q.FamilyId == setting.FamilyId);
                session.EventDispatcher.Send(new PokemonStatsChangedEvent
                {
                    Name = !string.IsNullOrEmpty(mon.Nickname)
                        ? mon.Nickname
                        : session.Translation.GetPokemonName(mon.PokemonId),
                    Uid        = pokemonId,
                    Id         = mon.PokemonId,
                    Family     = family.FamilyId,
                    Candy      = family.Candy_,
                    Cp         = mon.Cp,
                    MaxCp      = (int)PokemonInfo.GetMaxCpAtTrainerLevel(mon, session.Runtime.CurrentLevel),
                    Iv         = mon.CalculatePokemonPerfection(),
                    Favourite  = mon.Favorite == 1,
                    Weight     = mon.WeightKg,
                    Cpm        = mon.CpMultiplier + mon.AdditionalCpMultiplier,
                    Level      = mon.GetLevel(),
                    IvDef      = mon.IndividualDefense,
                    IvAtk      = mon.IndividualAttack,
                    Stamina    = mon.Stamina,
                    StaminaMax = mon.StaminaMax
                });
                session.EventDispatcher.Send(new PokemonActionDoneEvent
                {
                    Uid = pokemonId
                });
            }
            session.State = prevState;
        }
Beispiel #2
0
        public static async Task Execute(ISession session, CancellationToken cancellationToken, string fortId)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                var lureCount = await session.Inventory.GetItemAmountByType(ItemId.ItemTroyDisk);

                if (lureCount <= 0)
                {
                    session.EventDispatcher.Send(new WarnEvent
                    {
                        Message = "No Lure Modules"
                    });
                    return;
                }

                var pokestopList = await GetPokeStops(session);

                if (session.LogicSettings.UseEggIncubators && !session.EggWalker.Inited)
                {
                    await session.EggWalker.InitEggWalker(cancellationToken);
                }

                if (pokestopList.Count <= 0)
                {
                    return;
                }

                var targetPs = pokestopList.FirstOrDefault(x => x.Id == fortId);

                if (targetPs == null)
                {
                    session.EventDispatcher.Send(new WarnEvent
                    {
                        Message = "Pokestop not found"
                    });
                    return;
                }

                if (targetPs.LureInfo != null)
                {
                    session.EventDispatcher.Send(new WarnEvent
                    {
                        Message = "Pokestop lured already!"
                    });
                    return;
                }

                var distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                       session.Client.CurrentLongitude, targetPs.Latitude, targetPs.Longitude);
                var fortInfo = await session.Client.Fort.GetFort(targetPs.Id, targetPs.Latitude, targetPs.Longitude);

                session.StartForceMove(targetPs.Latitude, targetPs.Longitude);
                var loops = 0;
                while (distance > 15 && loops++ < 500)
                {
                    distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                       session.Client.CurrentLongitude, targetPs.Latitude, targetPs.Longitude);
                    await Task.Delay(1500, cancellationToken);
                }
                if (distance > 15)
                {
                    return;
                }


                var modifyReponse = await session.Client.Fort.AddFortModifier(fortInfo.FortId, ItemId.ItemTroyDisk);

                if (modifyReponse.Result == AddFortModifierResponse.Types.Result.SUCCESS)
                {
                    session.EventDispatcher.Send(new FortLureStartedEvent
                    {
                        Id            = fortId,
                        Name          = fortInfo.Name,
                        Dist          = distance,
                        LureCountLeft = lureCount - 1
                    });
                    session.EventDispatcher.Send(new ItemLostEvent
                    {
                        Id    = ItemId.ItemTroyDisk,
                        Count = 1
                    });
                }
                else
                {
                    session.EventDispatcher.Send(new WarnEvent
                    {
                        Message = $"Error while luring a pokestop, server response: {modifyReponse.Result}"
                    });
                }
            }
            catch (Exception ex)
            {
                session.EventDispatcher.Send(new WarnEvent
                {
                    Message = "Using LureMod failed, see log file for the detail"
                });
                Logger.Write($"[UseLureMod] Failed with error: {ex.Message}");
            }
        }