diff --git a/clashroyalebuildabot/bot/example/custom_bot.py b/clashroyalebuildabot/bot/example/custom_bot.py index a9dba3d..e7e49f9 100644 --- a/clashroyalebuildabot/bot/example/custom_bot.py +++ b/clashroyalebuildabot/bot/example/custom_bot.py @@ -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, @@ -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 @@ -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): @@ -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 @@ -100,9 +105,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): @@ -110,4 +113,8 @@ def run(self): 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() diff --git a/clashroyalebuildabot/bot/example/lib/__pycache__/messages.cpython-312.pyc b/clashroyalebuildabot/bot/example/lib/__pycache__/messages.cpython-312.pyc new file mode 100644 index 0000000..7ebcb27 Binary files /dev/null and b/clashroyalebuildabot/bot/example/lib/__pycache__/messages.cpython-312.pyc differ diff --git a/clashroyalebuildabot/bot/example/lib/messages.py b/clashroyalebuildabot/bot/example/lib/messages.py new file mode 100644 index 0000000..df869e4 --- /dev/null +++ b/clashroyalebuildabot/bot/example/lib/messages.py @@ -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}" diff --git a/clashroyalebuildabot/bot/random/messages.py b/clashroyalebuildabot/bot/random/messages.py new file mode 100644 index 0000000..a2ce00f --- /dev/null +++ b/clashroyalebuildabot/bot/random/messages.py @@ -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}" diff --git a/clashroyalebuildabot/bot/random/random_bot.py b/clashroyalebuildabot/bot/random/random_bot.py index 8b60656..71a0e7c 100644 --- a/clashroyalebuildabot/bot/random/random_bot.py +++ b/clashroyalebuildabot/bot/random/random_bot.py @@ -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: @@ -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) diff --git a/clashroyalebuildabot/bot/two_six_hog_cycle/messages.py b/clashroyalebuildabot/bot/two_six_hog_cycle/messages.py new file mode 100644 index 0000000..a2ce00f --- /dev/null +++ b/clashroyalebuildabot/bot/two_six_hog_cycle/messages.py @@ -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}" diff --git a/clashroyalebuildabot/bot/two_six_hog_cycle/two_six_hog_cycle_bot.py b/clashroyalebuildabot/bot/two_six_hog_cycle/two_six_hog_cycle_bot.py index 6e3cf2b..6cbcea4 100644 --- a/clashroyalebuildabot/bot/two_six_hog_cycle/two_six_hog_cycle_bot.py +++ b/clashroyalebuildabot/bot/two_six_hog_cycle/two_six_hog_cycle_bot.py @@ -8,7 +8,7 @@ TwoSixHogCycleAction, ) from clashroyalebuildabot.namespaces.cards import Cards - +from messages import NEW_SCREEN_STATE_MESSAGE class TwoSixHogCycle(Bot): PRESET_DECK = { @@ -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) diff --git a/clashroyalebuildabot/emulator.py b/clashroyalebuildabot/emulator.py index a727a50..cebf2e3 100644 --- a/clashroyalebuildabot/emulator.py +++ b/clashroyalebuildabot/emulator.py @@ -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 @@ -28,8 +32,8 @@ 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): @@ -37,7 +41,7 @@ def click(self, 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: @@ -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 diff --git a/clashroyalebuildabot/messages.py b/clashroyalebuildabot/messages.py new file mode 100644 index 0000000..a2ce00f --- /dev/null +++ b/clashroyalebuildabot/messages.py @@ -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}" diff --git a/clashroyalebuildabot/screen.py b/clashroyalebuildabot/screen.py index 2b9dd34..cd95ff8 100644 --- a/clashroyalebuildabot/screen.py +++ b/clashroyalebuildabot/screen.py @@ -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(): @@ -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) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b46ecb0 --- /dev/null +++ b/setup.cfg @@ -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