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

Added worker for incubating eggs #1404

Merged
merged 5 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
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
65 changes: 65 additions & 0 deletions pokemongo_bot/cell_workers/incubate_eggs_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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

self.api.get_inventory()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, use self.bot.get_inventory() - it is cached version of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cached inventory does not represent current egg and incubator status, so I need a fresh version

Copy link
Contributor

Choose a reason for hiding this comment

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

@tstumm it is strange. Cached version updated each tick, so it should contains same content that return self.api.get_inventory()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well seems like you're right. I looked through the code but I didn't think to look at the workers...

response_dict = self.api.call()
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", []):
incubators.append({"id":incubator.get("item_id", -1), "used":False})

Choose a reason for hiding this comment

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

You can check if the incubator is being used here. It will have 'pokemon_id' set if there is an egg in it. 'pokemon_id' matches 'id' from an egg.

if "pokemon_data" in inv_data:
pokemon = inv_data.get("pokemon_data", {})
if pokemon.get("is_egg", False):
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"]:
continue

self.api.use_item_egg_incubator(item_inc=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:
egg["used"] = True
incubator["used"] = True
elif code == 5 or code == 7:
incubator["used"] = True
break
elif code == 6:
egg["used"] = True
elif code == 2:
logger.log('Successfully incubated a ' + str(egg["km"]) + "km egg", 'green')