Skip to content

Commit

Permalink
incubate using infinite first, then breakable
Browse files Browse the repository at this point in the history
  • Loading branch information
rawgni committed Aug 28, 2016
1 parent 6c9f865 commit 18929ef
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 42 deletions.
76 changes: 35 additions & 41 deletions pokemongo_bot/cell_workers/incubate_eggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class IncubateEggs(BaseTask):

def initialize(self):
self.next_update = None
self.ready_incubators_breakable = []
self.ready_incubators_infinite = []
self.ready_breakable_incubators = []
self.ready_infinite_incubators = []
self.used_incubators = []
self.eggs = []
self.km_walked = 0
Expand Down Expand Up @@ -49,39 +49,33 @@ def work(self):

IncubateEggs.last_km_walked = self.km_walked

sorting = self.breakable_longer_eggs_first
self.eggs.sort(key=lambda x: x.get("km"), reverse=sorting)
if self.ready_incubators_breakable:
self._apply_incubators('breakable')
# if there is a ready infinite incubator
if self.ready_infinite_incubators:
# get available eggs
eggs = self._filter_sort_eggs(self.infinite_incubator,
self.infinite_longer_eggs_first)
self._apply_incubators(eggs, self.ready_infinite_incubators)

sorting = self.infinite_longer_eggs_first
self.eggs.sort(key=lambda x: x.get("km"), reverse=sorting)
if self.ready_incubators_infinite:
self._apply_incubators('infinite')
if self.ready_breakable_incubators:
# get available eggs
eggs = self._filter_sort_eggs(self.infinite_incubator,
self.infinite_longer_eggs_first)
self._apply_incubators(eggs, self.ready_infinite_incubators)

def _apply_incubators(self, type_of_incubator):
if type_of_incubator == 'breakable':
temp_ready_incubators = self.ready_incubators_breakable
elif type_of_incubator == 'infinite':
temp_ready_incubators = self.ready_incubators_infinite

for incubator in temp_ready_incubators:
if incubator.get('used', False):
continue
for egg in self.eggs:
if egg["used"] or egg["km"] == -1:
continue

km = int(egg["km"])
def _filter_sort_eggs(self, allowed, sorting):
eligible_eggs = filter(lambda egg: int(egg["km"]) in allowed, self.eggs)
eligible_eggs.sort(key=lambda egg: egg["km"], reverse=sorting)

# test if the incubator is of type breakable
if incubator.get('uses_remaining') is not None:
if km not in self.breakable_incubator:
continue
# test if the incubator is of type infinite
else:
if km not in self.infinite_incubator:
continue
return eligible_eggs


def _apply_incubators(self, available_eggs, available_incubators):

for incubator in available_incubators:
for egg in available_eggs:
if egg["used"] or egg["km"] == -1:
continue

self.emit_event(
'incubate_try',
Expand Down Expand Up @@ -131,8 +125,8 @@ def _check_inventory(self, lookup_ids=[]):
matched_pokemon = []
temp_eggs = []
temp_used_incubators = []
temp_ready_incubators_breakable = []
temp_ready_incubators_infinite = []
temp_ready_breakable_incubators = []
temp_ready_infinite_incubators = []
inv = reduce(
dict.__getitem__,
["responses", "GET_INVENTORY", "inventory_delta", "inventory_items"],
Expand All @@ -142,8 +136,8 @@ def _check_inventory(self, lookup_ids=[]):
inv_data = inv_data.get("inventory_item_data", {})
if "egg_incubators" in inv_data:
temp_used_incubators = []
temp_ready_incubators_breakable = []
temp_ready_incubators_infinite = []
temp_ready_breakable_incubators = []
temp_ready_infinite_incubators = []
incubators = inv_data.get("egg_incubators", {}).get("egg_incubator",[])
if isinstance(incubators, basestring): # checking for old response
incubators = [incubators]
Expand All @@ -158,11 +152,11 @@ def _check_inventory(self, lookup_ids=[]):
})
else:
if incubator.get('uses_remaining') is not None:
temp_ready_incubators_breakable.append({
temp_ready_breakable_incubators.append({
"id": incubator.get('id', -1)
})
else:
temp_ready_incubators_infinite.append({
temp_ready_infinite_incubators.append({
"id": incubator.get('id', -1)
})
continue
Expand All @@ -187,10 +181,10 @@ def _check_inventory(self, lookup_ids=[]):
self.km_walked = inv_data.get("player_stats", {}).get("km_walked", 0)
if temp_used_incubators:
self.used_incubators = temp_used_incubators
if temp_ready_incubators_breakable:
self.ready_incubators_breakable = temp_ready_incubators_breakable
if temp_ready_incubators_infinite:
self.ready_incubators_infinite = temp_ready_incubators_infinite
if temp_ready_breakable_incubators:
self.ready_breakable_incubators = temp_ready_breakable_incubators
if temp_ready_infinite_incubators:
self.ready_infinite_incubators = temp_ready_infinite_incubators
if temp_eggs:
self.eggs = temp_eggs
return matched_pokemon
Expand Down
58 changes: 58 additions & 0 deletions pokemongo_bot/test/incubate_eggs_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest
from mock import patch
from pokemongo_bot.cell_workers.incubate_eggs import IncubateEggs


class IncubateEggsTestCase(unittest.TestCase):

@patch('pokemongo_bot.PokemonGoBot')
def testFilterAndSort_AllowNone(self, mock_pokemongo_bot):
incubate_eggs = IncubateEggs(mock_pokemongo_bot, {})

incubate_eggs.eggs = [{"km": 2.0}, {"km": 5.0}, {"km": 5.0}]

allowed = []
sorting = True

result = incubate_eggs._filter_sort_eggs(allowed, sorting)
self.assertEqual([], result)


@patch('pokemongo_bot.PokemonGoBot')
def testFilterAndSort_AllowSome(self, mock_pokemongo_bot):
incubate_eggs = IncubateEggs(mock_pokemongo_bot, {})

incubate_eggs.eggs = [{"km": 5.0}, {"km": 2.0}, {"km": 5.0}, {"km": 10.0}]

allowed = [2, 10]
sorting = True

result = incubate_eggs._filter_sort_eggs(allowed, sorting)
self.assertEqual([{"km": 10.0}, {"km": 2.0}], result)


@patch('pokemongo_bot.PokemonGoBot')
def testFilterAndSort_AllowSomeNoReverseSort(self, mock_pokemongo_bot):
incubate_eggs = IncubateEggs(mock_pokemongo_bot, {})

incubate_eggs.eggs = [{"km": 5.0}, {"km": 2.0}, {"km": 5.0}, {"km": 10.0}]

allowed = [2, 10]
sorting = False

result = incubate_eggs._filter_sort_eggs(allowed, sorting)
self.assertEqual([{"km": 2.0}, {"km": 10.0}], result)



@patch('pokemongo_bot.PokemonGoBot')
def testFilterAndSort_AllowAll(self, mock_pokemongo_bot):
incubate_eggs = IncubateEggs(mock_pokemongo_bot, {})

incubate_eggs.eggs = [{"km": 5.0}, {"km": 2.0}, {"km": 5.0}]

allowed = [2, 5, 10]
sorting = True

result = incubate_eggs._filter_sort_eggs(allowed, sorting)
self.assertEqual([{"km": 5.0}, {"km": 5.0}, {"km": 2.0}], result)
2 changes: 1 addition & 1 deletion web
Submodule web updated from 607397 to 6ba560

0 comments on commit 18929ef

Please sign in to comment.