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

Messages Integration with Enhanced Logging #187

Closed
wants to merge 19 commits into from
Closed
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
43 changes: 25 additions & 18 deletions clashroyalebuildabot/bot/example/custom_bot.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from loguru import logger
import random
import subprocess
import time

from loguru import logger
from lib.messages import (
WAITING_MESSAGE,
ACTIONS_AFTER_END_GAME_MESSAGE,
LOBBY_DETECTED_MESSAGE,
CANNOT_FIND_BATTLE_BUTTON_MESSAGE,
NEW_SCREEN_STATE_MESSAGE,
MAIN_MENU_MESSAGE,
NO_ACTIONS_AVAILABLE_MESSAGE,
THANK_YOU_MESSAGE
)

from clashroyalebuildabot.bot import Bot
from clashroyalebuildabot.bot.example.custom_action import CustomAction
from clashroyalebuildabot.namespaces.cards import Cards
from clashroyalebuildabot.namespaces.screens import Screens


class CustomBot(Bot):
PRESET_DECK = [
Cards.MINIONS,
Expand Down Expand Up @@ -41,7 +49,7 @@ def _restart_game(self):
"adb shell am start -n com.supercell.clashroyale/com.supercell.titan.GameApp",
shell=True,
)
logger.info("Waiting 10 seconds.")
logger.info(WAITING_MESSAGE)
time.sleep(10)
self.end_of_game_clicked = False

Expand All @@ -52,13 +60,13 @@ def _end_of_game(self):

self.set_state()
actions = self.get_actions()
logger.info(f"Actions after end of game: {actions}")
logger.info(ACTIONS_AFTER_END_GAME_MESSAGE.format(actions=actions))

if self.state.screen == Screens.LOBBY:
logger.debug("Lobby detected, resuming normal operation.")
logger.debug(LOBBY_DETECTED_MESSAGE)
return

logger.info("Can't find Battle button, force game restart.")
logger.info(CANNOT_FIND_BATTLE_BUTTON_MESSAGE)
self._restart_game()

def step(self):
Expand All @@ -70,26 +78,23 @@ def step(self):
self.set_state()
new_screen = self.state.screen
if new_screen != old_screen:
logger.info(f"New screen state: {new_screen}")
logger.info(NEW_SCREEN_STATE_MESSAGE.format(new_screen=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

if new_screen == "lobby":
logger.info("In the main menu. Waiting for 1 second")
logger.info(MAIN_MENU_MESSAGE)
time.sleep(1)
return

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

Expand All @@ -100,14 +105,16 @@ 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):
try:
while True:
self.step()
except KeyboardInterrupt:
logger.info("Thanks for using CRBAB, see you next time!")
logger.info(THANK_YOU_MESSAGE)

if __name__ == "__main__":
bot = CustomBot(debug=True)
bot.run()
Binary file not shown.
18 changes: 18 additions & 0 deletions clashroyalebuildabot/bot/example/lib/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# this is the messages.py we will be referencing

WAITING_MESSAGE = "Waiting 10 seconds."
ACTIONS_AFTER_END_GAME_MESSAGE = "Actions after end of game: {actions}"
LOBBY_DETECTED_MESSAGE = "Lobby detected, resuming normal operation."
CANNOT_FIND_BATTLE_BUTTON_MESSAGE = "Can't find Battle button, force game restart."
NEW_SCREEN_STATE_MESSAGE = "New screen state: {new_screen}"
MAIN_MENU_MESSAGE = "In the main menu. Waiting for 1 second."
NO_ACTIONS_AVAILABLE_MESSAGE = "No actions available. Waiting for 1 second."
THANK_YOU_MESSAGE = "Thanks for using CRBAB, see you next time!"
PLAYING_ACTION_MESSAGE = "Playing {action}"
ERROR_GETTING_SCREEN_SIZE_MESSAGE = "Error getting screen size: {e}"
DEVICE_CONNECTION_ERROR_MESSAGE = "Exiting due to device connection error."
SCREENSHOT_SUCCESS_MESSAGE = "Screenshot captured successfully."
ADB_COMMAND_FAILED_MESSAGE = "ADB command failed: {e}"
EMULATOR_PROPERTIES_MESSAGE = "The emulator does not have the correct properties."
CURRENT_RESOLUTION_MESSAGE = "Current resolution: {resolution}"
CURRENT_DENSITY_MESSAGE = "Current density: {density}"
18 changes: 18 additions & 0 deletions clashroyalebuildabot/bot/random/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# this is the messages.py we will be referncing

WAITING_MESSAGE = "Waiting 10 seconds."
ACTIONS_AFTER_END_GAME_MESSAGE = "Actions after end of game: {actions}"
LOBBY_DETECTED_MESSAGE = "Lobby detected, resuming normal operation."
CANNOT_FIND_BATTLE_BUTTON_MESSAGE = "Can't find Battle button, force game restart."
NEW_SCREEN_STATE_MESSAGE = "New screen state: {new_screen}"
MAIN_MENU_MESSAGE = "In the main menu. Waiting for 1 second."
NO_ACTIONS_AVAILABLE_MESSAGE = "No actions available. Waiting for 1 second."
THANK_YOU_MESSAGE = "Thanks for using CRBAB, see you next time!"
PLAYING_ACTION_MESSAGE = "Playing {action}"
ERROR_GETTING_SCREEN_SIZE_MESSAGE = "Error getting screen size: {e}"
DEVICE_CONNECTION_ERROR_MESSAGE = "Exiting due to device connection error."
SCREENSHOT_SUCCESS_MESSAGE = "Screenshot captured successfully."
ADB_COMMAND_FAILED_MESSAGE = "ADB command failed: {e}"
EMULATOR_PROPERTIES_MESSAGE = "The emulator does not have the correct properties."
CURRENT_RESOLUTION_MESSAGE = "Current resolution: {resolution}"
CURRENT_DENSITY_MESSAGE = "Current density: {density}"
7 changes: 3 additions & 4 deletions clashroyalebuildabot/bot/random/random_bot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import random
import time

from loguru import logger
from messages import PLAYING_ACTION_MESSAGE # Import the PLAYING_ACTION_MESSAGE

from clashroyalebuildabot.bot.bot import Bot


class RandomBot(Bot):
def run(self):
while True:
Expand All @@ -18,6 +17,6 @@ def run(self):
action = random.choice(actions)
# Play the given action
self.play_action(action)
# Log the result
logger.info(f"Playing {action}")
# Log the result using the predefined message
logger.info(PLAYING_ACTION_MESSAGE.format(action=action))
time.sleep(3)
18 changes: 18 additions & 0 deletions clashroyalebuildabot/bot/two_six_hog_cycle/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# this is the messages.py we will be referncing

WAITING_MESSAGE = "Waiting 10 seconds."
ACTIONS_AFTER_END_GAME_MESSAGE = "Actions after end of game: {actions}"
LOBBY_DETECTED_MESSAGE = "Lobby detected, resuming normal operation."
CANNOT_FIND_BATTLE_BUTTON_MESSAGE = "Can't find Battle button, force game restart."
NEW_SCREEN_STATE_MESSAGE = "New screen state: {new_screen}"
MAIN_MENU_MESSAGE = "In the main menu. Waiting for 1 second."
NO_ACTIONS_AVAILABLE_MESSAGE = "No actions available. Waiting for 1 second."
THANK_YOU_MESSAGE = "Thanks for using CRBAB, see you next time!"
PLAYING_ACTION_MESSAGE = "Playing {action}"
ERROR_GETTING_SCREEN_SIZE_MESSAGE = "Error getting screen size: {e}"
DEVICE_CONNECTION_ERROR_MESSAGE = "Exiting due to device connection error."
SCREENSHOT_SUCCESS_MESSAGE = "Screenshot captured successfully."
ADB_COMMAND_FAILED_MESSAGE = "ADB command failed: {e}"
EMULATOR_PROPERTIES_MESSAGE = "The emulator does not have the correct properties."
CURRENT_RESOLUTION_MESSAGE = "Current resolution: {resolution}"
CURRENT_DENSITY_MESSAGE = "Current density: {density}"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
TwoSixHogCycleAction,
)
from clashroyalebuildabot.namespaces.cards import Cards

from messages import NEW_SCREEN_STATE_MESSAGE

class TwoSixHogCycle(Bot):
PRESET_DECK = {
Expand Down Expand Up @@ -52,4 +52,5 @@ def run(self):
logger.info(
f"Playing {action} with score {action.score} and sleeping for 1 second"
)
logger.info(NEW_SCREEN_STATE_MESSAGE.format(new_screen=self.state.screen))
time.sleep(1.0)
16 changes: 10 additions & 6 deletions clashroyalebuildabot/emulator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import os

from adb_shell.adb_device import AdbDeviceTcp
from loguru import logger
from PIL import Image
import yaml

from messages import (
ERROR_GETTING_SCREEN_SIZE_MESSAGE,
DEVICE_CONNECTION_ERROR_MESSAGE,
SCREENSHOT_SUCCESS_MESSAGE,
ADB_COMMAND_FAILED_MESSAGE
)
from clashroyalebuildabot.constants import SCREENSHOT_HEIGHT
from clashroyalebuildabot.constants import SCREENSHOT_WIDTH
from clashroyalebuildabot.constants import SRC_DIR
Expand All @@ -28,16 +32,16 @@ def __init__(self):
window_size = window_size.replace("Physical size: ", "")
self.size = tuple(int(i) for i in window_size.split("x"))
except Exception as e:
logger.critical(f"Error getting screen size: {e}")
logger.critical("Exiting due to device connection error.")
logger.critical(ERROR_GETTING_SCREEN_SIZE_MESSAGE.format(e=e))
logger.critical(DEVICE_CONNECTION_ERROR_MESSAGE)
raise SystemExit() from e

def click(self, x, y):
self.device.shell(f"input tap {x} {y}")

def _take_screenshot(self):
screenshot_bytes = self.device.shell("screencap", decode=False)
logger.debug("Screenshot captured successfully.")
logger.debug(SCREENSHOT_SUCCESS_MESSAGE)

image = None
try:
Expand Down Expand Up @@ -75,7 +79,7 @@ def take_screenshot(self):
try:
image = self._take_screenshot()
except Exception as e:
logger.error(f"ADB command failed: {e}")
logger.error(ADB_COMMAND_FAILED_MESSAGE.format(e=e))
raise

return image
18 changes: 18 additions & 0 deletions clashroyalebuildabot/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# this is the messages.py we will be referncing

WAITING_MESSAGE = "Waiting 10 seconds."
ACTIONS_AFTER_END_GAME_MESSAGE = "Actions after end of game: {actions}"
LOBBY_DETECTED_MESSAGE = "Lobby detected, resuming normal operation."
CANNOT_FIND_BATTLE_BUTTON_MESSAGE = "Can't find Battle button, force game restart."
NEW_SCREEN_STATE_MESSAGE = "New screen state: {new_screen}"
MAIN_MENU_MESSAGE = "In the main menu. Waiting for 1 second."
NO_ACTIONS_AVAILABLE_MESSAGE = "No actions available. Waiting for 1 second."
THANK_YOU_MESSAGE = "Thanks for using CRBAB, see you next time!"
PLAYING_ACTION_MESSAGE = "Playing {action}"
ERROR_GETTING_SCREEN_SIZE_MESSAGE = "Error getting screen size: {e}"
DEVICE_CONNECTION_ERROR_MESSAGE = "Exiting due to device connection error."
SCREENSHOT_SUCCESS_MESSAGE = "Screenshot captured successfully."
ADB_COMMAND_FAILED_MESSAGE = "ADB command failed: {e}"
EMULATOR_PROPERTIES_MESSAGE = "The emulator does not have the correct properties."
CURRENT_RESOLUTION_MESSAGE = "Current resolution: {resolution}"
CURRENT_DENSITY_MESSAGE = "Current density: {density}"
16 changes: 12 additions & 4 deletions clashroyalebuildabot/screen.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import tempfile

from adb_shell.adb_device import AdbDeviceTcp
from loguru import logger
from PIL import Image
import yaml

from clashroyalebuildabot.constants import SRC_DIR

from messages import (
EMULATOR_PROPERTIES_MESSAGE,
CURRENT_RESOLUTION_MESSAGE,
CURRENT_DENSITY_MESSAGE
)

# Load configuration from config.yaml
def load_config():
Expand Down Expand Up @@ -80,7 +82,13 @@ def check_emulator_properties():

if resolution_correct and density_correct:
logger.info(
"The emulator has the correct resolution (720x1280) and density (240 dpi)."
EMULATOR_PROPERTIES_MESSAGE
)
logger.info(
CURRENT_RESOLUTION_MESSAGE.format(resolution=resolution)
)
logger.info(
CURRENT_DENSITY_MESSAGE.format(density=density)
)

screenshot_path = take_screenshot(device)
Expand Down
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[isort]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 88

known_first_party = clashroyalebuildabot
skip = .tox, .venv, build, dist