-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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() | ||
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", []): | ||
if "pokemon_id" not in incubator: | ||
incubators.append({"id":incubator.get("id", -1), "used":False}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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...