diff --git a/src/GuiThread.py b/src/GuiThread.py index d1f6c9c..912fec8 100644 --- a/src/GuiThread.py +++ b/src/GuiThread.py @@ -1,5 +1,9 @@ from threading import Thread -from time import sleep +from time import sleep, strftime, localtime + +from VersionManager import VersionManager + +from rich.layout import Layout from rich.live import Live from rich.table import Table from rich.console import Console @@ -9,7 +13,7 @@ class GuiThread(Thread): A thread that creates a capsule farm for a given account """ - def __init__(self, log, config, stats, locks): + def __init__(self, log, config, stats, locks, currentVersion): """ Initializes the FarmThread @@ -22,35 +26,108 @@ def __init__(self, log, config, stats, locks): self.config = config self.stats = stats self.locks = locks + self.currentVersion = currentVersion + self.startedTime = strftime('%b %d, %H:%M', localtime()) def generateTable(self): - table = Table() - table.add_column("Account") - table.add_column("Status") - table.add_column("Live matches") - table.add_column("Heartbeat") - table.add_column("Last drop") - table.add_column("Session Drops") - if self.config.showHistoricalDrops: - table.add_column("Lifetime Drops") + vm = VersionManager() + + accountsKeyValues = {"Status": "status", + "Heartbeat": "lastCheck", + "Last drop": "lastDrop", + "Session Drops": "sessionDrops", + "Lifetime Drops": "totalDrops", + "Live Matches": "liveMatches"} + + infoTable = Table() + infoLayout = Layout(infoTable, name="info", size=8) + + infoTable.add_column(f"Capsule Farmer Evolved v{self.currentVersion}") + infoTable.add_row("[steel_blue1]Please consider supporting League of Poro on YouTube.[/]") + infoTable.add_row("If you need help with the app, join our Discord.") + infoTable.add_row("https://discord.gg/ebm5MJNvHU") + infoTable.add_row(f"Started: [green]{strftime(self.startedTime)}[/]") + updateTable = Table() + updateLayout = Layout(updateTable, name="update", size=5) + if not vm.isLatestVersion(self.currentVersion): + updateTable.add_column(f"NEW VERSION AVAILABLE v{vm.getLatestTag()}") + updateTable.add_row(f"Download it from: https://github.com/LeagueOfPoro/CapsuleFarmerEvolved/releases/latest") + + accountsTable = Table() + accountsLayout = Layout(accountsTable, name="accounts", size=10) + accountsTable.add_column("Accounts", width=15) for acc in self.stats.accountData: - status = self.stats.accountData[acc]["status"] - if self.config.showHistoricalDrops: - table.add_row(f"{acc}", f"{status}", f"{self.stats.accountData[acc]['liveMatches']}", f"{self.stats.accountData[acc]['lastCheck']}", f"{self.stats.accountData[acc]['lastDrop']}", f"{self.stats.accountData[acc]['sessionDrops']}", f"{self.stats.accountData[acc]['totalDrops']}") - else: - table.add_row(f"{acc}", f"{status}", f"{self.stats.accountData[acc]['liveMatches']}", f"{self.stats.accountData[acc]['lastCheck']}", f"{self.stats.accountData[acc]['lastDrop']}", f"{self.stats.accountData[acc]['sessionDrops']}") + accountsTable.add_column(f"{acc}") + + rows = [] + for key, value in accountsKeyValues.items(): + if value == "totalDrops" and not self.config.showHistoricalDrops: + continue + row = [key] + for acc in self.stats.accountData: + if value == "liveMatches": + if len(self.stats.accountData[acc][value]) == 0: + row.append("0") + else: + row.append(str(len(self.stats.accountData[acc][value].split(", ")))) + else: + row.append(str(self.stats.accountData[acc][value])) + rows.append(row) + + for row in rows: + accountsTable.add_row(*row) + + matches = set() + for acc in self.stats.accountData: + if len(self.stats.accountData[acc]["liveMatches"]) == 0: + continue + if len(self.stats.accountData[acc]["liveMatches"]) == 1: + matches.add(self.stats.accountData[acc]["liveMatches"][0]) + continue + liveMatches = self.stats.accountData[acc]["liveMatches"].split(", ") + for match in liveMatches: + if match not in matches: + matches.add(match) + + matchesTable = Table() + matchesLayout = Layout(matchesTable, name="matches") + matchesTable.add_column(f"Live Matches", width=15) + for acc in self.stats.accountData: + matchesTable.add_column("", width=len(acc)) + + for match in matches: + row = [match] + for acc in self.stats.accountData: + if match in self.stats.accountData[acc]["liveMatches"]: + row.append("[green]OK[/]") + else: + row.append("[red]Error[/]") + matchesTable.add_row(*row, end_section=True) + + if not vm.isLatestVersion(self.currentVersion): + tables = [infoLayout, updateLayout, accountsLayout, matchesLayout] + else: + tables = [infoLayout, accountsLayout, matchesLayout] + + return tables + + def generateLayout(self): + tablesWithLayouts = self.generateTable() + layout = Layout() + + layout.split(*tablesWithLayouts) - return table + return layout def run(self): """ Report the status of all accounts """ console = Console(force_terminal=True) - with Live(self.generateTable(), auto_refresh=False, console=console) as live: + with Live(self.generateLayout(), auto_refresh=False, console=console) as live: while True: - live.update(self.generateTable()) + live.update(self.generateLayout()) sleep(1) self.locks["refreshLock"].acquire() live.refresh() diff --git a/src/main.py b/src/main.py index cffbb5f..51be5bb 100644 --- a/src/main.py +++ b/src/main.py @@ -25,22 +25,10 @@ def init() -> tuple[logging.Logger, Config]: help='Path to a custom config file') args = parser.parse_args() - print("*********************************************************") - print(f"* Thank you for using Capsule Farmer Evolved v{str(CURRENT_VERSION)}! *") - print("* [steel_blue1]Please consider supporting League of Poro on YouTube.[/] *") - print("* If you need help with the app, join our Discord *") - print("* https://discord.gg/ebm5MJNvHU *") - print(f"* Started: [green]{strftime('%b %d, %H:%M', localtime())}[/] *") - print("*********************************************************") - print() - Path("./logs/").mkdir(parents=True, exist_ok=True) Path("./sessions/").mkdir(parents=True, exist_ok=True) config = Config(args.configPath) log = Logger.createLogger(config.debug, CURRENT_VERSION) - if not VersionManager.isLatestVersion(CURRENT_VERSION): - log.warning("!!! NEW VERSION AVAILABLE !!! Download it from: https://github.com/LeagueOfPoro/CapsuleFarmerEvolved/releases/latest") - print("[bold red]!!! NEW VERSION AVAILABLE !!!\nDownload it from: https://github.com/LeagueOfPoro/CapsuleFarmerEvolved/releases/latest\n") return log, config @@ -58,7 +46,7 @@ def main(log: logging.Logger, config: Config): restarter = Restarter(stats) log.info(f"Starting a GUI thread.") - guiThread = GuiThread(log, config, stats, locks) + guiThread = GuiThread(log, config, stats, locks, CURRENT_VERSION) guiThread.daemon = True guiThread.start()