diff --git a/configs/config.json.example b/configs/config.json.example index 838acc1d4b..a3108c9616 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -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 }, @@ -164,6 +168,5 @@ "Muk": {}, "Weezing": {}, "Flareon": {} - } } diff --git a/pokemongo_bot/cell_workers/recycle_items.py b/pokemongo_bot/cell_workers/recycle_items.py index e48dff3cd8..5c29b3c2ce 100644 --- a/pokemongo_bot/cell_workers/recycle_items.py +++ b/pokemongo_bot/cell_workers/recycle_items.py @@ -25,6 +25,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}, @@ -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): @@ -67,7 +76,7 @@ def should_run(self): :return: True if the recycling process should be run; otherwise, False. :rtype: bool """ - if inventory.Items.get_space_left() < (DEFAULT_MIN_EMPTY_SPACE if self.min_empty_space is None else self.min_empty_space): + if inventory.items().get_space_left() < (DEFAULT_MIN_EMPTY_SPACE if self.min_empty_space is None else self.min_empty_space): return True return False @@ -81,8 +90,18 @@ def work(self): # Updating inventory inventory.refresh_inventory() worker_result = WorkerResult.SUCCESS + if self.should_run(): + if not (self.max_balls_keep is None): + self.recycle_excess_category_max(self.max_balls_keep, [1,2,3,4]) + if not (self.max_potions_keep is None): + self.recycle_excess_category_max(self.max_potions_keep, [101,102,103,104]) + if not (self.max_berries_keep is None): + self.recycle_excess_category_max(self.max_potions_keep, [701,702,703,704]) + if not (self.max_revives_keep is None): + self.recycle_excess_category_max(self.max_potions_keep, [201,202]) + # For each user's item in inventory recycle it if needed for item_in_inventory in inventory.items().all(): amount_to_recycle = self.get_amount_to_recycle(item_in_inventory) @@ -93,6 +112,70 @@ def work(self): 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 + """ + category_inventory = self.get_category_inventory_list(inventory, 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 + + def get_category_inventory_list(self, inventory, category_inventory): + """ + Returns an array of items with the item id and item count. + :param inventory: + :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, amount_to_recycle): """