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

feat: Improve UI layout #189 #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 96 additions & 19 deletions src/GuiThread.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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()
Expand Down
14 changes: 1 addition & 13 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

Expand Down