Fix: InventoryManager.set() was using outdated items

This commit is contained in:
LmeSzinc 2024-06-21 02:24:03 +08:00
parent 7e14bde267
commit f4afb86d0e

View File

@ -4,10 +4,9 @@ from scipy import signal
from module.base.base import ModuleBase from module.base.base import ModuleBase
from module.base.button import ButtonWrapper from module.base.button import ButtonWrapper
from module.base.decorator import cached_property, del_cached_property from module.base.decorator import cached_property
from module.base.timer import Timer from module.base.timer import Timer
from module.base.utils import Lines, area_center, area_offset, color_similarity_2d from module.base.utils import Lines, area_center, area_offset, color_similarity_2d
from module.exception import ScriptError
from module.logger import logger from module.logger import logger
@ -71,6 +70,8 @@ class InventoryManager:
ERROR_LINES_TOLERANCE = (-10, 10) ERROR_LINES_TOLERANCE = (-10, 10)
COINCIDENT_POINT_ENCOURAGE_DISTANCE = 1. COINCIDENT_POINT_ENCOURAGE_DISTANCE = 1.
MAXIMUM_ITEMS = 30
def __init__(self, main: ModuleBase, inventory: ButtonWrapper): def __init__(self, main: ModuleBase, inventory: ButtonWrapper):
""" """
max_count: expected max count of this inventory page max_count: expected max count of this inventory page
@ -248,7 +249,10 @@ class InventoryManager:
else: else:
self.selected = selected[0] self.selected = selected[0]
logger.info(f'Inventory: {len(self.items)} items, selected {self.selected}') count = len(self.items)
logger.info(f'Inventory: {count} items, selected {self.selected}')
if count > self.MAXIMUM_ITEMS:
logger.warning('Too many inventory items detected')
def get_row_first(self, row=1, first=0) -> InventoryItem | None: def get_row_first(self, row=1, first=0) -> InventoryItem | None:
""" """
@ -292,12 +296,9 @@ class InventoryManager:
def select(self, item, skip_first_screenshot=True): def select(self, item, skip_first_screenshot=True):
logger.info(f'Inventory select {item}') logger.info(f'Inventory select {item}')
if isinstance(item, InventoryItem): if isinstance(item, InventoryItem):
pass loca = item.loca
else: else:
try: loca = item
item = self.items[item]
except KeyError:
raise ScriptError(f'Inventory select {item} but is not in current items')
interval = Timer(2, count=6) interval = Timer(2, count=6)
while 1: while 1:
@ -306,8 +307,16 @@ class InventoryManager:
else: else:
self.main.device.screenshot() self.main.device.screenshot()
self.update()
if len(self.items) > self.MAXIMUM_ITEMS:
continue
try:
item = self.items[loca]
except KeyError:
logger.warning(f'Item {loca} is not in inventory, cannot select')
continue
# End # End
del_cached_property(item, 'is_selected')
if item.is_selected: if item.is_selected:
logger.info('Inventory item selected') logger.info('Inventory item selected')
break break
@ -315,8 +324,7 @@ class InventoryManager:
if interval.reached(): if interval.reached():
self.main.device.click(item) self.main.device.click(item)
interval.reset() interval.reset()
continue
self.update()
def wait_selected(self, skip_first_screenshot=True): def wait_selected(self, skip_first_screenshot=True):
""" """
@ -334,8 +342,10 @@ class InventoryManager:
self.main.device.screenshot() self.main.device.screenshot()
self.update() self.update()
if self.selected is not None:
return True
if timeout.reached(): if timeout.reached():
logger.warning('Wait inventory selected timeout') logger.warning('Wait inventory selected timeout')
return False return False
if len(self.items) > self.MAXIMUM_ITEMS:
continue
if self.selected is not None:
return True