Skip to content

Commit

Permalink
Merge pull request #222 from Leviaria/main
Browse files Browse the repository at this point in the history
Improve Detection Reliability and Error Handling
  • Loading branch information
Leviaria authored Aug 30, 2024
2 parents c68b7a1 + 9326c42 commit 3f15d17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
28 changes: 20 additions & 8 deletions clashroyalebuildabot/detectors/detector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time

from loguru import logger

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion clashroyalebuildabot/detectors/screen_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = (
Expand All @@ -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)
Expand Down

0 comments on commit 3f15d17

Please sign in to comment.