diff --git a/assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png b/assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png new file mode 100644 index 000000000..691c095f6 Binary files /dev/null and b/assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png differ diff --git a/assets/shop/island/Apple Juice.png b/assets/shop/island/Apple Juice.png new file mode 100644 index 000000000..c2b66f98b Binary files /dev/null and b/assets/shop/island/Apple Juice.png differ diff --git a/assets/shop/island/Apple.png b/assets/shop/island/Apple.png new file mode 100644 index 000000000..a65ebcd25 Binary files /dev/null and b/assets/shop/island/Apple.png differ diff --git a/assets/shop/island/Autumn Bouquet.png b/assets/shop/island/Autumn Bouquet.png new file mode 100644 index 000000000..028beb17d Binary files /dev/null and b/assets/shop/island/Autumn Bouquet.png differ diff --git a/assets/shop/island/Autumn Chrysanthemum.png b/assets/shop/island/Autumn Chrysanthemum.png new file mode 100644 index 000000000..150e94d3b Binary files /dev/null and b/assets/shop/island/Autumn Chrysanthemum.png differ diff --git a/assets/shop/island/Avocado.png b/assets/shop/island/Avocado.png new file mode 100644 index 000000000..546f27d42 Binary files /dev/null and b/assets/shop/island/Avocado.png differ diff --git a/assets/shop/island/Baa Baa Sheep Feed.png b/assets/shop/island/Baa Baa Sheep Feed.png new file mode 100644 index 000000000..eb26e3c29 Binary files /dev/null and b/assets/shop/island/Baa Baa Sheep Feed.png differ diff --git a/assets/shop/island/Banana and Mango Juice.png b/assets/shop/island/Banana and Mango Juice.png new file mode 100644 index 000000000..97b58fa25 Binary files /dev/null and b/assets/shop/island/Banana and Mango Juice.png differ diff --git a/assets/shop/island/Banana.png b/assets/shop/island/Banana.png new file mode 100644 index 000000000..5a096de76 Binary files /dev/null and b/assets/shop/island/Banana.png differ diff --git a/assets/shop/island/Bauxite Ore.png b/assets/shop/island/Bauxite Ore.png new file mode 100644 index 000000000..a3a34586c Binary files /dev/null and b/assets/shop/island/Bauxite Ore.png differ diff --git a/assets/shop/island/Berry and Orange Dessert.png b/assets/shop/island/Berry and Orange Dessert.png new file mode 100644 index 000000000..3a293f714 Binary files /dev/null and b/assets/shop/island/Berry and Orange Dessert.png differ diff --git a/assets/shop/island/Cable.png b/assets/shop/island/Cable.png new file mode 100644 index 000000000..1ef653bc1 Binary files /dev/null and b/assets/shop/island/Cable.png differ diff --git a/assets/shop/island/Carrot.png b/assets/shop/island/Carrot.png new file mode 100644 index 000000000..82ba29d50 Binary files /dev/null and b/assets/shop/island/Carrot.png differ diff --git a/assets/shop/island/Cheese.png b/assets/shop/island/Cheese.png new file mode 100644 index 000000000..959d06159 Binary files /dev/null and b/assets/shop/island/Cheese.png differ diff --git a/assets/shop/island/Citrus Coffee.png b/assets/shop/island/Citrus Coffee.png new file mode 100644 index 000000000..f2dcbf06d Binary files /dev/null and b/assets/shop/island/Citrus Coffee.png differ diff --git a/assets/shop/island/Citrus Fruit.png b/assets/shop/island/Citrus Fruit.png new file mode 100644 index 000000000..d9c9a1f84 Binary files /dev/null and b/assets/shop/island/Citrus Fruit.png differ diff --git a/assets/shop/island/Classic Tofu Combo.png b/assets/shop/island/Classic Tofu Combo.png new file mode 100644 index 000000000..3ab06b013 Binary files /dev/null and b/assets/shop/island/Classic Tofu Combo.png differ diff --git a/assets/shop/island/Clucky Clucky Bird Feed.png b/assets/shop/island/Clucky Clucky Bird Feed.png new file mode 100644 index 000000000..6767ace19 Binary files /dev/null and b/assets/shop/island/Clucky Clucky Bird Feed.png differ diff --git a/assets/shop/island/Coal-Roasted Skewer.png b/assets/shop/island/Coal-Roasted Skewer.png new file mode 100644 index 000000000..6b5c90004 Binary files /dev/null and b/assets/shop/island/Coal-Roasted Skewer.png differ diff --git a/assets/shop/island/Coal.png b/assets/shop/island/Coal.png new file mode 100644 index 000000000..fddcd2e02 Binary files /dev/null and b/assets/shop/island/Coal.png differ diff --git a/assets/shop/island/Coffee Beans.png b/assets/shop/island/Coffee Beans.png new file mode 100644 index 000000000..69c121e7c Binary files /dev/null and b/assets/shop/island/Coffee Beans.png differ diff --git a/assets/shop/island/Colorful Fruit Paradise.png b/assets/shop/island/Colorful Fruit Paradise.png new file mode 100644 index 000000000..611733773 Binary files /dev/null and b/assets/shop/island/Colorful Fruit Paradise.png differ diff --git a/assets/shop/island/Copper Ore.png b/assets/shop/island/Copper Ore.png new file mode 100644 index 000000000..f694ced55 Binary files /dev/null and b/assets/shop/island/Copper Ore.png differ diff --git a/assets/shop/island/Corn Cup.png b/assets/shop/island/Corn Cup.png new file mode 100644 index 000000000..6023cbdda Binary files /dev/null and b/assets/shop/island/Corn Cup.png differ diff --git a/assets/shop/island/Corn.png b/assets/shop/island/Corn.png new file mode 100644 index 000000000..e9e42fc65 Binary files /dev/null and b/assets/shop/island/Corn.png differ diff --git a/assets/shop/island/Cotton.png b/assets/shop/island/Cotton.png new file mode 100644 index 000000000..65d2df179 Binary files /dev/null and b/assets/shop/island/Cotton.png differ diff --git a/assets/shop/island/Eggs.png b/assets/shop/island/Eggs.png new file mode 100644 index 000000000..77c378888 Binary files /dev/null and b/assets/shop/island/Eggs.png differ diff --git a/assets/shop/island/Elegant Wood.png b/assets/shop/island/Elegant Wood.png new file mode 100644 index 000000000..b61b9eb99 Binary files /dev/null and b/assets/shop/island/Elegant Wood.png differ diff --git a/assets/shop/island/Filing Cabinet.png b/assets/shop/island/Filing Cabinet.png new file mode 100644 index 000000000..7fff7bfe3 Binary files /dev/null and b/assets/shop/island/Filing Cabinet.png differ diff --git a/assets/shop/island/Flax.png b/assets/shop/island/Flax.png new file mode 100644 index 000000000..7196f0ec4 Binary files /dev/null and b/assets/shop/island/Flax.png differ diff --git a/assets/shop/island/Floral and Fruity.png b/assets/shop/island/Floral and Fruity.png new file mode 100644 index 000000000..eaacceb08 Binary files /dev/null and b/assets/shop/island/Floral and Fruity.png differ diff --git a/assets/shop/island/Flour.png b/assets/shop/island/Flour.png new file mode 100644 index 000000000..71d6d432d Binary files /dev/null and b/assets/shop/island/Flour.png differ diff --git a/assets/shop/island/Fresh Honey.png b/assets/shop/island/Fresh Honey.png new file mode 100644 index 000000000..49affe215 Binary files /dev/null and b/assets/shop/island/Fresh Honey.png differ diff --git a/assets/shop/island/Fresh Meat.png b/assets/shop/island/Fresh Meat.png new file mode 100644 index 000000000..d01f63cc6 Binary files /dev/null and b/assets/shop/island/Fresh Meat.png differ diff --git a/assets/shop/island/Grass.png b/assets/shop/island/Grass.png new file mode 100644 index 000000000..cc478b861 Binary files /dev/null and b/assets/shop/island/Grass.png differ diff --git a/assets/shop/island/Hearty Meal.png b/assets/shop/island/Hearty Meal.png new file mode 100644 index 000000000..5f0351771 Binary files /dev/null and b/assets/shop/island/Hearty Meal.png differ diff --git a/assets/shop/island/Honey and Lemon Water.png b/assets/shop/island/Honey and Lemon Water.png new file mode 100644 index 000000000..b4e7faae8 Binary files /dev/null and b/assets/shop/island/Honey and Lemon Water.png differ diff --git a/assets/shop/island/Iced Coffee.png b/assets/shop/island/Iced Coffee.png new file mode 100644 index 000000000..6ed55eae3 Binary files /dev/null and b/assets/shop/island/Iced Coffee.png differ diff --git a/assets/shop/island/Iron Ore.png b/assets/shop/island/Iron Ore.png new file mode 100644 index 000000000..e8567c24c Binary files /dev/null and b/assets/shop/island/Iron Ore.png differ diff --git a/assets/shop/island/Lavender Tea.png b/assets/shop/island/Lavender Tea.png new file mode 100644 index 000000000..abaeac31e Binary files /dev/null and b/assets/shop/island/Lavender Tea.png differ diff --git a/assets/shop/island/Lavender.png b/assets/shop/island/Lavender.png new file mode 100644 index 000000000..292e3ed4a Binary files /dev/null and b/assets/shop/island/Lavender.png differ diff --git a/assets/shop/island/Leather.png b/assets/shop/island/Leather.png new file mode 100644 index 000000000..9c2b01390 Binary files /dev/null and b/assets/shop/island/Leather.png differ diff --git a/assets/shop/island/Lemon.png b/assets/shop/island/Lemon.png new file mode 100644 index 000000000..5c3fb61e4 Binary files /dev/null and b/assets/shop/island/Lemon.png differ diff --git a/assets/shop/island/Mango.png b/assets/shop/island/Mango.png new file mode 100644 index 000000000..920d225ad Binary files /dev/null and b/assets/shop/island/Mango.png differ diff --git a/assets/shop/island/Matsutake.png b/assets/shop/island/Matsutake.png new file mode 100644 index 000000000..05a4385b3 Binary files /dev/null and b/assets/shop/island/Matsutake.png differ diff --git a/assets/shop/island/Milk.png b/assets/shop/island/Milk.png new file mode 100644 index 000000000..55e1a227a Binary files /dev/null and b/assets/shop/island/Milk.png differ diff --git a/assets/shop/island/Moo Moo Cow Feed.png b/assets/shop/island/Moo Moo Cow Feed.png new file mode 100644 index 000000000..4e0d522e9 Binary files /dev/null and b/assets/shop/island/Moo Moo Cow Feed.png differ diff --git a/assets/shop/island/Nails.png b/assets/shop/island/Nails.png new file mode 100644 index 000000000..4ff6b07d5 Binary files /dev/null and b/assets/shop/island/Nails.png differ diff --git a/assets/shop/island/Napa Cabbage.png b/assets/shop/island/Napa Cabbage.png new file mode 100644 index 000000000..c8dd5534a Binary files /dev/null and b/assets/shop/island/Napa Cabbage.png differ diff --git a/assets/shop/island/Oinky Oinky Pig Feed.png b/assets/shop/island/Oinky Oinky Pig Feed.png new file mode 100644 index 000000000..9986e5365 Binary files /dev/null and b/assets/shop/island/Oinky Oinky Pig Feed.png differ diff --git a/assets/shop/island/Omelette.png b/assets/shop/island/Omelette.png new file mode 100644 index 000000000..287e3f42c Binary files /dev/null and b/assets/shop/island/Omelette.png differ diff --git a/assets/shop/island/Omurice.png b/assets/shop/island/Omurice.png new file mode 100644 index 000000000..c415e92f0 Binary files /dev/null and b/assets/shop/island/Omurice.png differ diff --git a/assets/shop/island/Onion.png b/assets/shop/island/Onion.png new file mode 100644 index 000000000..6cb01a67d Binary files /dev/null and b/assets/shop/island/Onion.png differ diff --git a/assets/shop/island/Orange Pie.png b/assets/shop/island/Orange Pie.png new file mode 100644 index 000000000..ac8f260b0 Binary files /dev/null and b/assets/shop/island/Orange Pie.png differ diff --git a/assets/shop/island/Peanut Oil.png b/assets/shop/island/Peanut Oil.png new file mode 100644 index 000000000..a94fb0596 Binary files /dev/null and b/assets/shop/island/Peanut Oil.png differ diff --git a/assets/shop/island/Peanuts.png b/assets/shop/island/Peanuts.png new file mode 100644 index 000000000..678b407be Binary files /dev/null and b/assets/shop/island/Peanuts.png differ diff --git a/assets/shop/island/Pelt.png b/assets/shop/island/Pelt.png new file mode 100644 index 000000000..f0e6d6ecb Binary files /dev/null and b/assets/shop/island/Pelt.png differ diff --git a/assets/shop/island/Potato.png b/assets/shop/island/Potato.png new file mode 100644 index 000000000..b0d60d468 Binary files /dev/null and b/assets/shop/island/Potato.png differ diff --git a/assets/shop/island/Poultry.png b/assets/shop/island/Poultry.png new file mode 100644 index 000000000..68178cc82 Binary files /dev/null and b/assets/shop/island/Poultry.png differ diff --git a/assets/shop/island/Premium Wood.png b/assets/shop/island/Premium Wood.png new file mode 100644 index 000000000..24fe8f4b5 Binary files /dev/null and b/assets/shop/island/Premium Wood.png differ diff --git a/assets/shop/island/Raw Timber.png b/assets/shop/island/Raw Timber.png new file mode 100644 index 000000000..427bd19a4 Binary files /dev/null and b/assets/shop/island/Raw Timber.png differ diff --git a/assets/shop/island/Reed Flowers.png b/assets/shop/island/Reed Flowers.png new file mode 100644 index 000000000..3fdf671e3 Binary files /dev/null and b/assets/shop/island/Reed Flowers.png differ diff --git a/assets/shop/island/Rice.png b/assets/shop/island/Rice.png new file mode 100644 index 000000000..6ef7feb01 Binary files /dev/null and b/assets/shop/island/Rice.png differ diff --git a/assets/shop/island/Rubber.png b/assets/shop/island/Rubber.png new file mode 100644 index 000000000..535d70916 Binary files /dev/null and b/assets/shop/island/Rubber.png differ diff --git a/assets/shop/island/Shoes.png b/assets/shop/island/Shoes.png new file mode 100644 index 000000000..289a71a45 Binary files /dev/null and b/assets/shop/island/Shoes.png differ diff --git a/assets/shop/island/Silver Ore.png b/assets/shop/island/Silver Ore.png new file mode 100644 index 000000000..b5912a066 Binary files /dev/null and b/assets/shop/island/Silver Ore.png differ diff --git a/assets/shop/island/Soy Beans.png b/assets/shop/island/Soy Beans.png new file mode 100644 index 000000000..51d28e0dd Binary files /dev/null and b/assets/shop/island/Soy Beans.png differ diff --git a/assets/shop/island/Steak Bowl.png b/assets/shop/island/Steak Bowl.png new file mode 100644 index 000000000..a8ef16107 Binary files /dev/null and b/assets/shop/island/Steak Bowl.png differ diff --git a/assets/shop/island/Sticky Rice with Mango.png b/assets/shop/island/Sticky Rice with Mango.png new file mode 100644 index 000000000..01e80201f Binary files /dev/null and b/assets/shop/island/Sticky Rice with Mango.png differ diff --git a/assets/shop/island/Stir-Fried Chicken.png b/assets/shop/island/Stir-Fried Chicken.png new file mode 100644 index 000000000..27624ad83 Binary files /dev/null and b/assets/shop/island/Stir-Fried Chicken.png differ diff --git a/assets/shop/island/Strawberries.png b/assets/shop/island/Strawberries.png new file mode 100644 index 000000000..15112854c Binary files /dev/null and b/assets/shop/island/Strawberries.png differ diff --git a/assets/shop/island/Strawberry Charlotte.png b/assets/shop/island/Strawberry Charlotte.png new file mode 100644 index 000000000..8fe196d8a Binary files /dev/null and b/assets/shop/island/Strawberry Charlotte.png differ diff --git a/assets/shop/island/Strawberry Honey Frappé.png b/assets/shop/island/Strawberry Honey Frappé.png new file mode 100644 index 000000000..c5ee9f248 Binary files /dev/null and b/assets/shop/island/Strawberry Honey Frappé.png differ diff --git a/assets/shop/island/Strawberry Lemon Drink.png b/assets/shop/island/Strawberry Lemon Drink.png new file mode 100644 index 000000000..2f42f4ec8 Binary files /dev/null and b/assets/shop/island/Strawberry Lemon Drink.png differ diff --git a/assets/shop/island/Succulently Sweet.png b/assets/shop/island/Succulently Sweet.png new file mode 100644 index 000000000..15a138b23 Binary files /dev/null and b/assets/shop/island/Succulently Sweet.png differ diff --git a/assets/shop/island/Sulfur.png b/assets/shop/island/Sulfur.png new file mode 100644 index 000000000..1fafeda06 Binary files /dev/null and b/assets/shop/island/Sulfur.png differ diff --git a/assets/shop/island/Sunny Honey.png b/assets/shop/island/Sunny Honey.png new file mode 100644 index 000000000..f8e4071e9 Binary files /dev/null and b/assets/shop/island/Sunny Honey.png differ diff --git a/assets/shop/island/Tea Leaves.png b/assets/shop/island/Tea Leaves.png new file mode 100644 index 000000000..c68735f4e Binary files /dev/null and b/assets/shop/island/Tea Leaves.png differ diff --git a/assets/shop/island/Tofu with Minced Meat.png b/assets/shop/island/Tofu with Minced Meat.png new file mode 100644 index 000000000..796154911 Binary files /dev/null and b/assets/shop/island/Tofu with Minced Meat.png differ diff --git a/assets/shop/island/Tofu.png b/assets/shop/island/Tofu.png new file mode 100644 index 000000000..d1ad219bb Binary files /dev/null and b/assets/shop/island/Tofu.png differ diff --git a/assets/shop/island/Utensils.png b/assets/shop/island/Utensils.png new file mode 100644 index 000000000..8be2b5b5f Binary files /dev/null and b/assets/shop/island/Utensils.png differ diff --git a/assets/shop/island/Wheat.png b/assets/shop/island/Wheat.png new file mode 100644 index 000000000..23a0c23e7 Binary files /dev/null and b/assets/shop/island/Wheat.png differ diff --git a/assets/shop/island/Wool.png b/assets/shop/island/Wool.png new file mode 100644 index 000000000..7943a116b Binary files /dev/null and b/assets/shop/island/Wool.png differ diff --git a/assets/shop/island/Workable Wood.png b/assets/shop/island/Workable Wood.png new file mode 100644 index 000000000..eb559ff54 Binary files /dev/null and b/assets/shop/island/Workable Wood.png differ diff --git a/module/island/assets.py b/module/island/assets.py index cef0aea60..9c41913c1 100644 --- a/module/island/assets.py +++ b/module/island/assets.py @@ -5,5 +5,6 @@ from module.base.template import Template # Don't modify it manually. ISLAND_SEASON_TASK_SCROLL_AREA = Button(area={'cn': (1236, 171, 1237, 604), 'en': (1236, 171, 1237, 604), 'jp': (1236, 171, 1237, 604), 'tw': (1236, 171, 1237, 604)}, color={'cn': (186, 192, 191), 'en': (186, 192, 191), 'jp': (186, 192, 191), 'tw': (186, 192, 191)}, button={'cn': (1236, 171, 1237, 604), 'en': (1236, 171, 1237, 604), 'jp': (1236, 171, 1237, 604), 'tw': (1236, 171, 1237, 604)}, file={'cn': './assets/cn/island/ISLAND_SEASON_TASK_SCROLL_AREA.png', 'en': './assets/cn/island/ISLAND_SEASON_TASK_SCROLL_AREA.png', 'jp': './assets/cn/island/ISLAND_SEASON_TASK_SCROLL_AREA.png', 'tw': './assets/cn/island/ISLAND_SEASON_TASK_SCROLL_AREA.png'}) +ISLAND_STORAGE_SCROLL_AREA = Button(area={'cn': (1149, 135, 1153, 547), 'en': (1149, 135, 1153, 547), 'jp': (1149, 135, 1153, 547), 'tw': (1149, 135, 1153, 547)}, color={'cn': (216, 218, 218), 'en': (216, 218, 218), 'jp': (216, 218, 218), 'tw': (216, 218, 218)}, button={'cn': (1149, 135, 1153, 547), 'en': (1149, 135, 1153, 547), 'jp': (1149, 135, 1153, 547), 'tw': (1149, 135, 1153, 547)}, file={'cn': './assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png', 'en': './assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png', 'jp': './assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png', 'tw': './assets/cn/island/ISLAND_STORAGE_SCROLL_AREA.png'}) TEMPLATE_ISLAND_SEASON_TASK_ICON = Template(file={'cn': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_ICON.png', 'en': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_ICON.png', 'jp': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_ICON.png', 'tw': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_ICON.png'}) TEMPLATE_ISLAND_SEASON_TASK_OBTAINED = Template(file={'cn': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_OBTAINED.png', 'en': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_OBTAINED.png', 'jp': './assets/jp/island/TEMPLATE_ISLAND_SEASON_TASK_OBTAINED.png', 'tw': './assets/cn/island/TEMPLATE_ISLAND_SEASON_TASK_OBTAINED.png'}) diff --git a/module/island/storage.py b/module/island/storage.py new file mode 100644 index 000000000..2c28df54d --- /dev/null +++ b/module/island/storage.py @@ -0,0 +1,301 @@ +import cv2 +import numpy as np + +from module.base.button import ButtonGrid +from module.base.decorator import cached_property, del_cached_property +from module.base.timer import Timer +from module.base.utils import color_similar, color_similarity_2d, crop, rgb2gray, xywh2xyxy +from module.island.assets import * +from module.island.data import DIC_ISLAND_ITEM +from module.island.ui import IslandUI, ISLAND_STORAGE_SCROLL +from module.logger import logger +from module.map_detection.utils import Points +from module.ocr.ocr import Digit +from module.statistics.item import Item, ItemGrid +from module.ui.page import page_island_storage + + +AMOUNT_OCR = Digit([], lang='cnocr', letter=(218, 218, 218), name='Amount_ocr') + + +class StorageItem(Item): + def predict_valid(self): + return np.mean(rgb2gray(self.image) > 230) < 0.9 + + +class StorageItemGrid(ItemGrid): + item_class = StorageItem + + def match_template(self, image, similarity=None): + """ + Match templates, try most frequent hit templates first. + + Args: + image (np.ndarray): + similarity (float): + + Returns: + str: Template name. + """ + if similarity is None: + similarity = self.similarity + color = cv2.mean(crop(image, self.template_area))[:3] + # Match frequently hit templates first + names = np.array(list(self.templates.keys()))[np.argsort(list(self.templates_hit.values()))][::-1] + # Match known templates first + names = [name for name in names if not name.isdigit()] + [name for name in names if name.isdigit()] + for name in names: + # threshold=5 to match items in same shape but with different colors + if color_similar(color1=color, color2=self.colors[name], threshold=5): + res = cv2.matchTemplate(image, self.templates[name], cv2.TM_CCOEFF_NORMED) + _, sim, _, _ = cv2.minMaxLoc(res) + if sim > similarity: + self.templates_hit[name] += 1 + return name + + self.next_template_index += 1 + name = str(self.next_template_index) + logger.info(f'New template: {name}') + image = crop(image, self.template_area) + self.colors[name] = cv2.mean(image)[:3] + self.templates[name] = image + self.templates_hit[name] = self.templates_hit.get(name, 0) + 1 + return name + + @staticmethod + def item_id_parse(string): + for key, value in DIC_ISLAND_ITEM.items(): + if string == value['name']['en']: + return key + logger.warning(f'Unknown item name: {string}') + return None + + def predict(self, image, name=True, amount=True, cost=False, price=False, tag=False): + super().predict(image, name, amount, cost, price, tag) + items = [] + for item in self.items: + item.item_id = self.item_id_parse(item.name) + if item.item_id is not None: + items.append(item) + self.items = items + return self.items + + +class IslandStorage(IslandUI): + def _get_bars(self): + """ + Returns: + np.array: [[x1, y1], [x2, y2]], location of the item name icon upper left corner. + """ + area = (299, 135, 1116, 574) + image = self.image_crop(area, copy=True) + gray = color_similarity_2d(image, color=(116, 116, 118)) + cv2.inRange(gray, 250, 255, dst=gray) + bars = [] + contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + for cont in contours: + rect = cv2.boundingRect(cv2.convexHull(cont).astype(np.float32)) + rect = xywh2xyxy(rect) + # Check max_height + if rect[3] - rect[1] < 10: + continue + # Check item grid should be in the area + if rect[1] < 105: + continue + bars.append(rect) + bars = Points([(0., b[1]) for b in bars]).group(threshold=5) + logger.attr('Items_icon', len(bars)) + return bars + + def wait_until_bar_appear(self, skip_first_screenshot=True): + """ + After entering island storage page, + items are not loaded that fast, + wait until any bar icon appears + """ + timeout = Timer(1, count=3).start() + while 1: + if skip_first_screenshot: + skip_first_screenshot = False + else: + self.device.screenshot() + + bars = self._get_bars() + + if timeout.reached(): + break + if len(bars): + break + + @cached_property + def storage_grid(self): + return self.storage_bar_grid() + + def storage_bar_grid(self): + """ + Returns: + ButtonGrid: + """ + bars = self._get_bars() + count = len(bars) + if count == 0: + logger.warning('Unable to find bar icon, assume task list is at top') + origin_y = 157 + delta_y = 167 + row = 2 + elif count == 1: + y_list = bars[:, 1] + # -105 to adjust the bar position to grid position + origin_y = y_list[0] - 105 + 135 + delta_y = 167 + row = 1 + elif count == 2: + y_list = bars[:, 1] + origin_y = min(y_list) - 105 + 135 + delta_y = abs(y_list[1] - y_list[0]) + row = 2 + else: + logger.warning(f'Too many bars found ({count}), assume max rows') + y_list = bars[:, 1] + origin_y = min(y_list) - 105 + 135 + delta_y = abs(y_list[1] - y_list[0]) + row = 2 + storage_grid = ButtonGrid( + origin=(303, origin_y), delta=(142, delta_y), + button_shape=(102, 102), grid_shape=(6, row), + name='STORAGE_ITEM_GRID' + ) + return storage_grid + + storage_template_folder = './assets/shop/island' + + @cached_property + def storage_items(self): + """ + Returns: + ItemGrid: + """ + storage_grid = self.storage_grid + storage_items = StorageItemGrid( + storage_grid, + templates={}, + template_area=(0, 15, 96, 80), + amount_area=(36, 85, 95, 100), + ) + storage_items.load_template_folder(self.storage_template_folder) + storage_items.amount_ocr = AMOUNT_OCR + return storage_items + + def storage_has_loaded(self, items): + """ + Returns: + bool + """ + return any(bool(item.amount) for item in items) + + def storage_get_items(self, skip_first_screenshot=True): + """ + Args: + skip_first_screenshot (bool): + + Returns: + list[Item]: + """ + storage_items = self.storage_items + if storage_items is None: + logger.warning('Expected type \'StorageItemGrid\' but was None') + return [] + + # Loop on predict to ensure items + # have loaded and can accurately + # be read + record = 0 + timeout = Timer(3, count=9).start() + while 1: + if skip_first_screenshot: + skip_first_screenshot = False + else: + self.device.screenshot() + + if self.config.SHOP_EXTRACT_TEMPLATE: + if self.storage_template_folder: + logger.info(f'Extract item templates to {self.storage_template_folder}') + storage_items.extract_template(self.device.image, self.storage_template_folder) + else: + logger.warning('SHOP_EXTRACT_TEMPLATE enabled but shop_template_folder is not set, skip extracting') + + storage_items.predict(self.device.image) + + if timeout.reached(): + logger.warning('Items loading timeout; continue and assumed has loaded') + break + + # Check unloaded items, because AL loads items too slow. + items = storage_items.items + known = len([item for item in items if item.is_known_item]) + logger.attr('Item detected', known) + if known == 0 or known != record: + record = known + continue + else: + record = known + + # End + if self.storage_has_loaded(items): + break + + # Log final result on predicted items + items = storage_items.items + grids = storage_items.grids + if len(items): + min_row = grids[0, 0].area[1] + row = [str(item) for item in items if item.button[1] == min_row] + logger.info(f'Storage row 1: {row}') + row = [str(item) for item in items if item.button[1] != min_row] + logger.info(f'Storage row 2: {row}') + return items + else: + logger.info('No storage items found') + return [] + + def scan_all(self): + """ + Scans all items on the island storage page. + + Returns: + dict: {item_id: amount} + """ + logger.hr('Scanning storage items', level=2) + ISLAND_STORAGE_SCROLL.set_top(main=self) + self.wait_until_bar_appear() + items_dict = {} + while 1: + items = self.storage_get_items() + for item in items: + if not items_dict.get(item.item_id): + items_dict[item.item_id] = item.amount + + if ISLAND_STORAGE_SCROLL.at_bottom(main=self): + logger.info('Scroll bar reached end, stop') + break + else: + ISLAND_STORAGE_SCROLL.next_page(main=self, page=0.66) + self.device.click_record.pop() + del_cached_property(self, 'storage_grid') + del_cached_property(self, 'storage_items') + continue + return items_dict + + def run(self): + """ + Pages: + in: Any page + out: page_island + + Returns: + dict: {item_id: amount} + """ + self.ui_ensure(page_island_storage) + self.island_storage_side_navbar_ensure(upper=1) + result = self.scan_all() + return result diff --git a/module/island/ui.py b/module/island/ui.py index 7da131b8b..0d4fdeb54 100644 --- a/module/island/ui.py +++ b/module/island/ui.py @@ -2,7 +2,7 @@ from module.base.button import ButtonGrid from module.base.decorator import cached_property from module.island.assets import * from module.ui.navbar import Navbar -from module.ui.scroll import Scroll +from module.ui.scroll import AdaptiveScroll, Scroll from module.ui.ui import UI @@ -12,6 +12,12 @@ ISLAND_SEASON_TASK_SCROLL = Scroll( name="ISLAND_SEASON_TASK_SCROLL" ) +ISLAND_STORAGE_SCROLL = AdaptiveScroll( + ISLAND_STORAGE_SCROLL_AREA.button, + background=1, + name="ISLAND_STORAGE_SCROLL" +) + class IslandUI(UI): def ui_additional(self, get_ship=True): @@ -38,6 +44,25 @@ class IslandUI(UI): active_count=500, inactive_count=500) + @cached_property + def _island_storage_side_navbar(self): + """ + 3 options: + material, + consumable, + special + """ + island_storage_side_navbar = ButtonGrid( + origin=(164, 154), delta=(0, 106.5), + button_shape=(64, 64), grid_shape=(1, 3), + name='ISLAND_STORAGE_SIDE_NAVBAR' + ) + return Navbar(grids=island_storage_side_navbar, + active_color=(57, 189, 255), + inactive_color=(57, 58, 64), + active_count=500, + inactive_count=500) + def island_season_bottom_navbar_ensure(self, left=None, right=None): """ Args: @@ -56,4 +81,20 @@ class IslandUI(UI): """ if self._island_season_bottom_navbar.set(self, left=left, right=right): return True - return False \ No newline at end of file + return False + + def island_storage_side_navbar_ensure(self, upper=None, bottom=None): + """ + Args: + upper (int): + 1 for material, + 2 for consumable, + 3 for special + bottom (int): + 1 for special, + 2 for consumable, + 3 for material + """ + if self._island_storage_side_navbar.set(self, upper=upper, bottom=bottom): + return True + return False