From eada4e50a4c8419e1588629af16f05774cdec19e Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 4 Jul 2024 01:18:40 +0100 Subject: [PATCH] Move emulator functions into Emulator class --- clashroyalebuildabot/bot/bot.py | 26 ++++++----------------- clashroyalebuildabot/emulator/emulator.py | 16 ++++++++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/clashroyalebuildabot/bot/bot.py b/clashroyalebuildabot/bot/bot.py index ba0f59a..0ff74d4 100644 --- a/clashroyalebuildabot/bot/bot.py +++ b/clashroyalebuildabot/bot/bot.py @@ -1,6 +1,5 @@ import os import random -import subprocess import sys import time @@ -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(): @@ -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): @@ -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}") @@ -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 @@ -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 @@ -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 diff --git a/clashroyalebuildabot/emulator/emulator.py b/clashroyalebuildabot/emulator/emulator.py index 9efbacd..683b5ea 100644 --- a/clashroyalebuildabot/emulator/emulator.py +++ b/clashroyalebuildabot/emulator/emulator.py @@ -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)])