Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small screen updates #231

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions clashroyalebuildabot/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def __init__(self, actions, auto_start=True):
)
keyboard_thread.start()

@staticmethod
def _log_and_wait(prefix, delay):
suffix = ""
if delay > 1:
suffix = "s"
message = f"{prefix}. Waiting for {delay} second{suffix}."
logger.info(message)
time.sleep(delay)

@staticmethod
def _setup_logger():
config_path = os.path.join(SRC_DIR, "config.yaml")
Expand All @@ -80,7 +89,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 +182,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 +216,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.
15 changes: 4 additions & 11 deletions clashroyalebuildabot/namespaces/screens.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
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


@dataclass(frozen=True)
class Screen:
Expand All @@ -19,15 +11,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