Skip to content

Commit

Permalink
RecycleItemWorker implemented (runs on every tick) (#1156)
Browse files Browse the repository at this point in the history
* RecycleItemWorker implemented (runs on every tick)

* moved RecycleItemWorker to a better place

* recycle item worker logging improved

* simplify if in item_inventory_count

* removing extra space
  • Loading branch information
douglascamata authored Jul 27, 2016
1 parent 9320e5c commit dccae1f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
43 changes: 27 additions & 16 deletions 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 CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker
from cell_workers import CatchVisiblePokemonWorker, PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker, RecycleItemsWorker
from cell_workers.utils import distance, get_cellid, encode, i2f
from human_behaviour import sleep
from item_list import Item
Expand Down Expand Up @@ -43,6 +43,7 @@ def start(self):
random.seed()

def take_step(self):
location = self.navigator.take_step()
self.process_cells(work_on_forts=True)

def process_cells(self, work_on_forts=True):
Expand Down Expand Up @@ -171,6 +172,8 @@ def work_on_cell(self, cell, position, work_on_forts=1):
return
self.config.evolve_all = []

RecycleItemsWorker(self).work()

worker = CatchVisiblePokemonWorker(self, cell)
if worker.work() == WorkerResult.RUNNING:
return
Expand Down Expand Up @@ -302,14 +305,6 @@ def _print_character_info(self):

logger.log('')

def drop_item(self, item_id, count):
self.api.recycle_inventory_item(item_id=item_id, count=count)
inventory_req = self.api.call()

# Example of good request response
#{'responses': {'RECYCLE_INVENTORY_ITEM': {'result': 1, 'new_count': 46}}, 'status_code': 1, 'auth_ticket': {'expire_timestamp_ms': 1469306228058L, 'start': '/HycFyfrT4t2yB2Ij+yoi+on778aymMgxY6RQgvrGAfQlNzRuIjpcnDd5dAxmfoTqDQrbz1m2dGqAIhJ+eFapg==', 'end': 'f5NOZ95a843tgzprJo4W7Q=='}, 'request_id': 8145806132888207460L}
return inventory_req

def use_lucky_egg(self):
self.api.use_item_xp_boost(item_id=301)
inventory_req = self.api.call()
Expand Down Expand Up @@ -380,16 +375,32 @@ def item_inventory_count(self, id):
inventory_dict = inventory_req['responses'][
'GET_INVENTORY']['inventory_delta']['inventory_items']

if id == 'all':
return self._all_items_inventory_count(inventory_dict)
else:
return self._item_inventory_count_per_id(id, inventory_dict)

def _item_inventory_count_per_id(self, id, inventory_dict):
item_count = 0

for item in inventory_dict:
try:
if item['inventory_item_data']['item']['item_id'] == int(id):
item_count = item[
'inventory_item_data']['item']['count']
except:
continue
return item_count
item_dict = item.get('inventory_item_data', {}).get('item', {})
item_id = item_dict.get('item_id', False)
item_count = item_dict.get('count', False)
if item_id == int(id) and item_count:
return item_count

def _all_items_inventory_count(self, inventory_dict):
item_count_dict = {}

for item in inventory_dict:
item_dict = item.get('inventory_item_data', {}).get('item', {})
item_id = item_dict.get('item_id', False)
item_count = item_dict.get('count', False)
if item_id and item_count:
item_count_dict[item_id] = item_count

return item_count_dict

def _set_starting_position(self):

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 @@ -6,3 +6,4 @@
from initial_transfer_worker import InitialTransferWorker
from evolve_all_worker import EvolveAllWorker
from catch_visible_pokmeon_worker import CatchVisiblePokemonWorker
from recycle_items_worker import RecycleItemsWorker
53 changes: 53 additions & 0 deletions pokemongo_bot/cell_workers/recycle_items_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pokemongo_bot import logger


class RecycleItemsWorker(object):

def __init__(self, bot):
self.bot = bot
self.api = bot.api
self.config = bot.config
self.item_list = bot.item_list

def work(self):
logger.log('Starting to recycle items...', 'yellow')
item_count_dict = self.bot.item_inventory_count('all')

for item_id, bag_count in item_count_dict.iteritems():
item_name = self.item_list[str(item_id)]
id_filter = self.config.item_filter.get(str(item_id), 0)
if id_filter is not 0:
id_filter_keep = id_filter.get('keep', 20)

bag_count = self.bot.item_inventory_count(item_id)
if str(item_id) in self.config.item_filter and bag_count > id_filter_keep:
items_recycle_count = bag_count - id_filter_keep

response_dict_recycle = self.send_recycle_item_request(
item_id=item_id,
count=items_recycle_count
)

result = response_dict_recycle.get('responses', {}) \
.get('RECYCLE_INVENTORY_ITEM', {}) \
.get('result', 0)

if result == 1: # Request success
message_template = "-- Recycled {}x {} (keeps only {} maximum) "
message = message_template.format(
str(items_recycle_count),
item_name,
str(id_filter_keep)
)
logger.log(message, 'green')
else:
logger.log("-- Failed to recycle " + item_name + "has failed!", 'red')
logger.log('Finished.', 'yellow')

def send_recycle_item_request(self, item_id, count):
self.api.recycle_inventory_item(item_id=item_id, count=count)
inventory_req = self.api.call()

# Example of good request response
#{'responses': {'RECYCLE_INVENTORY_ITEM': {'result': 1, 'new_count': 46}}, 'status_code': 1, 'auth_ticket': {'expire_timestamp_ms': 1469306228058L, 'start': '/HycFyfrT4t2yB2Ij+yoi+on778aymMgxY6RQgvrGAfQlNzRuIjpcnDd5dAxmfoTqDQrbz1m2dGqAIhJ+eFapg==', 'end': 'f5NOZ95a843tgzprJo4W7Q=='}, 'request_id': 8145806132888207460L}
return inventory_req
25 changes: 0 additions & 25 deletions pokemongo_bot/cell_workers/seen_fort_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,7 @@ def work(self):

for item_id, item_count in tmp_count_items.iteritems():
item_name = self.item_list[str(item_id)]

logger.log('- ' + str(item_count) + "x " + item_name + " (Total: " + str(self.bot.item_inventory_count(item_id)) + ")", 'yellow')


# RECYCLING UNWANTED ITEMS
id_filter = self.config.item_filter.get(str(item_id), 0)
if id_filter is not 0:
id_filter_keep = id_filter.get('keep',20)
new_bag_count = self.bot.item_inventory_count(item_id)
if str(item_id) in self.config.item_filter and new_bag_count > id_filter_keep:
#RECYCLE_INVENTORY_ITEM
items_recycle_count = new_bag_count - id_filter_keep
logger.log("-- Recycling " + str(items_recycle_count) + "x " + item_name + " to match filter "+ str(id_filter_keep) +"...", 'green')
response_dict_recycle = self.bot.drop_item(item_id=item_id, count=items_recycle_count)

result = 0
if response_dict_recycle and \
'responses' in response_dict_recycle and \
'RECYCLE_INVENTORY_ITEM' in response_dict_recycle['responses'] and \
'result' in response_dict_recycle['responses']['RECYCLE_INVENTORY_ITEM']:
result = response_dict_recycle['responses']['RECYCLE_INVENTORY_ITEM']['result']

if result is 1: # Request success
logger.log("-- Recycled " + item_name + "!", 'green')
else:
logger.log("-- Recycling " + item_name + "has failed!", 'red')
else:
logger.log("[#] Nothing found.", 'yellow')

Expand Down

0 comments on commit dccae1f

Please sign in to comment.