diff --git a/clashroyalebuildabot/detectors/screen_detector.py b/clashroyalebuildabot/detectors/screen_detector.py index 8082135..723e886 100644 --- a/clashroyalebuildabot/detectors/screen_detector.py +++ b/clashroyalebuildabot/detectors/screen_detector.py @@ -5,6 +5,7 @@ from clashroyalebuildabot.constants import IMAGES_DIR from clashroyalebuildabot.namespaces import Screens +from clashroyalebuildabot.namespaces.screens import Screen class ScreenDetector: @@ -30,15 +31,21 @@ def _calculate_screen_hashes(self): screen_hashes[screen] = self._image_hash(image) return screen_hashes - def run(self, image): + def run(self, image: Image) -> Screen: current_screen = Screens.UNKNOWN best_diff = self.threshold for screen in Screens.__dict__.values(): if screen.ltrb is None: continue - - hash_ = self._image_hash(image.crop(screen.ltrb)) + # screen.ltb are dimensions scaled to 720x1280 so we scale them : + treated_ltrb = ( + int(screen.ltrb[0] * image.size[0] / 720), + int(screen.ltrb[1] * image.size[1] / 1280), + int(screen.ltrb[2] * image.size[0] / 720), + int(screen.ltrb[3] * image.size[1] / 1280), + ) + hash_ = self._image_hash(image.crop(treated_ltrb)) target_hash = self.screen_hashes[screen] diff = np.mean(np.abs(hash_ - target_hash)) diff --git a/clashroyalebuildabot/emulator/emulator.py b/clashroyalebuildabot/emulator/emulator.py index e402056..8502729 100644 --- a/clashroyalebuildabot/emulator/emulator.py +++ b/clashroyalebuildabot/emulator/emulator.py @@ -10,6 +10,7 @@ import av from loguru import logger +from PIL.Image import Image import requests from tqdm import tqdm @@ -237,7 +238,7 @@ def start_game(self): def click(self, x, y): self._run_command(["shell", "input", "tap", str(x), str(y)]) - def take_screenshot(self): + def take_screenshot(self) -> Image: logger.debug("Starting to take screenshot...") while self.frame is None: time.sleep(0.01) diff --git a/clashroyalebuildabot/images/screen/in_game.jpg b/clashroyalebuildabot/images/screen/in_game.jpg index e5e2cd8..c69911e 100644 Binary files a/clashroyalebuildabot/images/screen/in_game.jpg and b/clashroyalebuildabot/images/screen/in_game.jpg differ diff --git a/clashroyalebuildabot/images/screen/lobby.jpg b/clashroyalebuildabot/images/screen/lobby.jpg index 75e2246..9d61cd2 100644 Binary files a/clashroyalebuildabot/images/screen/lobby.jpg and b/clashroyalebuildabot/images/screen/lobby.jpg differ diff --git a/clashroyalebuildabot/namespaces/screens.py b/clashroyalebuildabot/namespaces/screens.py index 54bc6dc..55ae4ec 100644 --- a/clashroyalebuildabot/namespaces/screens.py +++ b/clashroyalebuildabot/namespaces/screens.py @@ -9,18 +9,19 @@ class Screen: click_xy: Optional[Tuple[int, int]] +# coords are scaled to 720x1280 @dataclass(frozen=True) class _ScreensNamespace: UNKNOWN: Screen = Screen("unknown", None, None) - IN_GAME: Screen = Screen("in_game", (14, 586, 57, 601), None) + IN_GAME: Screen = Screen("in_game", (148, 1254, 163, 1274), None) LOBBY: Screen = Screen( "lobby", - (315, 48, 356, 89), - (220, 830), + (424, 126, 506, 181), + (360, 820), ) END_OF_GAME: Screen = Screen( "end_of_game", - (143, 558, 225, 588), + (279, 1095, 440, 1154), (360, 1125), )