Skip to content

Commit

Permalink
Move emulator functions into Emulator class (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pbatch authored Jul 4, 2024
1 parent ff8ebbc commit af48b27
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
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

0 comments on commit af48b27

Please sign in to comment.