Last active
August 10, 2016 11:32
-
-
Save g2384/bb86b2fd0f04d1c62cd6ca3a7d0a3aa3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# updated to https://github.com/PokemonGoF/PokemonGo-Bot/commit/0c3c4c004d5081b91ffe278850d1b1821e4f63b4 | |
import json | |
import os | |
from pokemongo_bot.base_task import BaseTask | |
from pokemongo_bot.cell_workers.pokemon_catch_worker import PokemonCatchWorker | |
from utils import distance | |
from pokemongo_bot.worker_result import WorkerResult | |
from pokemongo_bot.base_dir import _base_dir | |
ignored_poke_arr = [] | |
class CatchVisiblePokemon(BaseTask): | |
SUPPORTED_TASK_API_VERSION = 1 | |
def work(self): | |
global ignored_poke_arr | |
num_catchable_pokemon = 0 | |
if 'catchable_pokemons' in self.bot.cell: | |
num_catchable_pokemon = len(self.bot.cell['catchable_pokemons']) | |
num_wild_pokemon = 0 | |
if 'wild_pokemons' in self.bot.cell: | |
num_wild_pokemon = len(self.bot.cell['wild_pokemons']) | |
num_available_pokemon = num_catchable_pokemon + num_wild_pokemon | |
if num_catchable_pokemon > 0: | |
# Sort all by distance from current pos- eventually this should | |
# build graph & A* it | |
self.bot.cell['catchable_pokemons'].sort( | |
key= | |
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']) | |
) | |
user_web_catchable = os.path.join(_base_dir, 'web', 'catchable-{}.json'.format(self.bot.config.username)) | |
for pokemon in self.bot.cell['catchable_pokemons']: | |
if pokemon['encounter_id'] in ignored_poke_arr: | |
print('fixed infinite loop, CatchVisiblePokemon num_catchable_pokemon') | |
self.bot.cell['catchable_pokemons'].pop(0) | |
num_catchable_pokemon -= 1 | |
if num_catchable_pokemon < 1: | |
return WorkerResult.SUCCESS | |
continue | |
else: | |
ignored_poke_arr.append(pokemon['encounter_id']) | |
if len(ignored_poke_arr) > 20: | |
ignored_poke_arr.pop(0) | |
with open(user_web_catchable, 'w') as outfile: | |
json.dump(pokemon, outfile) | |
self.emit_event( | |
'catchable_pokemon', | |
level='debug', | |
data={ | |
'pokemon_id': pokemon['pokemon_id'], | |
'spawn_point_id': pokemon['spawn_point_id'], | |
'encounter_id': pokemon['encounter_id'], | |
'latitude': pokemon['latitude'], | |
'longitude': pokemon['longitude'], | |
'expiration_timestamp_ms': pokemon['expiration_timestamp_ms'], | |
} | |
) | |
self.catch_pokemon(self.bot.cell['catchable_pokemons'].pop(0)) | |
if num_catchable_pokemon > 1: | |
return WorkerResult.RUNNING | |
else: | |
return WorkerResult.SUCCESS | |
if num_available_pokemon > 0: | |
# Sort all by distance from current pos- eventually this should | |
# build graph & A* it | |
self.bot.cell['wild_pokemons'].sort( | |
key= | |
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude'])) | |
if pokemon['encounter_id'] in ignored_poke_arr: | |
print('fixed infinite loop, CatchVisiblePokemon num_available_pokemon') | |
self.bot.cell['wild_pokemons'].pop(0) | |
num_available_pokemon -= 1 | |
if num_available_pokemon < 1: | |
return WorkerResult.SUCCESS | |
else: | |
ignored_poke_arr.append(pokemon['encounter_id']) | |
if len(ignored_poke_arr) > 20: | |
ignored_poke_arr.pop(0) | |
self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0)) | |
if num_catchable_pokemon > 1: | |
return WorkerResult.RUNNING | |
else: | |
return WorkerResult.SUCCESS | |
def catch_pokemon(self, pokemon): | |
worker = PokemonCatchWorker(pokemon, self.bot) | |
return_value = worker.work() | |
return return_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment