private static async Task FollowTheYellowbrickroad(ISession session, CancellationToken cancellationToken, CustomRoute route,
                                                           Navigation navi, EggWalker eggWalker, string prevRouteName)
        {
            var initialize = true;
            //Find closest point of route and it's index!
            var closestPoint = await CheckClosestAndMove(session, cancellationToken, route);

            long nextMaintenceStamp = 0;
            var  sameRoute          = true;

            while (sameRoute)
            {
                foreach (var wp in route.RoutePoints)
                {
                    if (session.ForceMoveTo != null)
                    {
                        break;
                    }

                    if (initialize)
                    {
                        if (wp != closestPoint)
                        {
                            continue;
                        }
                        initialize = false;
                    }
                    if (prevRouteName != session.LogicSettings.CustomRouteName)
                    {
                        sameRoute = false;
                        session.EventDispatcher.Send(new NoticeEvent()
                        {
                            Message = $"Route switched from {prevRouteName} to {session.LogicSettings.CustomRouteName}!"
                        });
                        break;
                    }

                    session.State = BotState.Walk;

                    var distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                           session.Client.CurrentLongitude, wp.Latitude, wp.Longitude);

                    await navi.HumanPathWalking(
                        session,
                        wp,
                        NextMoveSpeed(session),
                        async() =>
                    {
                        await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                        //Catch Incense Pokemon
                        await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                        return(true);
                    },
                        async() =>
                    {
                        await UseNearbyPokestopsTask.Execute(session, cancellationToken, true);
                        await PokeNearbyGym.Execute(session, cancellationToken);
                        return(true);
                    },
                        cancellationToken
                        );

                    session.State = BotState.Idle;
                    await eggWalker.ApplyDistance(distance, cancellationToken);

                    if (nextMaintenceStamp >= DateTime.UtcNow.ToUnixTime() && session.Runtime.StopsHit < 100)
                    {
                        continue;
                    }
                    await MaintenanceTask.Execute(session, cancellationToken);

                    nextMaintenceStamp = DateTime.UtcNow.AddMinutes(3).ToUnixTime();
                }
                if (initialize)
                {
                    initialize = false;
                }

                if (session.ForceMoveTo != null)
                {
                    await ForceMoveTask.Execute(session, cancellationToken);

                    closestPoint = await CheckClosestAndMove(session, cancellationToken, route);

                    initialize = true;
                }
            }
        }
        public static async Task Teleport(ISession session, CancellationToken cancellationToken, Random random)
        {
            bool ShownSoftBanMessage = false;
            int  stopsToHit          = 20; //We should return to the main loop after some point, might as well limit this.
            //Not sure where else we could put this? Configs maybe if we incorporate
            //deciding how many pokestops in a row we want to hit before doing things like recycling?
            //might increase xp/hr not stopping every 5 stops. - Pocket


            //TODO: run through this with a fine-tooth comb and optimize it.
            var pokestopList = await GetPokeStops(session);

            session.EventDispatcher.Send(new PokeStopListEvent {
                Forts = pokestopList.Select(x => x.BaseFortData).ToList()
            });

            for (int stopsHit = 0; stopsHit < stopsToHit; stopsHit++)
            {
                session.Runtime.BreakOutOfPathing = false;
                if (pokestopList.Count > 0)
                {
                    //start at 0 ends with 19 = 20 for the leechers{
                    cancellationToken.ThrowIfCancellationRequested();

                    var distanceFromStart = LocationUtils.CalculateDistanceInMeters(
                        session.Client.InitialLatitude, session.Client.InitialLongitude,
                        session.Client.CurrentLatitude, session.Client.CurrentLongitude);

                    // Edge case for when the client somehow ends up outside the defined radius
                    if (session.LogicSettings.MaxTravelDistanceInMeters != 0 &&
                        distanceFromStart > session.LogicSettings.MaxTravelDistanceInMeters)
                    {
                        session.EventDispatcher.Send(new WarnEvent
                        {
                            Message = session.Translation.GetTranslation(TranslationString.FarmPokestopsOutsideRadius, distanceFromStart)
                        });
                        await Task.Delay(1000, cancellationToken);

                        await session.Navigation.Move(
                            new GeoCoordinate(session.Settings.DefaultLatitude, session.Settings.DefaultLongitude),
                            session.LogicSettings.WalkingSpeedMin, session.LogicSettings.WalkingSpeedMax, null, null, cancellationToken, session);
                    }
                    if (session.ForceMoveJustDone)
                    {
                        session.ForceMoveJustDone = false;
                    }
                    if (session.ForceMoveTo != null)
                    {
                        await ForceMoveTask.Execute(session, cancellationToken);

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

                    if (pokestopList.Count <= 0)
                    {
                        if (!pokestopList.Any())
                        {
                            await CheckChallengeDoneTask.Execute(session, cancellationToken);

                            await CheckChallengeTask.Execute(session, cancellationToken);
                        }
                        session.EventDispatcher.Send(new WarnEvent
                        {
                            Message = session.Translation.GetTranslation(TranslationString.FarmPokestopsNoUsableFound)
                        });
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    await ActionQueueTask.Execute(session, cancellationToken);

                    //resort
                    pokestopList =
                        pokestopList.OrderBy(
                            i =>
                            LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                    session.Client.CurrentLongitude, i.Latitude, i.Longitude)).ToList();

                    if (session.LogicSettings.UsePokeStopLuckyNumber)
                    {
                        if (pokestopList.Count >= session.LogicSettings.PokestopSkipLuckyNumberMinUse)
                        {
                            int rng = random.Next(session.LogicSettings.PokestopSkipLuckyMin, session.LogicSettings.PokestopSkipLuckyMax);
#if DEBUG
                            Logger.Write("Skip Pokestop RNG: " + rng + " against " + session.LogicSettings.PokestopSkipLuckyNumber, LogLevel.Debug);
#endif
                            if (rng == session.LogicSettings.PokestopSkipLuckyNumber)
                            {
#if DEBUG
                                Logger.Write("Skipping Pokestop due to the rng god's will.", LogLevel.Debug);
#endif
                                pokestopList.RemoveAt(0);
                            }
                        }
                    }


                    var pokeStop = pokestopList[0];
                    pokestopList.RemoveAt(0);
                    session.Runtime.TargetStopID = pokeStop.Id;
                    var distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                           session.Client.CurrentLongitude, pokeStop.Latitude, pokeStop.Longitude);
                    var fortInfo = await session.Client.Fort.GetFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);

                    session.EventDispatcher.Send(new FortTargetEvent {
                        Id = fortInfo.FortId, Name = fortInfo.Name, Distance = distance, Latitude = fortInfo.Latitude, Longitude = fortInfo.Longitude, Description = fortInfo.Description, url = fortInfo.ImageUrls[0]
                    });
                    if (session.LogicSettings.Teleport)
                    {
                        await session.Client.Player.UpdatePlayerLocation(fortInfo.Latitude, fortInfo.Longitude,
                                                                         session.Client.Settings.DefaultAltitude);
                    }

                    else
                    {
                        await session.Navigation.Move(new GeoCoordinate(pokeStop.Latitude, pokeStop.Longitude),
                                                      session.LogicSettings.WalkingSpeedMin, session.LogicSettings.WalkingSpeedMax,
                                                      async() =>
                        {
                            if (session.LogicSettings.CatchWildPokemon)
                            {
                                // Catch normal map Pokemon
                                await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                                //Catch Incense Pokemon remove this for time contraints
                                //await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                            }
                            return(true);
                        },
                                                      async() =>
                        {
                            await UseNearbyPokestopsTask.Execute(session, cancellationToken);
                            return(true);
                        },

                                                      cancellationToken, session);
                    }

                    if (!session.LogicSettings.LootPokestops)
                    {
                        session.MapCache.UsedPokestop(pokeStop, session);
                        continue;
                    }

                    if (!session.ForceMoveJustDone)
                    {
                        var       timesZeroXPawarded = 0;
                        var       fortTry            = 0;  //Current check
                        const int retryNumber        = 50; //How many times it needs to check to clear softban
                        const int zeroCheck          = 5;  //How many times it checks fort before it thinks it's softban
                        if (session.Runtime.BreakOutOfPathing)
                        {
                            continue;
                        }
                        do
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            var fortSearch = await session.Client.Fort.SearchFort(pokeStop.Id, pokeStop.Latitude, pokeStop.Longitude);

                            if (fortSearch.ExperienceAwarded > 0 && timesZeroXPawarded > 0)
                            {
                                timesZeroXPawarded = 0;
                            }
                            if (fortSearch.ExperienceAwarded == 0)
                            {
                                timesZeroXPawarded++;

                                if (timesZeroXPawarded > zeroCheck)
                                {
                                    if ((int)fortSearch.CooldownCompleteTimestampMs != 0)
                                    {
                                        break;
                                        // Check if successfully looted, if so program can continue as this was "false alarm".
                                    }

                                    fortTry += 1;

                                    if (!ShownSoftBanMessage)
                                    {
                                        session.EventDispatcher.Send(new FortFailedEvent
                                        {
                                            Name = fortInfo.Name,
                                            Try  = fortTry,
                                            Max  = retryNumber - zeroCheck
                                        });
                                        ShownSoftBanMessage = true;
                                    }
                                    await Task.Delay(session.LogicSettings.DelaySoftbanRetry, cancellationToken);
                                }
                            }
                            else
                            {
                                session.EventDispatcher.Send(new FortUsedEvent
                                {
                                    Id            = pokeStop.Id,
                                    Name          = fortInfo.Name,
                                    Exp           = fortSearch.ExperienceAwarded,
                                    Gems          = fortSearch.GemsAwarded,
                                    Items         = StringUtils.GetSummedFriendlyNameOfItemAwardList(fortSearch.ItemsAwarded),
                                    Latitude      = pokeStop.Latitude,
                                    Longitude     = pokeStop.Longitude,
                                    InventoryFull = fortSearch.Result == FortSearchResponse.Types.Result.InventoryFull,
                                    Description   = fortInfo.Description,
                                    url           = fortInfo.ImageUrls[0]
                                });

                                if (fortSearch.PokemonDataEgg != null)
                                {
                                    session.EventDispatcher.Send(new EggFoundEvent
                                    {
                                        Distance     = fortSearch.PokemonDataEgg.EggKmWalkedTarget,
                                        PokeStopName = fortInfo.Name
                                    });
                                }
                                if (session.Runtime.PokeBallsToCollect > 0)
                                {
                                    session.Runtime.PokeBallsToCollect -= fortSearch.ItemsAwarded.ToItemList()
                                                                          .Where(x => x.Item1 == ItemId.ItemPokeBall ||
                                                                                 x.Item1 == ItemId.ItemGreatBall || x.Item1 == ItemId.ItemUltraBall ||
                                                                                 x.Item1 == ItemId.ItemMasterBall).Sum(x => x.Item2);
                                }
                                session.MapCache.UsedPokestop(pokeStop, session);
                                session.EventDispatcher.Send(new InventoryNewItemsEvent
                                {
                                    Items = fortSearch.ItemsAwarded.ToItemList()
                                });
                                break; //Continue with program as loot was succesfull.
                            }
                        } while (fortTry < retryNumber - zeroCheck);
                        //Stop trying if softban is cleaned earlier or if 40 times fort looting failed.

                        ShownSoftBanMessage = false;
                        await Task.Delay(session.LogicSettings.DelayPokestop, cancellationToken);


                        //Catch Lure Pokemon

                        if (session.LogicSettings.CatchWildPokemon)
                        {
                            if (pokeStop.LureInfo != null)
                            {
                                await CatchLurePokemonsTask.Execute(session, pokeStop.BaseFortData, cancellationToken);
                            }
                            // Catch normal map Pokemon
                            if (session.LogicSettings.Teleport)
                            {
                                await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                            }
                            //Catch Incense Pokemon
                            await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                        }
                        await ActionQueueTask.Execute(session, cancellationToken);

                        await MaintenanceTask.Execute(session, cancellationToken);
                    }
                    if (session.LogicSettings.SnipeAtPokestops || session.LogicSettings.UseSnipeLocationServer)
                    {
                        await SnipePokemonTask.Execute(session, cancellationToken);
                    }
                }
            }
        }
        public static async Task Execute(ISession session, CancellationToken cancellationToken)
        {
            var route     = session.LogicSettings.CustomRoute;
            var eggWalker = new EggWalker(1000, session);

            if (route == null || route.RoutePoints.Count < 2)
            {
                session.EventDispatcher.Send(new WarnEvent
                {
                    Message = "No proper route loaded"
                });
                return;
            }

            var navi = new Navigation(session.Client);

            navi.UpdatePositionEvent += (lat, lng, alt) =>
            {
                session.EventDispatcher.Send(new UpdatePositionEvent {
                    Latitude = lat, Longitude = lng, Altitude = alt
                });
            };
            if (LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude, session.Client.CurrentLongitude,
                                                        route.RoutePoints[0].Latitude, route.RoutePoints[0].Longitude) > 10)
            {
                session.State = BotState.Walk;
                await session.Navigation.Move(route.RoutePoints[0],
                                              session.LogicSettings.WalkingSpeedMin, session.LogicSettings.WalkingSpeedMax,
                                              async() =>
                {
                    if (session.LogicSettings.CatchWildPokemon)
                    {
                        // Catch normal map Pokemon
                        await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                        //Catch Incense Pokemon
                        await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                    }
                    return(true);
                },
                                              async() =>
                {
                    await UseNearbyPokestopsTask.Execute(session, cancellationToken, true);
                    return(true);
                }, cancellationToken, session);

                session.State = BotState.Idle;
            }

            var nextPath = route.RoutePoints.Select(item => Tuple.Create(item.Latitude, item.Longitude)).ToList();

            session.EventDispatcher.Send(new NextRouteEvent
            {
                Coords = nextPath
            });

            long nextMaintenceStamp = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                foreach (var wp in route.RoutePoints)
                {
                    session.State = BotState.Walk;

                    var distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                           session.Client.CurrentLongitude, wp.Latitude, wp.Longitude);

                    await navi.HumanPathWalking(
                        session,
                        wp,
                        NextMoveSpeed(session),
                        async() =>
                    {
                        await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                        //Catch Incense Pokemon
                        await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                        return(true);
                    },
                        async() =>
                    {
                        await UseNearbyPokestopsTask.Execute(session, cancellationToken, true);
                        return(true);
                    },
                        cancellationToken
                        );

                    session.State = BotState.Idle;
                    await eggWalker.ApplyDistance(distance, cancellationToken);

                    if (nextMaintenceStamp >= DateTime.UtcNow.ToUnixTime())
                    {
                        continue;
                    }
                    await MaintenanceTask.Execute(session, cancellationToken);

                    nextMaintenceStamp = DateTime.UtcNow.AddMinutes(3).ToUnixTime();
                }
            }
        }
Example #4
0
        public static async Task Execute(ISession session, CancellationToken cancellationToken)
        {
            var route     = session.LogicSettings.CustomRoute;
            var eggWalker = new EggWalker(1000, session);

            if (route == null || route.RoutePoints.Count < 2)
            {
                session.EventDispatcher.Send(new BotCompleteFailureEvent()
                {
                    Shutdown = false,
                    Stop     = true
                });
                session.EventDispatcher.Send(new WarnEvent
                {
                    Message = "No proper route loaded, or route is too short"
                });
                return;
            }

            session.EventDispatcher.Send(new NoticeEvent()
            {
                Message = $"You are using a custom route named: '{session.LogicSettings.CustomRouteName}' with {session.LogicSettings.CustomRoute.RoutePoints.Count} routing points"
            });

            var navi = new Navigation(session.Client);

            navi.UpdatePositionEvent += (lat, lng, alt) =>
            {
                session.EventDispatcher.Send(new UpdatePositionEvent {
                    Latitude = lat, Longitude = lng, Altitude = alt
                });
            };

            //Find closest point of route and it's index!
            var closestPoint =
                route.RoutePoints.OrderBy(
                    x =>
                    LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                            session.Client.CurrentLongitude,
                                                            x.Latitude, x.Longitude)).First();
            var distToClosest = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                        session.Client.CurrentLongitude,
                                                                        closestPoint.Latitude, closestPoint.Longitude);

            if (distToClosest > 10)
            {
                session.State = BotState.Walk;
                session.EventDispatcher.Send(new NoticeEvent()
                {
                    Message = $"Found closest point at {closestPoint.Latitude} - {closestPoint.Longitude}, distance to that point: {distToClosest.ToString("N1")} meters, moving there!"
                });
                await session.Navigation.Move(closestPoint,
                                              session.LogicSettings.WalkingSpeedMin, session.LogicSettings.WalkingSpeedMax,
                                              async() =>
                {
                    if (session.LogicSettings.CatchWildPokemon)
                    {
                        // Catch normal map Pokemon
                        await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                        //Catch Incense Pokemon
                        await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                    }
                    return(true);
                },
                                              async() =>
                {
                    await UseNearbyPokestopsTask.Execute(session, cancellationToken, true);
                    return(true);
                }, cancellationToken, session);

                session.State = BotState.Idle;
            }

            var nextPath = route.RoutePoints.Select(item => Tuple.Create(item.Latitude, item.Longitude)).ToList();

            session.EventDispatcher.Send(new NextRouteEvent
            {
                Coords = nextPath
            });

            long nextMaintenceStamp = 0;
            var  initialize         = true;

            while (!cancellationToken.IsCancellationRequested)
            {
                foreach (var wp in route.RoutePoints)
                {
                    if (initialize)
                    {
                        if (wp != closestPoint)
                        {
                            continue;
                        }
                        initialize = false;
                    }

                    session.State = BotState.Walk;

                    var distance = LocationUtils.CalculateDistanceInMeters(session.Client.CurrentLatitude,
                                                                           session.Client.CurrentLongitude, wp.Latitude, wp.Longitude);

                    await navi.HumanPathWalking(
                        session,
                        wp,
                        NextMoveSpeed(session),
                        async() =>
                    {
                        await CatchNearbyPokemonsTask.Execute(session, cancellationToken);
                        //Catch Incense Pokemon
                        await CatchIncensePokemonsTask.Execute(session, cancellationToken);
                        return(true);
                    },
                        async() =>
                    {
                        await UseNearbyPokestopsTask.Execute(session, cancellationToken, true);
                        return(true);
                    },
                        cancellationToken
                        );

                    session.State = BotState.Idle;
                    await eggWalker.ApplyDistance(distance, cancellationToken);

                    if (nextMaintenceStamp >= DateTime.UtcNow.ToUnixTime() && session.Runtime.StopsHit < 100)
                    {
                        continue;
                    }
                    await MaintenanceTask.Execute(session, cancellationToken);

                    nextMaintenceStamp = DateTime.UtcNow.AddMinutes(3).ToUnixTime();
                }
                if (initialize)
                {
                    initialize = false;
                }
            }
        }