def visit_near_pokestops(self, bot, pokestops=None): # type: (PokemonGoBot, Optional[List[Fort]]) -> Dict[Str, List[PokeStop]] if pokestops is None: return # If we're debugging, don't filter pokestops so we can test if they are on cooldown if not bot.config["debug"]: pokestops = filtered_forts(bot.stepper.current_lat, bot.stepper.current_lng, pokestops) now = int(time.time()) * 1000 for pokestop in pokestops: dist = distance(bot.stepper.current_lat, bot.stepper.current_lng, pokestop.latitude, pokestop.longitude) if dist < 35: if pokestop.is_in_cooldown() is False: self.event_manager.fire_with_context('pokestop_arrived', bot, pokestop=pokestop) elif bot.config["debug"]: self.log( "Nearby fort found is in cooldown for {} ({}m away)". format( format_time( (pokestop.cooldown_timestamp_ms - now) / 1000), ceil(dist)), color="yellow")
def work(self): lat = self.fort.latitude lng = self.fort.longitude unit = self.config.distance_unit # Unit to use when printing formatted distance fort_id = self.fort.fort_id dist = distance(self.position[0], self.position[1], lat, lng) self.api_wrapper.fort_details(fort_id=fort_id, latitude=lat, longitude=lng) response_dict = self.api_wrapper.call() if response_dict is None: return fort_details = response_dict["fort"] fort_name = fort_details.fort_name self.bot.fire("fort_found", fort_name=fort_name, fort_distance=format_dist(dist, unit)) if dist > 0: self.bot.fire("fort_moving", fort_name=fort_name) position = (lat, lng, 0.0) if self.config.walk > 0: self.stepper.walk_to(self.config.walk, *position) else: self.api_wrapper.set_position(*position) self.api_wrapper.player_update(latitude=lat, longitude=lng) sleep(2) self.bot.fire("fort_arrived", fort_name=fort_name)
def navigate(self, map_cells): # type: (List[Cell]) -> None try: camp_site = self.camping_sites[self.pointer] lat, lng = camp_site position = (lat, lng, 0.0) unit = self.config.distance_unit # Unit to use when printing formatted distance dist = floor(distance(self.stepper.current_lat, self.stepper.current_lng, lat, lng)) # Given the random delta we add to the if dist > 0: logger.log( "[#] Moving to camping position at {},{} at distance {}".format(lat, lng, format_dist(dist, unit))) self.stepper.walk_to(*position) self.stepper.snap_to(*position) else: # fire any events on these cells logger.log("[#] Camping on {},{}".format(lat, lng)) position_map_cells = self.bot.mapper.get_cells(lat, lng) self.bot.work_on_cells(position_map_cells) sleep(5) except KeyError: logger.log("[#] No campsite location found", color="red")
def step(self, destination): # type: (Destination) -> None self.bot.fire("walking_started", coords=(destination.target_lat, destination.target_lng, destination.target_alt)) dist = distance(self.current_lat, self.current_lng, destination.target_lat, destination.target_lng) if destination.name: logger.log("Walking towards {} ({} away, eta {})".format( destination.name, format_dist(dist, self.config.distance_unit), format_time(len(destination.steps))), prefix="Navigation") for step in destination.steps: self._step_to(*step) yield step if destination.name: logger.log("Arrived at {} ({} away)".format( destination.name, format_dist(dist, self.config.distance_unit)), prefix="Navigation") self.bot.fire("walking_finished", coords=(destination.target_lat, destination.target_lng, destination.target_alt))
def get_cells(self, lat, lng): # type: (float, float) -> List[Cell] cell_id = self._get_cell_id_from_latlong() timestamp = [0, ] * len(cell_id) self.api_wrapper.get_map_objects(latitude=lat, longitude=lng, since_timestamp_ms=timestamp, cell_id=cell_id) response_dict = self.api_wrapper.call() if response_dict is None: return [] # Passing data through last-location and location map_objects = response_dict["worldmap"] with open("data/last-location-{}.json".format(self.config["login"]["username"]), "w") as outfile: outfile.truncate() json.dump({"lat": lat, "lng": lng}, outfile) map_cells = map_objects.cells # Sort all by distance from current pos - eventually this should build graph and A* it map_cells.sort(key=lambda x: distance(lat, lng, x.pokestops[0].latitude, x.pokestops[0].longitude) if len( x.pokestops) > 0 else 1e6) return map_cells
def work(self): lat = self.fort.latitude lng = self.fort.longitude unit = self.config.distance_unit # Unit to use when printing formatted distance fort_id = self.fort.fort_id dist = distance(self.position[0], self.position[1], lat, lng) self.api_wrapper.fort_details(fort_id=fort_id, latitude=lat, longitude=lng) response_dict = self.api_wrapper.call() if response_dict is None: return fort_details = response_dict["fort"] fort_name = fort_details.fort_name logger.log(u"[#] Found fort {} at distance {}".format(fort_name, format_dist(dist, unit))) if dist > 0: logger.log(u"[#] Moving closer to {}".format(fort_name)) position = (lat, lng, 0.0) if self.config.walk > 0: self.stepper.walk_to(self.config.walk, *position) else: self.api_wrapper.set_position(*position) self.api_wrapper.player_update(latitude=lat, longitude=lng) sleep(2) logger.log(u"[#] Now at Pokestop: {}".format(fort_name))
def work(self): lat = self.fort["latitude"] lng = self.fort["longitude"] unit = self.config.distance_unit # Unit to use when printing formatted distance fort_id = self.fort["id"] dist = distance(self.position[0], self.position[1], lat, lng) logger.log("[#] Found fort {} at distance {}".format(fort_id, format_dist(dist, unit))) if dist > 0: logger.log("[#] Need to move closer to Pokestop") position = (lat, lng, 0.0) if self.config.walk > 0: self.stepper.walk_to(self.config.walk, *position) else: self.api.set_position(*position) self.api.player_update(latitude=lat, longitude=lng) logger.log("[#] Arrived at Pokestop") sleep(2) self.api.fort_details(fort_id=self.fort["id"], latitude=lat, longitude=lng) response_dict = self.api.call() if response_dict is None: return fort_details = response_dict.get("responses", {}).get("FORT_DETAILS", {}) fort_name = fort_details.get("name") if fort_details.get("name") else "Unknown" logger.log(u"[#] Now at Pokestop: " + fort_name)
def get_cells(self, lat, lng): # type: (float, float) -> List[Cell] cell_id = self._get_cell_id_from_latlong( self.config['mapping']['cell_radius'] ) timestamp = [0, ] * len(cell_id) self.api_wrapper.get_map_objects(latitude=lat, longitude=lng, since_timestamp_ms=timestamp, cell_id=cell_id) response_dict = self.api_wrapper.call() if response_dict is None: return [] # Passing data through last-location and location map_objects = response_dict["worldmap"] with open("data/last-location-{}.json".format(self.config["login"]["username"]), "w") as outfile: outfile.truncate() json.dump({"lat": lat, "lng": lng}, outfile) map_cells = map_objects.cells # Sort all by distance from current pos - eventually this should build graph and A* it map_cells.sort(key=lambda x: distance(lat, lng, x.pokestops[0].latitude, x.pokestops[0].longitude) if len( x.pokestops) > 0 else 1e6) return map_cells
def _work_at_position(self, lat, lng, pokemon_only=False): cell_id = self._get_cell_id_from_latlong() timestamp = [0, ] * len(cell_id) self.api.get_map_objects(latitude=f2i(lat), longitude=f2i(lng), since_timestamp_ms=timestamp, cell_id=cell_id) response_dict = self.api.call() if response_dict is None: return # Passing data through last-location and location map_objects = response_dict.get("responses", {}).get("GET_MAP_OBJECTS") if map_objects is not None: with open("web/location-{}.json".format(self.config.username), "w") as outfile: json.dump({"lat": lat, "lng": lng, "cells": convert_to_utf8(map_objects.get("map_cells"))}, outfile) with open("data/last-location-{}.json".format(self.config.username), "w") as outfile: outfile.truncate() json.dump({"lat": lat, "lng": lng}, outfile) if "status" in map_objects: if map_objects.get("status") is 1: map_cells = map_objects.get("map_cells") # Sort all by distance from current pos - eventually this should build graph and A* it map_cells.sort(key=lambda x: distance(lat, lng, x["forts"][0]["latitude"], x["forts"][0]["longitude"]) if "forts" in x and x["forts"] != [] else 1e6) for cell in map_cells: self.bot.work_on_cell(cell, pokemon_only)
def navigate(self, map_cells): # type: (List[Cell]) -> List([Destination]) for cell in map_cells: pokestops = [pokestop for pokestop in cell.pokestops if pokestop.latitude is not None and pokestop.longitude is not None] # gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] # Sort all by distance from current pos- eventually this should # build graph & A* it pokestops.sort(key=lambda x: distance(self.stepper.current_lat, self.stepper.current_lng, x.latitude, x.longitude)) for fort in pokestops: response_dict = self.api_wrapper.fort_details( fort_id=fort.fort_id, latitude=fort.latitude, longitude=fort.longitude ).call() if response_dict is None: fort_name = fort.fort_id else: fort_name = response_dict["fort"].fort_name if isinstance(fort_name, bytes): fort_name = fort_name.decode() yield Destination(fort.latitude, fort.longitude, 0.0, name="PokeStop \"{}\"".format(fort_name)) self.api_wrapper.player_update(latitude=fort.latitude, longitude=fort.longitude) sleep(2)
def _do_walk_to(self, speed, from_lat, from_lng, to_lat, to_lng, alt, delta_factor): # type: (float, float, float, float, float, float, float) -> None dist = distance(from_lat, from_lng, to_lat, to_lng) steps = (dist / (self.AVERAGE_STRIDE_LENGTH_IN_METRES * speed)) logger.log("[#] Walking from " + str((from_lat, from_lng)) + " to " + str( str((to_lat, to_lng))) + " for approx. " + str(format_time(ceil(steps)))) if steps != 0: d_lat = (to_lat - from_lat) / steps d_long = (to_lng - from_lng) / steps for _ in range(int(steps)): position_lat, position_lng, _ = self.api_wrapper.get_position() c_lat = position_lat + d_lat + random_lat_long_delta(delta_factor) c_long = position_lng + d_long + random_lat_long_delta(delta_factor) self.api_wrapper.set_position(c_lat, c_long, alt) self.bot.heartbeat() sleep(1) # sleep one second plus a random delta position_lat, position_lng, _ = self.api_wrapper.get_position() self._work_at_position(position_lat, position_lng) self.bot.heartbeat()
def visit_near_pokestops(bot, pokestops=None): # type: (PokemonGoBot, Optional[List[Fort]]) -> Dict[Str, List[PokeStop]] def log(text, color="black"): logger.log(text, color=color, prefix="PokeStop") if pokestops is None: return # If we're debugging, don't filter pokestops so we can test if they are on cooldown if not bot.config.debug: pokestops = filtered_forts(bot.stepper.current_lat, bot.stepper.current_lng, pokestops) now = int(time.time()) * 1000 for pokestop in pokestops: dist = distance(bot.stepper.current_lat, bot.stepper.current_lng, pokestop.latitude, pokestop.longitude) if dist < 15: if pokestop.cooldown_timestamp_ms < now: manager.fire_with_context("pokestop_arrived", bot, pokestop=pokestop) elif bot.config.debug: log( "Nearby fort found is in cooldown for {} ({}m away)".format( format_time((pokestop.cooldown_timestamp_ms - now) / 1000), ceil(dist) ), color="yellow", )
def work_on_cell(self, cell, include_fort_on_path): # check if token session has expired self.check_session() self._remove_ignored_pokemon(cell) if (self.config.mode == "all" or self.config.mode == "poke") and 'catchable_pokemons' in cell and len(cell['catchable_pokemons']) > 0: logger.log('[#] Something rustles nearby!') # Sort all by distance from current pos- eventually this should # build graph & A* it cell['catchable_pokemons'].sort(key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) user_web_catchable = 'web/catchable-%s.json' % self.config.username for pokemon in cell['catchable_pokemons']: with open(user_web_catchable, 'w') as outfile: json.dump(pokemon, outfile) if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS: break with open(user_web_catchable, 'w') as outfile: json.dump({}, outfile) if (self.config.mode == "all" or self.config.mode == "poke") and 'wild_pokemons' in cell and len(cell['wild_pokemons']) > 0: # Sort all by distance from current pos- eventually this should # build graph & A* it cell['wild_pokemons'].sort(key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) for pokemon in cell['wild_pokemons']: if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS: break if include_fort_on_path: if 'forts' in cell: # Only include those with a lat/long forts = [fort for fort in cell['forts'] if 'latitude' in fort and 'type' in fort] # gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] # Sort all by distance from current pos- eventually this should # build graph & A* it forts.sort(key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) for fort in forts: walk_worker = WalkTowardsFortWorker(fort, self) walk_worker.work() if self.config.mode == "all" or self.config.mode == "farm": spinner_worker = SeenFortWorker(fort, self) spinner_worker.work()
def step(self, destination): # type: (Destination) -> List[(float, float, float)] dist = distance(self.current_lat, self.current_lng, destination.target_lat, destination.target_lng) if destination.name: self.logger.log("Walking towards {} ({} away, eta {})".format(destination.name, format_dist(dist, self.config["mapping"]["distance_unit"]), format_time(destination.get_step_count())), prefix="Navigation") for step in destination.step(): if distance(self.current_lat, self.current_lng, destination.target_lat, destination.target_lng) < 30: break self._step_to(*step) yield step if destination.name: self.logger.log("Arrived at {} ({} away)".format(destination.name, format_dist(dist, self.config["mapping"]["distance_unit"])), prefix="Navigation")
def _work_on_wild_pokemon(self, map_cells): for cell in map_cells: if 'wild_pokemons' in cell and len(cell['wild_pokemons']) > 0: # Sort all by distance from current pos- eventually this should # build graph & A* it cell['wild_pokemons'].sort( key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) for pokemon in cell['wild_pokemons']: worker = PokemonCatchWorker(pokemon, self) if worker.work() == -1: break
def step(self, destination): # type: (Destination) -> List[(float, float, float)] self.bot.fire("walking_started", coords=(destination.target_lat, destination.target_lng, destination.target_alt)) dist = distance(self.current_lat, self.current_lng, destination.target_lat, destination.target_lng) if destination.name: logger.log("Walking towards {} ({} away, eta {})".format(destination.name, format_dist(dist, self.config.distance_unit), format_time(destination.get_step_count())), prefix="Navigation") for step in destination.step(): if distance(self.current_lat, self.current_lng, destination.target_lat, destination.target_lng) < 30: break self._step_to(*step) yield step if destination.name: logger.log("Arrived at {} ({} away)".format(destination.name, format_dist(dist, self.config.distance_unit)), prefix="Navigation") self.bot.fire("walking_finished", coords=(destination.target_lat, destination.target_lng, destination.target_alt))
def snap_to(self, to_lat, to_lng, to_alt): # type: (float, float, float) -> None """ This method is to correct a position you are near to. If you try and snap a distance over 10 meters, it will fail. """ # type: (float, float, float) -> None dist = distance(self.current_lat, self.current_lng, to_lat, to_lng) # Never snap big distances if dist > 15: return self._step_to(to_lat, to_lng, to_alt)
def work(self): if self.config.fill_incubators: self.api_wrapper.get_inventory() response_dict = self.api_wrapper.call() eggs = [egg.unique_id for egg in response_dict["eggs"] if egg.egg_incubator_id == ""] incubators = [incu.unique_id for incu in response_dict["egg_incubators"] if incu.pokemon_id == 0 and ( self.config.use_all_incubators or incu.item_id == 901)] for incubator_unique_id in incubators: if len(eggs) > 0: self.api_wrapper.use_item_egg_incubator(item_id=incubator_unique_id, pokemon_id=eggs.pop()) self.api_wrapper.call() logger.log("[+] Put an egg into an incubator", "green") else: logger.log("[+] No more free incubators", "yellow") lat = self.fort.latitude lng = self.fort.longitude unit = self.config.distance_unit # Unit to use when printing formatted distance fort_id = self.fort.fort_id dist = distance(self.position[0], self.position[1], lat, lng) self.api_wrapper.fort_details(fort_id=fort_id, latitude=lat, longitude=lng) response_dict = self.api_wrapper.call() if response_dict is None: return fort_details = response_dict["fort"] fort_name = fort_details.fort_name logger.log(u"[#] Found fort {} at distance {}".format(fort_name, format_dist(dist, unit))) if dist > 0: logger.log(u"[#] Moving closer to {}".format(fort_name)) position = (lat, lng, 0.0) if self.config.walk > 0: self.stepper.walk_to(self.config.walk, *position) else: self.api_wrapper.set_position(*position) self.api_wrapper.player_update(latitude=lat, longitude=lng) sleep(2) logger.log(u"[#] Now at Pokestop: {}".format(fort_name))
def _work_on_catchable_pokemon(self, map_cells): for cell in map_cells: if 'catchable_pokemons' in cell and len(cell['catchable_pokemons']) > 0: logger.log('[#] Something rustles nearby!') # Sort all by distance from current pos- eventually this should # build graph & A* it cell['catchable_pokemons'].sort( key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) for pokemon in cell['catchable_pokemons']: with open('web/catchable-%s.json' % self.config.username, 'w') as outfile: json.dump(pokemon, outfile) worker = PokemonCatchWorker(pokemon, self) if worker.work() == -1: break with open('web/catchable-%s.json' % self.config.username, 'w') as outfile: json.dump({}, outfile)
def work_on_cell(self, cell, include_fort_on_path): # type: (Cell, bool) -> None self.fire("pokemon_found", pokemon=cell.catchable_pokemon + cell.wild_pokemon) self._remove_ignored_pokemon(cell) if self.config.mode in ["all", "poke"]: pass if (self.config.mode == "all" or self.config.mode == "poke") and len(cell.catchable_pokemon) > 0: logger.log('[#] Something rustles nearby!') # Sort all by distance from current pos- eventually this should # build graph & A* it cell.catchable_pokemon.sort(key=lambda x: x.get("time_until_hidden_ms", 0)) for pokemon in cell.catchable_pokemon: if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS: break if (self.config.mode == "all" or self.config.mode == "poke") and len(cell.wild_pokemon) > 0: # Sort all by distance from current pos- eventually this should # build graph & A* it # cell.wild_pokemon.sort(key=lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude'])) cell.wild_pokemon.sort(key=lambda x: x.get("time_until_hidden_ms", 0)) for pokemon in cell.wild_pokemon: if self.catch_pokemon(pokemon) == PokemonCatchWorker.NO_POKEBALLS: break if include_fort_on_path: # Only include those with a lat/long pokestops = [pokestop for pokestop in cell.pokestops if pokestop.latitude is not None and pokestop.longitude is not None] # gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] # Sort all by distance from current pos- eventually this should # build graph & A* it pokestops.sort(key=lambda x: distance(self.position[0], self.position[1], x.latitude, x.longitude)) for pokestop in pokestops: walk_worker = WalkTowardsFortWorker(pokestop, self) walk_worker.work() if self.config.mode == "all" or self.config.mode == "farm": spinner_worker = SeenFortWorker(pokestop, self) spinner_worker.work()
def _walk_to(self, to_lat, to_lng, to_alt): # type: (float, float, float) -> None dist = distance(self.current_lat, self.current_lng, to_lat, to_lng) steps = (dist / (self.AVERAGE_STRIDE_LENGTH_IN_METRES * self.speed)) if self.config.debug: logger.log("[#] Walking from " + str((self.current_lat, self.current_lng)) + " to " + str( str((to_lat, to_lng))) + " for approx. " + str(format_time(ceil(steps)))) if steps != 0: d_lat = (to_lat - self.current_lat) / steps d_long = (to_lng - self.current_lng) / steps for _ in range(int(ceil(steps))): c_lat = self.current_lat + d_lat + random_lat_long_delta(10) c_long = self.current_lng + d_long + random_lat_long_delta(10) self._jump_to(c_lat, c_long, to_alt) self.bot.heartbeat()
def _get_steps_between(self, from_lat, from_lng, to_lat, to_lng, alt): # type: (float, float, float) -> List[(float,float,float)] dist = distance(from_lat, from_lng, to_lat, to_lng) steps = (dist / (self.AVERAGE_STRIDE_LENGTH_IN_METRES * self.speed)) step_locations = list() if steps != 0: d_lat = (to_lat - from_lat) / steps d_long = (to_lng - from_lng) / steps total_steps = int(ceil(steps)) for _ in range(total_steps): from_lat += d_lat from_lng += d_long c_lat = from_lat + random_lat_long_delta(10) c_long = from_lng + random_lat_long_delta(10) step_locations.append((c_lat, c_long, alt)) return step_locations
def navigate(self, map_cells): # type: (List[Cell]) -> None while self.pointer < len(self.waypoints)-1: waypoint = self.waypoints[self.pointer] if waypoint is None: self.pointer += 1 continue lat, lng = waypoint position = (lat, lng, 0.0) unit = self.config.distance_unit # Unit to use when printing formatted distance dist = distance(self.stepper.current_lat, self.stepper.current_lng, lat, lng) logger.log("[#] Moving to waypoint at {},{} at distance {}".format(lat, lng, format_dist(dist, unit))) self.stepper.walk_to(*position) self.pointer += 1 self.pointer = 0
def navigate(self, map_cells): # type: (List[Cell]) -> None for cell in map_cells: pokestops = [ pokestop for pokestop in cell.pokestops if pokestop.latitude is not None and pokestop.longitude is not None ] # gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] # Sort all by distance from current pos- eventually this should # build graph & A* it pokestops.sort( key=lambda x: distance(self.stepper.current_lat, self.stepper. current_lng, x.latitude, x.longitude)) for fort in pokestops: response_dict = self.api_wrapper.fort_details( fort_id=fort.fort_id, latitude=fort.latitude, longitude=fort.longitude).call() if response_dict is None: fort_name = fort.fort_id else: fort_name = response_dict["fort"].fort_name yield Destination(fort.latitude, fort.longitude, 0.0, name="PokeStop \"{}\"".format(fort_name)) self.api_wrapper.player_update(latitude=fort.latitude, longitude=fort.longitude) sleep(2)
def navigate(self, map_cells): # type: (List[Cell]) -> List([Destination]) for cell in map_cells: pokestops = [ pokestop for pokestop in cell.pokestops if pokestop.latitude is not None and pokestop.longitude is not None ] # gyms = [gym for gym in cell['forts'] if 'gym_points' in gym] # Sort all by distance from current pos- eventually this should # build graph & A* it current_lat, current_lng, _ = self.api_wrapper.get_position() pokestops.sort(key=lambda x, lat=current_lat, lng=current_lng: distance(lat, lng, x.latitude, x.longitude)) for fort in pokestops: response_dict = self.api_wrapper.fort_details( fort_id=fort.fort_id, latitude=fort.latitude, longitude=fort.longitude) if response_dict is None: fort_name = fort.fort_id else: fort_name = response_dict["fort"].fort_name if isinstance(fort_name, bytes): fort_name = fort_name.decode() yield Destination(fort.latitude, fort.longitude, 0.0, name="PokeStop \"{}\"".format(fort_name))
def walk_to(self, speed, lat, lng, alt): position_lat, position_lng, _ = self.api.get_position() dist = distance(i2f(position_lat), i2f(position_lng), lat, lng) steps = (dist / (self.AVERAGE_STRIDE_LENGTH_IN_METRES * speed)) logger.log("[#] Walking from " + str((i2f(position_lat), i2f(position_lng))) + " to " + str(str((lat, lng))) + " for approx. " + str(format_time(ceil(steps)))) if steps != 0: d_lat = (lat - i2f(position_lat)) / steps d_long = (lng - i2f(position_lng)) / steps for _ in range(int(steps)): position_lat, position_lng, _ = self.api.get_position() c_lat = i2f(position_lat) + d_lat + random_lat_long_delta() c_long = i2f(position_lng) + d_long + random_lat_long_delta() self.api.set_position(c_lat, c_long, alt) self.bot.heartbeat() sleep(1) # sleep one second plus a random delta position_lat, position_lng, _ = self.api.get_position() self._work_at_position(i2f(position_lat), i2f(position_lng), False) self.bot.heartbeat() logger.log("[#] Finished walking")
def _work_at_position(self, lat, lng, pokemon_only=False): # type: (float, float, Optional[bool]) -> None cell_id = self._get_cell_id_from_latlong() timestamp = [0, ] * len(cell_id) self.api_wrapper.get_map_objects(latitude=f2i(lat), longitude=f2i(lng), since_timestamp_ms=timestamp, cell_id=cell_id) response_dict = self.api_wrapper.call() if response_dict is None: return # Passing data through last-location and location map_objects = response_dict["worldmap"] with open("data/last-location-{}.json".format(self.config.username), "w") as outfile: outfile.truncate() json.dump({"lat": lat, "lng": lng}, outfile) map_cells = map_objects.cells # Sort all by distance from current pos - eventually this should build graph and A* it map_cells.sort(key=lambda x: distance(lat, lng, x.pokestops[0].latitude, x.pokestops[0].longitude) if len( x.pokestops) > 0 else 1e6) for cell in map_cells: self.bot.work_on_cell(cell, pokemon_only)
def spin_pokestop(bot, pokestop=None): # type: (PokemonGoBot, Optional[List[Fort]]) -> None if pokestop is None: return def log(text, color="black"): logger.log(text, color=color, prefix="PokeStop") fort_id = pokestop.fort_id latitude = pokestop.latitude longitude = pokestop.longitude player_latitude = bot.stepper.current_lat player_longitude = bot.stepper.current_lng fort_details = bot.api_wrapper.fort_details(fort_id=pokestop.fort_id, latitude=pokestop.latitude, longitude=pokestop.longitude).call() dist = distance(bot.stepper.current_lat, bot.stepper.current_lng, pokestop.latitude, pokestop.longitude) log("Nearby PokeStop found \"{}\" ({} away)".format(fort_details["fort"].fort_name, format_dist(dist, bot.config["mapping"]["distance_unit"])), color="yellow") log("Spinning...", color="yellow") sleep(3) bot.api_wrapper.fort_search(fort_id=fort_id, fort_latitude=latitude, fort_longitude=longitude, player_latitude=player_latitude, player_longitude=player_longitude) response = bot.api_wrapper.call() if response is None: log("Got empty response from the API. Skipping.", color="red") return # TODO: Fix this to use a response object spin_details = response["FORT_SEARCH"] spin_result = spin_details.get("result") if spin_result == 1: log("Loot: ", "green") manager.fire_with_context('pokestop_visited', bot, pokestop=pokestop) experience_awarded = spin_details.get("experience_awarded", False) if experience_awarded: log("+ {} XP".format(experience_awarded), "green") items_awarded = spin_details.get("items_awarded", []) if len(items_awarded) > 0: tmp_count_items = {} for item in items_awarded: item_id = item["item_id"] if item_id not in tmp_count_items: tmp_count_items[item_id] = item["item_count"] else: tmp_count_items[item_id] += item["item_count"] for item_id, item_count in tmp_count_items.items(): item_name = bot.item_list[item_id] log("+ {} {}{}".format(item_count, item_name, "s" if item_count > 1 else ""), "green") else: log("Nothing found.", "yellow") pokestop_cooldown = spin_details.get("cooldown_complete_timestamp_ms") if pokestop_cooldown: seconds_since_epoch = time.time() cooldown_time = str(format_time((pokestop_cooldown / 1000) - seconds_since_epoch)) log("PokeStop is on cooldown for {}.".format(cooldown_time)) # Update the cooldown manually pokestop.cooldown_timestamp_ms = pokestop_cooldown if not items_awarded and not experience_awarded and not pokestop_cooldown: log("Might be softbanned, try again later.", "red") elif spin_result == 2: log("PokeStop is out of range.", "red") elif spin_result == 3: log("PokeStop is already on cooldown.", "red") pokestop_cooldown = spin_details.get("cooldown_complete_timestamp_ms") if pokestop_cooldown: seconds_since_epoch = time.time() cooldown_time = str(format_time((pokestop_cooldown / 1000) - seconds_since_epoch)) log("PokeStop is already on cooldown for {}.".format(cooldown_time), "red") elif spin_result == 4: manager.fire_with_context('pokestop_visited', bot, pokestop=pokestop) experience_awarded = spin_details.get("experience_awarded", False) if experience_awarded: log("Loot: ", "green") log("+ {} XP".format(experience_awarded), "green") log("Item bag is full.", "red") bot.fire("item_bag_full") if not experience_awarded and not pokestop_cooldown: log("Might be softbanned, try again later.", "red") else: log("I don't know what happened! Maybe servers are down?", "red") sleep(2)
def spin_pokestop(self, bot, pokestop=None): # type: (PokemonGoBot, Optional[List[Fort]]) -> None if pokestop is None: return fort_id = pokestop.fort_id latitude = pokestop.latitude longitude = pokestop.longitude player_latitude = bot.stepper.current_lat player_longitude = bot.stepper.current_lng fort_details = bot.api_wrapper.fort_details( fort_id=pokestop.fort_id, latitude=pokestop.latitude, longitude=pokestop.longitude).call() dist = distance(bot.stepper.current_lat, bot.stepper.current_lng, pokestop.latitude, pokestop.longitude) self.log("Nearby PokeStop found \"{}\" ({} away)".format( fort_details["fort"].fort_name, format_dist(dist, bot.config["mapping"]["distance_unit"])), color="yellow") self.log("Spinning...", color="yellow") sleep(3) bot.api_wrapper.fort_search(fort_id=fort_id, fort_latitude=latitude, fort_longitude=longitude, player_latitude=player_latitude, player_longitude=player_longitude) response = bot.api_wrapper.call() if response is None: self.log("Got empty response from the API. Skipping.", color="red") return # TODO: Fix this to use a response object spin_details = response["FORT_SEARCH"] spin_result = spin_details.get("result") if spin_result == 1: self.log("Loot: ", "green") self.event_manager.fire_with_context('pokestop_visited', bot, pokestop=pokestop) experience_awarded = spin_details.get("experience_awarded", False) if experience_awarded: self.log("+ {} XP".format(experience_awarded), "green") items_awarded = spin_details.get("items_awarded", []) if len(items_awarded) > 0: tmp_count_items = {} for item in items_awarded: item_id = item["item_id"] if item_id not in tmp_count_items: tmp_count_items[item_id] = item["item_count"] else: tmp_count_items[item_id] += item["item_count"] for item_id, item_count in tmp_count_items.items(): item_name = bot.item_list[item_id] self.log( "+ {} {}{}".format(item_count, item_name, "s" if item_count > 1 else ""), "green") else: self.log("Nothing found.", "yellow") pokestop_cooldown = spin_details.get( "cooldown_complete_timestamp_ms") if pokestop_cooldown: seconds_since_epoch = time.time() cooldown_time = str( format_time((pokestop_cooldown / 1000) - seconds_since_epoch)) self.log( "PokeStop is on cooldown for {}.".format(cooldown_time)) # Update the cooldown manually pokestop.cooldown_timestamp_ms = pokestop_cooldown if not items_awarded and not experience_awarded and not pokestop_cooldown: self.log("Might be softbanned, try again later.", "red") elif spin_result == 2: self.log("PokeStop is out of range.", "red") elif spin_result == 3: self.log("PokeStop is already on cooldown.", "red") pokestop_cooldown = spin_details.get( "cooldown_complete_timestamp_ms") if pokestop_cooldown: seconds_since_epoch = time.time() cooldown_time = str( format_time((pokestop_cooldown / 1000) - seconds_since_epoch)) self.log( "PokeStop is already on cooldown for {}.".format( cooldown_time), "red") elif spin_result == 4: self.event_manager.fire_with_context('pokestop_visited', bot, pokestop=pokestop) experience_awarded = spin_details.get("experience_awarded", False) if experience_awarded: self.log("Loot: ", "green") self.log("+ {} XP".format(experience_awarded), "green") self.log("Item bag is full.", "red") bot.fire("item_bag_full") if not experience_awarded and not pokestop_cooldown: self.log("Might be softbanned, try again later.", "red") else: self.log("I don't know what happened! Maybe servers are down?", "red") sleep(2)
def test_distance(): dist = distance(51.503056, -0.119500, 51.503635, -0.119337) assert round(dist, 2) == 65.41