Skip to content

Commit

Permalink
Added worker for incubating eggs (#1404)
Browse files Browse the repository at this point in the history
* Added worker for incubating eggs

* Added options to configuration

* Bugfix

* Ignoring used eggs and incubators

* Using cached inventory instead of getting a fresh copy
  • Loading branch information
tstumm authored and solderzzc committed Jul 28, 2016
1 parent fb01677 commit 8f0bace
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"evolve_speed": 20,
"cp_min": 300,
"use_lucky_egg": false,
"hatch_eggs": true,
"longer_eggs_first": true,
"evolve_captured": false,
"release_pokemon": true,
"catch": {
Expand Down
2 changes: 2 additions & 0 deletions configs/config.json.pokemons.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"evolve_all": "NONE",
"evolve_speed": 20,
"use_lucky_egg": false,
"hatch_eggs": true,
"longer_eggs_first": true,
"evolve_captured": false,
"release_pokemon": true,
"catch": {
Expand Down
3 changes: 3 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ def init_config():
config.action_wait_max = load.get('action_wait_max', 4)
config.action_wait_min = load.get('action_wait_min', 1)

config.hatch_eggs = load.get("hatch_eggs", True)
config.longer_eggs_first = load.get("longer_eggs_first", True)

if config.auth_service not in ['ptc', 'google']:
logging.error("Invalid Auth service specified! ('ptc' or 'google')")
return None
Expand Down
3 changes: 2 additions & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pgoapi.utilities import f2i

import logger
from cell_workers import SpinNearestFortWorker, CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker
from cell_workers import SpinNearestFortWorker, CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, PokemonTransferWorker, EvolveAllWorker, RecycleItemsWorker, IncubateEggsWorker
from cell_workers.utils import distance, get_cellid, encode, i2f
from human_behaviour import sleep
from item_list import Item
Expand Down Expand Up @@ -51,6 +51,7 @@ def tick(self):
self.check_session(self.position[0:2])

workers = [
IncubateEggsWorker,
PokemonTransferWorker,
EvolveAllWorker,
RecycleItemsWorker,
Expand Down
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from catch_visible_pokmeon_worker import CatchVisiblePokemonWorker
from recycle_items_worker import RecycleItemsWorker
from spin_nearest_fort_worker import SpinNearestFortWorker
from incubate_eggs_worker import IncubateEggsWorker
66 changes: 66 additions & 0 deletions pokemongo_bot/cell_workers/incubate_eggs_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from utils import distance, format_dist
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot import logger


class IncubateEggsWorker(object):
def __init__(self, bot):
self.api = bot.api
self.config = bot.config
self.bot = bot
# self.position = bot.position

def work(self):
if not self.config.hatch_eggs:
return

response_dict = self.bot.get_inventory()
inv = {}
incubators = []
eggs = []

try:
inv = reduce(dict.__getitem__, [
"responses", "GET_INVENTORY", "inventory_delta", "inventory_items"], response_dict)
except KeyError:
pass
else:
for inv_data in inv:
inv_data = inv_data.get("inventory_item_data", {})

if "egg_incubators" in inv_data:
for incubator in inv_data.get("egg_incubators", {}).get("egg_incubator", []):
if "pokemon_id" not in incubator:
incubators.append({"id":incubator.get("id", -1), "used":False})

if "pokemon_data" in inv_data:
pokemon = inv_data.get("pokemon_data", {})
if pokemon.get("is_egg", False) and "egg_incubator_id" not in pokemon:
eggs.append({"id": pokemon.get("id", -1), "km": pokemon.get("egg_km_walked_target", -1), "used": False})

sorting = self.config.longer_eggs_first
eggs.sort(key=lambda x: x.get("km"), reverse=sorting)

for incubator in incubators:
if incubator["used"]:
continue

for egg in eggs:
if egg["used"] or egg["km"] == -1:
continue

self.api.use_item_egg_incubator(item_id=incubator["id"], pokemon_id=egg["id"])
ret = self.api.call()
if ret:
code = ret.get("responses", {}).get("USE_ITEM_EGG_INCUBATOR", {}).get("result", 0)
if code == 1:
logger.log('Successfully incubated a ' + str(egg["km"]) + "km egg", 'green')
egg["used"] = True
incubator["used"] = True
break
elif code == 5 or code == 7:
incubator["used"] = True
break
elif code == 6:
egg["used"] = True

3 comments on commit 8f0bace

@fe-ax
Copy link

@fe-ax fe-ax commented on 8f0bace Jul 28, 2016

Choose a reason for hiding this comment

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

Doesn't seem to start hatching, config is edited:
"hatch_eggs": true,
"longer_eggs_first": true,

@lennart24
Copy link

@lennart24 lennart24 commented on 8f0bace Jul 28, 2016

Choose a reason for hiding this comment

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

is it just me or does the incubate_eggs_worker not work?

[12:44:07] Starting PokemonGo Bot....
Traceback (most recent call last):
  File "pokecli.py", line 294, in <module>
    main()
  File "pokecli.py", line 260, in main
    bot.tick()
  File "[...]/PokemonGo-Bot/pokemongo_bot/__init__.py", line 63, in tick
    if worker(self).work() == WorkerResult.RUNNING:
  File "[...]/PokemonGo-Bot/pokemongo_bot/cell_workers/incubate_eggs_worker.py", line 34, in work
    incubators.append({"id":incubator.get("id", -1), "used":False})
AttributeError: 'str' object has no attribute 'get'

@lennart24
Copy link

Choose a reason for hiding this comment

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

you're right. Had to update my requirements!

Please sign in to comment.