diff --git a/clashroyalebuildabot/utils/git_utils.py b/clashroyalebuildabot/utils/git_utils.py new file mode 100644 index 0000000..e8b67e0 --- /dev/null +++ b/clashroyalebuildabot/utils/git_utils.py @@ -0,0 +1,45 @@ +import subprocess +import sys +from time import sleep + +from loguru import logger + + +def _is_branch_late() -> bool: + subprocess.run( + ["git", "fetch"], check=True, capture_output=True, text=True + ) + + status = subprocess.run( + ["git", "status", "-uno"], + capture_output=True, + text=True, + check=True, + ).stdout + return "Your branch is up to date" not in status + + +def _check_and_pull_updates() -> None: + if not _is_branch_late(): + return + + should_update = input( + "New updates available. Do you want to update the bot? [y]/n: " + ) + if should_update.lower() not in ["y", "yes", ""]: + return + subprocess.run(["git", "pull"], check=True, capture_output=True, text=True) + + +def check_and_pull_updates() -> None: + try: + _check_and_pull_updates() + except subprocess.CalledProcessError as e: + if "not a git repository" in e.stderr: + err = "We recommend getting the project using git." + err += "You won't be able to get any updates until you do." + logger.warning(err) + sleep(3) + return + logger.error(f"Error while checking / pulling updates: {e.stderr}") + sys.exit(1) diff --git a/main.py b/main.py index dccb1d3..125b286 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,10 @@ +from datetime import datetime import signal import sys +import threading +import time from loguru import logger -from PyQt6.QtWidgets import QApplication from clashroyalebuildabot.actions import ArchersAction from clashroyalebuildabot.actions import BabyDragonAction @@ -12,9 +14,28 @@ from clashroyalebuildabot.actions import MinipekkaAction from clashroyalebuildabot.actions import MusketeerAction from clashroyalebuildabot.actions import WitchAction -from clashroyalebuildabot.gui.main_window import MainWindow -from clashroyalebuildabot.gui.utils import load_config -from clashroyalebuildabot.utils.logger import setup_logger +from clashroyalebuildabot.bot import Bot + +start_time = datetime.now() + +logger.remove() +logger.add( + sys.stderr, + format="{time} {level} {message}", + backtrace=False, + diagnose=False, +) + + +def update_terminal_title(): + while True: + elapsed_time = datetime.now() - start_time + hours, remainder = divmod(elapsed_time.total_seconds(), 3600) + minutes, seconds = divmod(remainder, 60) + formatted_time = f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}" + sys.stdout.write(f"\x1b]2;{formatted_time} | BuildABot\x07") + sys.stdout.flush() + time.sleep(1) def main(): @@ -29,19 +50,17 @@ def main(): WitchAction, ] try: - config = load_config() - - app = QApplication(sys.argv) - window = MainWindow(config, actions) - setup_logger(window, config) - - window.show() - sys.exit(app.exec()) + bot = Bot(actions=actions) + bot.run() except Exception as e: - logger.error(f"An error occurred in main loop: {e}") + logger.error(f"An error occurred: {e}") sys.exit(1) if __name__ == "__main__": signal.signal(signal.SIGINT, signal.SIG_DFL) + + title_thread = threading.Thread(target=update_terminal_title, daemon=True) + title_thread.start() + main()