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

Fix screen interactions #259

Merged
merged 1 commit into from
Oct 21, 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
13 changes: 10 additions & 3 deletions clashroyalebuildabot/detectors/screen_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from clashroyalebuildabot.constants import IMAGES_DIR
from clashroyalebuildabot.namespaces import Screens
from clashroyalebuildabot.namespaces.screens import Screen


class ScreenDetector:
Expand All @@ -30,15 +31,21 @@ def _calculate_screen_hashes(self):
screen_hashes[screen] = self._image_hash(image)
return screen_hashes

def run(self, image):
def run(self, image: Image) -> Screen:
current_screen = Screens.UNKNOWN
best_diff = self.threshold

for screen in Screens.__dict__.values():
if screen.ltrb is None:
continue

hash_ = self._image_hash(image.crop(screen.ltrb))
# screen.ltb are dimensions scaled to 720x1280 so we scale them :
treated_ltrb = (
int(screen.ltrb[0] * image.size[0] / 720),
int(screen.ltrb[1] * image.size[1] / 1280),
int(screen.ltrb[2] * image.size[0] / 720),
int(screen.ltrb[3] * image.size[1] / 1280),
)
hash_ = self._image_hash(image.crop(treated_ltrb))
target_hash = self.screen_hashes[screen]

diff = np.mean(np.abs(hash_ - target_hash))
Expand Down
3 changes: 2 additions & 1 deletion clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import av
from loguru import logger
from PIL.Image import Image
import requests
from tqdm import tqdm

Expand Down Expand Up @@ -237,7 +238,7 @@ def start_game(self):
def click(self, x, y):
self._run_command(["shell", "input", "tap", str(x), str(y)])

def take_screenshot(self):
def take_screenshot(self) -> Image:
logger.debug("Starting to take screenshot...")
while self.frame is None:
time.sleep(0.01)
Expand Down
Binary file modified 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.
9 changes: 5 additions & 4 deletions clashroyalebuildabot/namespaces/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ class Screen:
click_xy: Optional[Tuple[int, int]]


# coords are scaled to 720x1280
@dataclass(frozen=True)
class _ScreensNamespace:
UNKNOWN: Screen = Screen("unknown", None, None)
IN_GAME: Screen = Screen("in_game", (14, 586, 57, 601), None)
IN_GAME: Screen = Screen("in_game", (148, 1254, 163, 1274), None)
LOBBY: Screen = Screen(
"lobby",
(315, 48, 356, 89),
(220, 830),
(424, 126, 506, 181),
(360, 820),
)
END_OF_GAME: Screen = Screen(
"end_of_game",
(143, 558, 225, 588),
(279, 1095, 440, 1154),
(360, 1125),
)

Expand Down