Skip to content

Commit

Permalink
Update custom_bot.py
Browse files Browse the repository at this point in the history
Moving Cards from main.py to specific bot profiles
  • Loading branch information
Leviaria authored Jun 20, 2024
1 parent acbe128 commit 7cebf5c
Showing 1 changed file with 33 additions and 39 deletions.
72 changes: 33 additions & 39 deletions clashroyalebuildabot/bot/example/custom_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@

from clashroyalebuildabot.bot import Bot
from clashroyalebuildabot.bot.example.custom_action import CustomAction
from clashroyalebuildabot.constants import DISPLAY_HEIGHT
from clashroyalebuildabot.constants import DISPLAY_WIDTH
from clashroyalebuildabot.constants import SCREENSHOT_HEIGHT
from clashroyalebuildabot.constants import SCREENSHOT_WIDTH
from clashroyalebuildabot.constants import DISPLAY_HEIGHT, DISPLAY_WIDTH, SCREENSHOT_HEIGHT, SCREENSHOT_WIDTH
from clashroyalebuildabot.namespaces.cards import Cards


class CustomBot(Bot):
def __init__(self, cards, debug=False):
preset_deck = {
Cards.MINIONS,
Cards.ARCHERS,
Cards.ARROWS,
Cards.GIANT,
Cards.MINIPEKKA,
Cards.FIREBALL,
Cards.KNIGHT,
Cards.MUSKETEER,
}
if set(cards) != preset_deck:
raise ValueError(f"CustomBot must use cards: {preset_deck}")
PRESET_DECK = [
Cards.MINIONS,
Cards.ARCHERS,
Cards.ARROWS,
Cards.GIANT,
Cards.MINIPEKKA,
Cards.FIREBALL,
Cards.KNIGHT,
Cards.MUSKETEER,
]

def __init__(self, cards=None, debug=False):
if cards is None:
cards = self.PRESET_DECK
if set(cards) != set(self.PRESET_DECK):
raise ValueError(f"CustomBot must use cards: {self.PRESET_DECK}")
super().__init__(cards, CustomAction, debug=debug)
self.end_of_game_clicked = False
self.pause_until = 0
Expand All @@ -45,8 +44,16 @@ def _preprocess(self):
bbox_bottom = [((bbox[0] + bbox[2]) / 2), bbox[3]]
unit["tile_xy"] = self._get_nearest_tile(*bbox_bottom)

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

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

Expand All @@ -59,38 +66,27 @@ def _end_of_game(self):
return

logger.info("Can't find Battle button, force game restart.")
subprocess.run(
"adb shell am force-stop com.supercell.clashroyale",
shell=True,
)
time.sleep(1)
subprocess.run(
"adb shell am start -n com.supercell.clashroyale/com.supercell.titan.GameApp",
shell=True,
)
logger.info("Waiting 10 seconds.")
time.sleep(10)
self.end_of_game_clicked = False
self._restart_game()

def step(self):
if self.end_of_game_clicked:
self._end_of_game()
return

old_screen = self.state.screen if self.state is not None else None
old_screen = self.state.screen if self.state else None
self.set_state()
new_screen = self.state.screen
if new_screen != old_screen:
logger.debug(f"New screen state: {new_screen}")

if new_screen == "end_of_game":
logger.info(
"End of game detected. Waiting 10 seconds for battle button"
)
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
elif new_screen == "lobby":

if new_screen == "lobby":
logger.info("In the main menu. Waiting for 1 second")
time.sleep(1)
return
Expand All @@ -110,9 +106,7 @@ def step(self):
return

self.play_action(action)
logger.info(
f"Playing {action} with score {action.score}. Waiting for 1 second"
)
logger.info(f"Playing {action} with score {action.score}. Waiting for 1 second")
time.sleep(1)

def run(self):
Expand Down

0 comments on commit 7cebf5c

Please sign in to comment.