diff --git a/clashroyalebuildabot/bot/bot.py b/clashroyalebuildabot/bot/bot.py index 2eeb317..33e15fa 100644 --- a/clashroyalebuildabot/bot/bot.py +++ b/clashroyalebuildabot/bot/bot.py @@ -66,6 +66,17 @@ def __init__(self, actions, auto_start=True): ) keyboard_thread.start() + @staticmethod + def _log_and_wait(prefix, delay): + if delay == 1: + suffix = "second." + else: + suffix = "seconds." + message = f"{prefix}. Waiting for {delay} {suffix}" + logger.info(message) + time.sleep(delay) + return None + @staticmethod def _setup_logger(): config_path = os.path.join(SRC_DIR, "config.yaml") @@ -80,7 +91,8 @@ def _setup_logger(): level=log_level, ) - def _handle_keyboard_shortcut(self): + @staticmethod + def _handle_keyboard_shortcut(): while True: keyboard.wait("ctrl+p") if pause_event.is_set(): @@ -172,32 +184,28 @@ def step(self): if new_screen != old_screen: logger.info(f"New screen state: {new_screen}") + if new_screen == Screens.UNKNOWN: + self._log_and_wait("Unknown screen", 2) + return + if new_screen == Screens.END_OF_GAME: if not self.end_of_game_clicked: self.emulator.click(*self.state.screen.click_xy) self.end_of_game_clicked = True - logger.debug( - "Clicked END_OF_GAME screen. Waiting for 2 Seconds." - ) - time.sleep(2) + self._log_and_wait("Clicked END_OF_GAME screen", 2) return self.end_of_game_clicked = False if self.auto_start and new_screen == Screens.LOBBY: self.emulator.click(*self.state.screen.click_xy) - logger.info("Starting game. Waiting for 2 Seconds.") self.end_of_game_clicked = False - time.sleep(2) + self._log_and_wait("Starting game", 2) return actions = self.get_actions() if not actions: - logger.debug( - f"No actions available. Waiting for {self.play_action_delay} " - f"{'Second' if self.play_action_delay == 1 else 'Seconds'}." - ) - time.sleep(self.play_action_delay) + self._log_and_wait("No actions available", self.play_action_delay) return random.shuffle(actions) @@ -210,19 +218,16 @@ def step(self): best_score = score if best_score[0] == 0: - logger.info( - f"No good actions available. Waiting for {self.play_action_delay} " - f"{'Second' if self.play_action_delay == 1 else 'Seconds'}." + self._log_and_wait( + "No good actions available", self.play_action_delay ) - time.sleep(self.play_action_delay) return self.play_action(best_action) - logger.info( - f"Playing {best_action} with score {best_score}. Waiting for {self.play_action_delay} " - f"{'Second' if self.play_action_delay == 1 else 'Seconds'}." + self._log_and_wait( + f"Playing {best_action} with score {best_score}", + self.play_action_delay, ) - time.sleep(self.play_action_delay) def run(self): try: diff --git a/clashroyalebuildabot/detectors/screen_detector.py b/clashroyalebuildabot/detectors/screen_detector.py index 41ee6b1..8082135 100644 --- a/clashroyalebuildabot/detectors/screen_detector.py +++ b/clashroyalebuildabot/detectors/screen_detector.py @@ -31,7 +31,7 @@ def _calculate_screen_hashes(self): return screen_hashes def run(self, image): - current_screen = Screens.IN_GAME + current_screen = Screens.UNKNOWN best_diff = self.threshold for screen in Screens.__dict__.values(): diff --git a/clashroyalebuildabot/images/screen/in_game.jpg b/clashroyalebuildabot/images/screen/in_game.jpg new file mode 100644 index 0000000..e397f5a Binary files /dev/null and b/clashroyalebuildabot/images/screen/in_game.jpg differ diff --git a/clashroyalebuildabot/images/screen/lobby.jpg b/clashroyalebuildabot/images/screen/lobby.jpg index b6a0dd3..75e2246 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 cd81d1c..6e36fdc 100644 --- a/clashroyalebuildabot/namespaces/screens.py +++ b/clashroyalebuildabot/namespaces/screens.py @@ -1,13 +1,10 @@ from dataclasses import dataclass from typing import Optional, Tuple -CHEST_SIZE = 62 -CHEST_X = 0 -CHEST_Y = 590 -OK_X = 143 -OK_Y = 558 -OK_WIDTH = 82 -OK_HEIGHT = 30 +IN_GAME_X = 143 +IN_GAME_Y = 558 +IN_GAME_WIDTH = 82 +IN_GAME_HEIGHT = 30 @dataclass(frozen=True) @@ -19,15 +16,16 @@ class Screen: @dataclass(frozen=True) class _ScreensNamespace: - IN_GAME: Screen = Screen("in_game", None, None) + UNKNOWN: Screen = Screen("unknown", None, None) + IN_GAME: Screen = Screen("in_game", (315, 5, 365, 15), None) LOBBY: Screen = Screen( "lobby", - (CHEST_X, CHEST_Y, CHEST_X + CHEST_SIZE, CHEST_Y + CHEST_SIZE), + (315, 48, 356, 89), (220, 830), ) END_OF_GAME: Screen = Screen( "end_of_game", - (OK_X, OK_Y, OK_X + OK_WIDTH, OK_Y + OK_HEIGHT), + (143, 558, 225, 588), (360, 1125), )