From f3e48cf81445f252944037c2bd679caf87753116 Mon Sep 17 00:00:00 2001 From: Leviaria <113382526+Leviaria@users.noreply.github.com> Date: Fri, 30 Aug 2024 18:21:02 +0200 Subject: [PATCH 1/3] Update emulator.py --- clashroyalebuildabot/emulator/emulator.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clashroyalebuildabot/emulator/emulator.py b/clashroyalebuildabot/emulator/emulator.py index 218f055..14a0aa1 100644 --- a/clashroyalebuildabot/emulator/emulator.py +++ b/clashroyalebuildabot/emulator/emulator.py @@ -168,6 +168,7 @@ def _restart_server(self): self._run_command(["start-server"]) def _update_frame(self): + logger.debug("Starting to update frames...") for line in iter(self.video_thread.stdout.readline, b""): try: if not line: @@ -177,11 +178,11 @@ def _update_frame(self): line = line.replace(b"\r\n", b"\n") packets = self.codec.parse(line) - if len(packets) == 0: + if not packets: continue frames = self.codec.decode(packets[-1]) - if len(frames) == 0: + if not frames: continue self.frame = ( @@ -194,8 +195,10 @@ def _update_frame(self): .to_image() ) + except av.AVError as av_error: + logger.error(f"Error while decoding video stream: {av_error}") except Exception as e: - logger.error(str(e)) + logger.error(f"Unexpected error in frame update: {str(e)}") def _start_updating_frame(self): self.frame_thread = threading.Thread(target=self._update_frame) From 069a74897426893936fd78d1b19dadb19e74e2f2 Mon Sep 17 00:00:00 2001 From: Leviaria <113382526+Leviaria@users.noreply.github.com> Date: Fri, 30 Aug 2024 18:21:25 +0200 Subject: [PATCH 2/3] Update detector.py --- clashroyalebuildabot/detectors/detector.py | 28 +++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/clashroyalebuildabot/detectors/detector.py b/clashroyalebuildabot/detectors/detector.py index 6f57813..f544874 100644 --- a/clashroyalebuildabot/detectors/detector.py +++ b/clashroyalebuildabot/detectors/detector.py @@ -1,4 +1,5 @@ import os +import time from loguru import logger @@ -32,11 +33,22 @@ def __init__(self, cards): def run(self, image): logger.debug("Setting state...") - cards, ready = self.card_detector.run(image) - allies, enemies = self.unit_detector.run(image) - numbers = self.number_detector.run(image) - screen = self.screen_detector.run(image) - - state = State(allies, enemies, numbers, cards, ready, screen) - - return state + retries = 3 + for attempt in range(retries): + try: + cards, ready = self.card_detector.run(image) + allies, enemies = self.unit_detector.run(image) + numbers = self.number_detector.run(image) + screen = self.screen_detector.run(image) + + state = State(allies, enemies, numbers, cards, ready, screen) + return state + except Exception as e: + logger.error( + f"Detection failed on attempt {attempt + 1}: {str(e)}" + ) + if attempt < retries - 1: + time.sleep(1) + + logger.error("All detection attempts failed. Returning default state.") + return State([], [], [], [], False, None) From 9326c422ae7a39781f41ecf08624e464faf90a50 Mon Sep 17 00:00:00 2001 From: Leviaria <113382526+Leviaria@users.noreply.github.com> Date: Fri, 30 Aug 2024 18:21:39 +0200 Subject: [PATCH 3/3] Update screen_detector.py --- clashroyalebuildabot/detectors/screen_detector.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clashroyalebuildabot/detectors/screen_detector.py b/clashroyalebuildabot/detectors/screen_detector.py index 0aa8b31..41ee6b1 100644 --- a/clashroyalebuildabot/detectors/screen_detector.py +++ b/clashroyalebuildabot/detectors/screen_detector.py @@ -8,7 +8,7 @@ class ScreenDetector: - def __init__(self, hash_size=8, threshold=20): + def __init__(self, hash_size=8, threshold=30): self.hash_size = hash_size self.threshold = threshold self.screen_hashes = self._calculate_screen_hashes()