Skip to content

Commit

Permalink
new gui foundations
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmiglio committed Jul 12, 2024
1 parent 2d8426b commit 0bd8fc1
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 311 deletions.
43 changes: 26 additions & 17 deletions src/pyclashbot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
from PySide6.QtWidgets import QApplication
import sys
from pyclashbot.interface.layout import FrontEnd
from pyclashbot.bot.stats import window, app
from pyclashbot.bot.states import StateTree
from threading import Thread
from PySide6.QtCore import QThread

def main():
window.start_button.clicked.connect(on_start_button_clicked)
window.show()
sys.exit(app.exec())

def run_backend():
job_list = ["fight", "upgrade", "restart"]
state_tree = StateTree(job_list)
state_tree.run()


def main():
app = QApplication(sys.argv)
def start_worker_thread():
thread = QThread()
jobs = window.get_checkboxes() # Correct instance of Layout class
worker = StateTree(jobs=jobs)
worker.moveToThread(thread)

# Initialize frontend
frontend = FrontEnd()
frontend.show()
thread.started.connect(worker.run)
worker.finished.connect(thread.quit)
worker.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)

# Start backend in a separate thread
backend_thread = Thread(target=run_backend)
backend_thread.start()
# Ensure the thread and worker are properly managed
window.thread = thread
window.worker = worker

# Run the application
sys.exit(app.exec())
thread.start()

def on_start_button_clicked():
print('Start button!')

# Create and start the worker thread
start_worker_thread()


if __name__ == "__main__":
print('\n'*50)
main()
25 changes: 16 additions & 9 deletions src/pyclashbot/bot/states.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from PySide6.QtCore import QObject, Signal, Slot
from pyclashbot.bot.stats import stats
import time
from pyclashbot.bot.event_dispatcher import event_dispatcher
from pyclashbot.bot.update_stats import increment_bot_failures,stat_tester
from pyclashbot.bot.test_state import do_fight

class StateTree:
def __init__(self, job_list):
self.job_list = job_list
class StateTree(QObject):
def __init__(self, jobs=False):
super().__init__()
self.jobs = jobs

finished = Signal()

@Slot()
def run(self):
print("Running statetree.run()")
while True:
time.sleep(1)
stat_tester()
print("Jobs in state tree:")
for job_name, value in self.jobs.items():
print(' -{:^40} : {}'.format(job_name, value))

while True:
do_fight()
time.sleep(10)
32 changes: 32 additions & 0 deletions src/pyclashbot/bot/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
from pyclashbot.interface.layout import Layout
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import Signal, QObject


class Stats(QObject):
update_signal = Signal(int)

def __init__(self, window):
super().__init__()
self.wins = 0
self.losses = 0
self.window = window
self.update_signal.connect(self.update_window)

def increment_wins(self):
self.wins += 1
self.update_window()

def increment_losses(self):
self.losses += 1
self.update_window()

def update_window(self):
self.window.losses_textbox.setText(str(self.losses)) # update losses
self.window.wins_textbox.setText(str(self.wins)) # update wins


app = QApplication(sys.argv)
window = Layout()
stats = Stats(window)
26 changes: 26 additions & 0 deletions src/pyclashbot/bot/test_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import time
import random
from pyclashbot.bot.stats import stats



#simulate running a fight in the backend and updateing the gui with values
def do_fight():
fight_duration = 10#s
start_time = time.time()

print(f'Simulating a fight for {fight_duration} seconds')

while time.time() - start_time < fight_duration:
#randomly update values
if random.randint(1,3)== 1:
stats.increment_wins()
print('incrementing wins')
if random.randint(1,3)== 1:
stats.increment_losses()
print('incrementing losses')

time.sleep(0.3)

return True

Loading

0 comments on commit 0bd8fc1

Please sign in to comment.