Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incense bugfixing #5364

Merged
merged 8 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"enabled": true,
"catch_visible_pokemon": true,
"catch_lured_pokemon": true,
"catch_incensed_pokemon": true,
"min_ultraball_to_keep": 5,
"berry_threshold": 0.35,
"vip_berry_threshold": 0.9,
Expand Down
1 change: 1 addition & 0 deletions docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ The behaviors of the bot are configured via the `tasks` key in the `config.json`
* `treat_unseen_as_vip`: Default `"true"` | If true, treat new to dex as VIP
* `catch_visible_pokemon`: Default "true" | If enabled, attempts to catch "visible" pokemon that are reachable
* `catch_lured_pokemon`: Default "true" | If enabled, attempts to catch "lured" pokemon that are reachable
* `catch_incensed_pokemon`: Default "true" | If enabled, attempts to catch pokemon that are found because of an active incense
* `min_ultraball_to_keep`: Default 5 | Minimum amount of reserved ultraballs to have on hand (for VIP)
* `berry_threshold`: Default 0.35 | Catch percentage we start throwing berries
* `vip_berry_threshold`: Default 0.9 | Something similar?
Expand Down
10 changes: 10 additions & 0 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ def _register_events(self):
'pokemon_name'
)
)
self.event_manager.register_event(
'incensed_pokemon_found',
parameters=(
'pokemon_id',
'encounter_id',
'encounter_location',
'latitude',
'longitude'
)
)
self.event_manager.register_event(
'pokemon_appeared',
parameters=(
Expand Down
55 changes: 42 additions & 13 deletions pokemongo_bot/cell_workers/catch_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.item_list import Item
from pokemongo_bot import inventory
from utils import fort_details, distance
from utils import fort_details, distance, format_time
from pokemongo_bot.base_dir import _base_dir
from pokemongo_bot.constants import Constants
from pokemongo_bot.inventory import Pokemons, Pokemon, Attack

from pokemongo_bot.inventory import Pokemons

class CatchPokemon(BaseTask):
SUPPORTED_TASK_API_VERSION = 1
Expand All @@ -35,6 +34,8 @@ def work(self):
self.get_visible_pokemon()
if self.config.get('catch_lured_pokemon', True):
self.get_lured_pokemon()
if self._have_applied_incense() and self.config.get('catch_incensed_pokemon', True):
self.get_incensed_pokemon()

random.shuffle(self.pokemon)

Expand All @@ -60,17 +61,17 @@ def get_visible_pokemon(self):
pokemon_to_catch = self.bot.cell['catchable_pokemons']

if len(pokemon_to_catch) > 0:
user_web_catchable = os.path.join(_base_dir, 'web', 'catchable-{}.json'.format(self.bot.config.username))
for pokemon in pokemon_to_catch:

# Update web UI
with open(user_web_catchable, 'w') as outfile:
user_web_catchable = os.path.join(_base_dir, 'web', 'catchable-{}.json'.format(self.bot.config.username))
for pokemon in pokemon_to_catch:
# Update web UI
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)

self.emit_event(
'catchable_pokemon',
level='debug',
data={

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'],
Expand All @@ -81,7 +82,7 @@ def get_visible_pokemon(self):
}
)

self.add_pokemon(pokemon)
self.add_pokemon(pokemon)

if 'wild_pokemons' in self.bot.cell:
for pokemon in self.bot.cell['wild_pokemons']:
Expand Down Expand Up @@ -132,6 +133,22 @@ def get_lured_pokemon(self):

self.add_pokemon(pokemon)

def get_incensed_pokemon(self):
# call self.bot.api.get_incense_pokemon
pokemon_to_catch = self.bot.api.get_incense_pokemon()

if len(pokemon_to_catch) > 0:
for pokemon in pokemon_to_catch:
self.logger.warning("Pokemon: %s", pokemon)
self.emit_event(
'incensed_pokemon_found',
level='info',
formatted='Incense attracted a pokemon at {encounter_location}',
data=pokemon
)

self.add_pokemon(pokemon)

def add_pokemon(self, pokemon):
if pokemon['encounter_id'] not in self.pokemon:
self.pokemon.append(pokemon)
Expand All @@ -141,3 +158,15 @@ def catch_pokemon(self, pokemon):
return_value = worker.work()

return return_value

def _have_applied_incense(self):
for applied_item in inventory.applied_items().all():
self.logger.info(applied_item)
if applied_item.expire_ms > 0:
mins = format_time(applied_item.expire_ms * 1000)
self.logger.info("Not applying incense, currently active: %s, %s minutes remaining", applied_item.item.name, mins)
return True
else:
self.logger.info("")
return False
return False
25 changes: 18 additions & 7 deletions pokemongo_bot/cell_workers/pokemon_catch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
import json
import logging
import time
import sys

from random import random, randrange, uniform
Expand All @@ -25,6 +24,8 @@
ENCOUNTER_STATUS_SUCCESS = 1
ENCOUNTER_STATUS_NOT_IN_RANGE = 5
ENCOUNTER_STATUS_POKEMON_INVENTORY_FULL = 7
INCENSE_ENCOUNTER_AVAILABLE = 1
INCENSE_ENCOUNTER_NOT_AVAILABLE = 2

ITEM_POKEBALL = 1
ITEM_GREATBALL = 2
Expand Down Expand Up @@ -105,9 +106,11 @@ def work(self, response_dict=None):
try:
responses = response_dict['responses']
response = responses[self.response_key]
if response[self.response_status_key] != ENCOUNTER_STATUS_SUCCESS:
if response[self.response_status_key] != ENCOUNTER_STATUS_SUCCESS and response[self.response_status_key] != INCENSE_ENCOUNTER_AVAILABLE:
if response[self.response_status_key] == ENCOUNTER_STATUS_NOT_IN_RANGE:
self.emit_event('pokemon_not_in_range', formatted='Pokemon went out of range!')
elif response[self.response_status_key] == INCENSE_ENCOUNTER_NOT_AVAILABLE:
self.emit_event('pokemon_not_in_range', formatted='Incensed Pokemon went out of range!')
elif response[self.response_status_key] == ENCOUNTER_STATUS_POKEMON_INVENTORY_FULL:
self.emit_event('pokemon_inventory_full', formatted='Your Pokemon inventory is full! Could not catch!')
return WorkerResult.ERROR
Expand All @@ -122,14 +125,14 @@ def work(self, response_dict=None):
if not self._should_catch_pokemon(pokemon):
if not hasattr(self.bot,'skipped_pokemon'):
self.bot.skipped_pokemon = []

# Check if pokemon already skipped and suppress alert if so
for skipped_pokemon in self.bot.skipped_pokemon:
if pokemon.pokemon_id == skipped_pokemon.pokemon_id and \
pokemon.cp_exact == skipped_pokemon.cp_exact and \
pokemon.ivcp == skipped_pokemon.ivcp:
return WorkerResult.SUCCESS

self.bot.skipped_pokemon.append(pokemon)
self.emit_event(
'pokemon_appeared',
Expand Down Expand Up @@ -215,7 +218,7 @@ def create_encounter_api_call(self):
player_latitude=player_latitude,
player_longitude=player_longitude
)
else:
elif 'fort_id' in self.pokemon:
fort_id = self.pokemon['fort_id']
self.spawn_point_guid = fort_id
self.response_key = 'DISK_ENCOUNTER'
Expand All @@ -226,6 +229,14 @@ def create_encounter_api_call(self):
player_latitude=player_latitude,
player_longitude=player_longitude
)
else:
# This must be a incensed mon
self.response_key = 'INCENSE_ENCOUNTER'
self.response_status_key = 'result'
request.incense_encounter(
encounter_id=encounter_id,
encounter_location=self.pokemon['encounter_location']
)
return request.call()

############################################################################
Expand Down Expand Up @@ -716,7 +727,7 @@ def generate_throw_quality_parameters(self, throw_parameters):
def start_rest(self):
duration = int(uniform(self.rest_duration_min, self.rest_duration_max))
resume = datetime.now() + timedelta(seconds=duration)

self.emit_event(
'vanish_limit_reached',
formatted="Vanish limit reached! Taking a rest now for {duration}, will resume at {resume}.",
Expand All @@ -725,7 +736,7 @@ def start_rest(self):
'resume': resume.strftime("%H:%M:%S")
}
)

sleep(duration)
self.rest_completed = True
self.bot.login()
136 changes: 73 additions & 63 deletions pokemongo_bot/cell_workers/use_incense.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,91 @@
from pokemongo_bot.worker_result import WorkerResult
from pokemongo_bot.item_list import Item
from pokemongo_bot import inventory
from utils import format_time


class UseIncense(BaseTask):
SUPPORTED_TASK_API_VERSION = 1

def initialize(self):
self.start_time = 0
self.use_incense = self.config.get('use_incense', False)
self.use_order = self.config.get('use_order', {})
self._update_inventory()
self.types = {
401: "Ordinary",
402: "Spicy",
403: "Cool",
404: "Floral"
self.start_time = 0
self.use_incense = self.config.get('use_incense', False)
self.use_order = self.config.get('use_order', {})
self._update_inventory()

self.types = {
401: "Ordinary",
402: "Spicy",
403: "Cool",
404: "Floral"
}


def _have_applied_incense(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate code from catch_pokemon.py ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, how would I dry this up?

for applied_item in inventory.applied_items().all():
if applied_item.expire_ms > 0:
mins = format_time(applied_item.expire_ms * 1000)
self.logger.info("Not applying incense, currently active: %s, %s minutes remaining", applied_item.item.name, mins)
return False
else:
return True

def _get_type(self):
for order in self.use_order:
if order == "ordinary" and self.incense_ordinary_count > 0:
return Item.ITEM_INCENSE_ORDINARY.value
if order == "spicy" and self.incense_spicy_count > 0:
return Item.ITEM_INCENSE_SPICY.value
if order == "cool" and self.incense_cool_count > 0:
return Item.ITEM_INCENSE_COOL.value
if order == "floral" and self.incense_floral_count > 0:
return Item.ITEM_INCENSE_FLORAL.value
return Item.ITEM_INCENSE_ORDINARY.value
for order in self.use_order:
if order == "ordinary" and self.incense_ordinary_count > 0:
return Item.ITEM_INCENSE_ORDINARY.value
if order == "spicy" and self.incense_spicy_count > 0:
return Item.ITEM_INCENSE_SPICY.value
if order == "cool" and self.incense_cool_count > 0:
return Item.ITEM_INCENSE_COOL.value
if order == "floral" and self.incense_floral_count > 0:
return Item.ITEM_INCENSE_FLORAL.value

return Item.ITEM_INCENSE_ORDINARY.value

def _update_inventory(self):
self.incense_ordinary_count = inventory.items().get(Item.ITEM_INCENSE_ORDINARY.value).count
self.incense_spicy_count = inventory.items().get(Item.ITEM_INCENSE_SPICY.value).count
self.incense_cool_count = inventory.items().get(Item.ITEM_INCENSE_COOL.value).count
self.incense_floral_count = inventory.items().get(Item.ITEM_INCENSE_FLORAL.value).count
self.incense_ordinary_count = inventory.items().get(Item.ITEM_INCENSE_ORDINARY.value).count
self.incense_spicy_count = inventory.items().get(Item.ITEM_INCENSE_SPICY.value).count
self.incense_cool_count = inventory.items().get(Item.ITEM_INCENSE_COOL.value).count
self.incense_floral_count = inventory.items().get(Item.ITEM_INCENSE_FLORAL.value).count

def _has_count(self):
return self.incense_ordinary_count > 0 or self.incense_spicy_count > 0 or self.incense_cool_count > 0 or self.incense_floral_count > 0
return self.incense_ordinary_count > 0 or self.incense_spicy_count > 0 or self.incense_cool_count > 0 or self.incense_floral_count > 0

def _should_run(self):
if not self.use_incense:
return False
if self._have_applied_incense:
return False

if not self.use_incense:
return False

if self._has_count() > 0 and self.start_time == 0:
return True

if self._has_count() > 0 and self.start_time == 0:
return True

using_incense = time.time() - self.start_time < 1800
if not using_incense:
self._update_inventory()
if self._has_count() and self.use_incense:
return True
return True

def work(self):
if self._should_run():
self.start_time = time.time()
type = self._get_type()
response_dict = self.bot.api.use_incense(incense_type=type)
result = response_dict.get('responses', {}).get('USE_INCENSE', {}).get('result', 0)
if result is 1:
self.emit_event(
'use_incense',
formatted="Using {type} incense. {incense_count} incense remaining",
data={
'type': self.types.get(type, 'Unknown'),
'incense_count': inventory.items().get(type).count
}
)
else:
self.emit_event(
'use_incense',
formatted="Unable to use incense {type}. {incense_count} incense remaining",
data={
'type': self.types.get(type, 'Unknown'),
'incense_count': inventory.items().get(type).count
}
)

return WorkerResult.SUCCESS
if self._should_run():
self.start_time = time.time()
response_dict = self.bot.api.use_incense(incense_type=self._get_type())
result = response_dict.get('responses', {}).get('USE_INCENSE', {}).get('result', 0)
if result is 1:
self.emit_event(
'use_incense',
formatted="Using {type} incense. {incense_count} incense remaining",
data={
'type': self.types.get(type, 'Unknown'),
'incense_count': inventory.items().get(type).count
}
)
else:
self.emit_event(
'use_incense',
formatted="Unable to use incense {type}. {incense_count} incense remaining",
data={
'type': self.types.get(type, 'Unknown'),
'incense_count': inventory.items().get(type).count
}
)
return WorkerResult.SUCCESS
Loading