Skip to content

Commit

Permalink
Add unknown screen + find in-game screen explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
Pbatch committed Sep 8, 2024
1 parent cfe4cfe commit bc1cc5e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
45 changes: 25 additions & 20 deletions clashroyalebuildabot/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion clashroyalebuildabot/detectors/screen_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Binary file added clashroyalebuildabot/images/screen/in_game.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified clashroyalebuildabot/images/screen/lobby.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 8 additions & 10 deletions clashroyalebuildabot/namespaces/screens.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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),
)

Expand Down

0 comments on commit bc1cc5e

Please sign in to comment.