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

Move emulator functions into Emulator class #198

Merged
merged 1 commit into from
Jul 4, 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
26 changes: 6 additions & 20 deletions clashroyalebuildabot/bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import random
import subprocess
import sys
import time

Expand Down Expand Up @@ -44,7 +43,6 @@ def __init__(self, actions, auto_start=True, debug=False):

self._setup_logger()
self.end_of_game_clicked = False
self.pause_until = 0

@staticmethod
def _setup_logger():
Expand Down Expand Up @@ -115,6 +113,7 @@ def set_state(self):
self.state = self.detector.run(screenshot)
if self.auto_start and self.state.screen != Screens.IN_GAME:
self.emulator.click(*self.state.screen.click_xy)
logger.info("Starting game. Waiting for 2 seconds")
time.sleep(2)

def play_action(self, action):
Expand All @@ -124,26 +123,14 @@ def play_action(self, action):
self.emulator.click(*tile_centre)

def _restart_game(self):
subprocess.run(
"adb shell am force-stop com.supercell.clashroyale",
shell=True,
check=True,
)
self.emulator.stop_game()
time.sleep(1)
subprocess.run(
"adb shell am start -n com.supercell.clashroyale/com.supercell.titan.GameApp",
shell=True,
check=True,
)
logger.info("Waiting 10 seconds.")
self.emulator.start_game()
logger.info("Starting game. Waiting 10 seconds.")
time.sleep(10)
self.end_of_game_clicked = False

def _end_of_game(self):
if time.time() < self.pause_until:
time.sleep(1)
return

self.set_state()
actions = self.get_actions()
logger.info(f"Actions after end of game: {actions}")
Expand All @@ -170,7 +157,6 @@ def step(self):
logger.info(
"End of game detected. Waiting 10 seconds for battle button"
)
self.pause_until = time.time() + 10
self.end_of_game_clicked = True
time.sleep(10)
return
Expand All @@ -182,8 +168,7 @@ def step(self):

actions = self.get_actions()
if not actions:
if self.debug:
logger.debug("No actions available. Waiting for 1 second")
logger.debug("No actions available. Waiting for 1 second")
time.sleep(1)
return

Expand All @@ -197,6 +182,7 @@ def step(self):
best_score = score

if best_score[0] == 0:
logger.info("No good actions available. Waiting for 1 second")
time.sleep(1)
return

Expand Down
16 changes: 16 additions & 0 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ def _get_width_and_height(self):
width, height = tuple(int(i) for i in window_size.split("x"))
return width, height

def stop_game(self):
self._run_command(
["shell", "am", "force-stop", "com.supercell.clashroyale"]
)

def start_game(self):
self._run_command(
[
"shell",
"am",
"start",
"-n",
"com.supercell.clashroyale/com.supercell.titan.GameApp",
]
)

def click(self, x, y):
self._run_command(["shell", "input", "tap", str(x), str(y)])

Expand Down