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

recycle items in cascading #3930

Merged
merged 6 commits into from
Aug 14, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"type": "RecycleItems",
"config": {
"min_empty_space": 15,
"max_balls_keep": 150,
"max_potions_keep": 50,
"max_berries_keep": 70,
"max_revives_keep": 70,
"item_filter": {
"Pokeball": { "keep" : 100 },
"Potion": { "keep" : 10 },
Expand Down Expand Up @@ -164,6 +168,5 @@
"Muk": {},
"Weezing": {},
"Flareon": {}

}
}
100 changes: 93 additions & 7 deletions pokemongo_bot/cell_workers/recycle_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class RecycleItems(BaseTask):
"type": "RecycleItems",
"config": {
"min_empty_space": 6, # 6 by default
"max_balls_keep": 150,
"max_potions_keep": 50,
"max_berries_keep": 70,
"max_revives_keep": 70,
"item_filter": {
"Pokeball": {"keep": 20},
"Greatball": {"keep": 50},
Expand All @@ -44,6 +48,11 @@ class RecycleItems(BaseTask):
def initialize(self):
self.items_filter = self.config.get('item_filter', {})
self.min_empty_space = self.config.get('min_empty_space', None)
self.max_balls_keep = self.config.get('max_balls_keep', None)
self.max_potions_keep = self.config.get('max_potions_keep', None)
self.max_berries_keep = self.config.get('max_berries_keep', None)
self.max_revives_keep = self.config.get('max_revives_keep', None)

self._validate_item_filter()

def _validate_item_filter(self):
Expand Down Expand Up @@ -77,23 +86,100 @@ def work(self):
:return: Returns whether or not the task went well
:rtype: WorkerResult
"""

# TODO: Use new inventory everywhere and then remove this inventory update

# TODO: Use new inventory everywhere and then remove the inventory update
# Updating inventory
inventory.refresh_inventory()

worker_result = WorkerResult.SUCCESS

if self.should_run():

if not (self.max_balls_keep is None):
worker_result = self.recycle_excess_category_max(self.max_balls_keep, [1,2,3,4])
if not (self.max_potions_keep is None):
worker_result = self.recycle_excess_category_max(self.max_potions_keep, [101,102,103,104])
if not (self.max_berries_keep is None):
worker_result = self.recycle_excess_category_max(self.max_berries_keep, [701,702,703,704,705])
if not (self.max_revives_keep is None):
worker_result = self.recycle_excess_category_max(self.max_revives_keep, [201,202])

inventory.refresh_inventory()

for item_in_inventory in inventory.items().all():
amount_to_recycle = self.get_amount_to_recycle(item_in_inventory)

if self.item_should_be_recycled(item_in_inventory):
# Make the bot appears more human
action_delay(self.bot.config.action_wait_min, self.bot.config.action_wait_max)
# If at any recycling process call we got an error, we consider that the result of this task is error too.
if ItemRecycler(self.bot, item_in_inventory, self.get_amount_to_recycle(item_in_inventory)).work() == WorkerResult.ERROR:
worker_result = WorkerResult.ERROR

return worker_result

def recycle_excess_category_max(self, category_max, category_items_list):
"""
Recycle the item which excess the category max
:param category_max:
:param category_items_list:
:return: none:
:rtype: None
"""
worker_result = WorkerResult.SUCCESS
category_inventory = self.get_category_inventory_list(category_items_list)
category_count = 0
for i in category_inventory:
category_count = category_count + i[1]
items_to_recycle = self.get_category_items_to_recycle(category_inventory, category_count, category_max)
for item in items_to_recycle:
action_delay(self.bot.config.action_wait_min, self.bot.config.action_wait_max)
if ItemRecycler(self.bot, inventory.items().get(item[0]), item[1]).work() == WorkerResult.ERROR:
worker_result = WorkerResult.ERROR
return worker_result

def get_category_inventory_list(self, category_inventory):
"""
Returns an array of items with the item id and item count.
:param category_inventory:
:return: array of items within a category:
:rtype: array
"""
x = 0
category_inventory_list = []
for c in category_inventory:
category_inventory_list.append([])
category_inventory_list[x].append(c)
category_inventory_list[x].append(inventory.items().get(c).count)
x = x + 1
return category_inventory_list

def get_category_items_to_recycle(self, category_inventory, category_count, category_max):
"""
Returns an array to be recycle within a category of items with the item id and item count.
:param category_inventory:
:param category_count:
:param category_max:
:return: array of items to be recycle.
:rtype: array
"""
x = 0
items_to_recycle = []
if category_count > self.max_balls_keep:
items_to_be_recycled = category_count - category_max

for item in category_inventory:
if items_to_be_recycled == 0:
break
if items_to_be_recycled >= item[1]:
items_to_recycle.append([])
items_to_recycle[x].append(item[0])
items_to_recycle[x].append(item[1])
else:
items_to_recycle.append([])
items_to_recycle[x].append(item[0])
items_to_recycle[x].append(items_to_be_recycled)
items_to_be_recycled = items_to_be_recycled - items_to_recycle[x][1]
x = x + 1
return items_to_recycle

def item_should_be_recycled(self, item):
"""
Expand All @@ -107,8 +193,8 @@ def item_should_be_recycled(self, item):
def get_amount_to_recycle(self, item):
"""
Determine the amount to recycle accordingly to user config
:param item: Item to determine the amount to recycle.
:return: The amount to recycle
:param item: Item to determine the amount to recycle
:return: The amount to recycle.
:rtype: int
"""
amount_to_keep = self.get_amount_to_keep(item)
Expand Down