Skip to content

Commit

Permalink
Merge remote-tracking branch 'Leviaria/main' into fork/Leviaria/main
Browse files Browse the repository at this point in the history
  • Loading branch information
fe-art committed Sep 13, 2024
2 parents 267bf1c + d9c2d27 commit 3d2f992
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
36 changes: 23 additions & 13 deletions clashroyalebuildabot/gui/layout_setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from PyQt6.QtCore import Qt # Fügen Sie den fehlenden Import für Qt hinzu
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QCheckBox
from PyQt6.QtWidgets import QComboBox
from PyQt6.QtWidgets import QDoubleSpinBox
Expand All @@ -25,33 +26,42 @@ def setup_top_bar(main_window):
top_bar.setStyleSheet("background-color: #1E272E;")
top_bar_layout = QHBoxLayout(top_bar)

left_layout = QVBoxLayout()
left_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
logo_text_layout = QHBoxLayout()

logo_label = QLabel()
logo_pixmap = QPixmap("logo.png").scaled(
120,
120,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
logo_label.setPixmap(logo_pixmap)
logo_label.setFixedSize(120, 120)
logo_text_layout.addWidget(logo_label)

text_layout = QVBoxLayout()
text_layout.setAlignment(Qt.AlignmentFlag.AlignTop)

server_name = QLabel("Clash Royale Build-A-Bot")
server_name.setStyleSheet(
"font-weight: bold; font-size: 16pt; color: white;"
)
left_layout.addWidget(server_name)
text_layout.addWidget(server_name)

server_details = QLabel(
'<a href="https://github.com/Pbatch/ClashRoyaleBuildABot">https://github.com/Pbatch/ClashRoyaleBuildABot</a>'
)
server_details.setOpenExternalLinks(True)
server_details.setStyleSheet("color: #57A6FF;")
left_layout.addWidget(server_details)
text_layout.addWidget(server_details)

main_window.server_id_label = QLabel("Status")
main_window.server_id_label = QLabel("Status: Stopped")
main_window.server_id_label.setStyleSheet("color: #999;")
left_layout.addWidget(main_window.server_id_label)
text_layout.addWidget(main_window.server_id_label)

port_link = QLabel(
'<a href="http://localhost:5555" style="color: #57A6FF;">127.0.0.1:5555</a>'
)
port_link.setOpenExternalLinks(True)
left_layout.addWidget(port_link)
logo_text_layout.addLayout(text_layout)

top_bar_layout.addLayout(left_layout)
top_bar_layout.addLayout(logo_text_layout)

right_layout = QVBoxLayout()
right_layout.setAlignment(Qt.AlignmentFlag.AlignRight)
Expand Down
11 changes: 8 additions & 3 deletions clashroyalebuildabot/gui/main_window.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from threading import Thread

from loguru import logger
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWidgets import QMainWindow
from PyQt6.QtWidgets import QVBoxLayout
Expand All @@ -25,14 +28,17 @@ def __init__(self, config, actions):
self.bot_thread = None
self.is_running = False

self.setWindowTitle("Clash Royale Build-A-Bot")
self.setWindowTitle(" ")
self.setGeometry(100, 100, 900, 600)

transparent_pixmap = QPixmap(1, 1)
transparent_pixmap.fill(Qt.GlobalColor.transparent)
self.setWindowIcon(QIcon(transparent_pixmap))

main_widget = QWidget(self)
self.setCentralWidget(main_widget)
main_layout = QVBoxLayout(main_widget)

# Setup GUI components
top_bar = setup_top_bar(self)
tab_widget = setup_tabs(self)

Expand Down Expand Up @@ -135,7 +141,6 @@ def bot_task(self):
self.bot.run()
self.stop_bot()
except Exception as e:
# output error in logger
logger.error(f"Bot crashed: {e}")
self.stop_bot()
raise
Expand Down
45 changes: 45 additions & 0 deletions clashroyalebuildabot/utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 32 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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():
Expand All @@ -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()

0 comments on commit 3d2f992

Please sign in to comment.