mirror of
https://github.com/fishyboteso/fishyboteso.git
synced 2024-08-30 18:32:13 +00:00
reworked engine parent class to impement common things in both the engine
This commit is contained in:
parent
b79a7ed076
commit
0f35faa59f
@ -1,19 +1,24 @@
|
||||
import logging
|
||||
import traceback
|
||||
import typing
|
||||
from abc import ABC, abstractmethod
|
||||
from threading import Thread
|
||||
from typing import Callable
|
||||
|
||||
from playsound import playsound
|
||||
|
||||
from fishy.gui.funcs import GUIFuncsMock
|
||||
from fishy.helper import helper
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from fishy.gui import GUI
|
||||
|
||||
|
||||
class IEngine(ABC):
|
||||
class IEngine:
|
||||
|
||||
def __init__(self, gui_ref: 'Callable[[], GUI]'):
|
||||
self.get_gui = gui_ref
|
||||
self.start = False
|
||||
# 0 - off, 1 - running, 2 - quitting
|
||||
self.state = 0
|
||||
self.window = None
|
||||
self.thread = None
|
||||
|
||||
@ -24,10 +29,35 @@ class IEngine(ABC):
|
||||
|
||||
return self.get_gui().funcs
|
||||
|
||||
@abstractmethod
|
||||
def toggle_start(self):
|
||||
...
|
||||
if self.state == 1:
|
||||
self.turn_off()
|
||||
elif self.state == 0:
|
||||
self.turn_on()
|
||||
|
||||
def turn_on(self):
|
||||
self.state = 1
|
||||
playsound(helper.manifest_file("beep.wav"), False)
|
||||
self.thread = Thread(target=self._crash_safe)
|
||||
self.thread.start()
|
||||
|
||||
def turn_off(self):
|
||||
"""
|
||||
this method only signals the thread to close using start flag,
|
||||
its the responsibility of the thread to shut turn itself off
|
||||
"""
|
||||
self.state = 2
|
||||
helper.playsound_multiple(helper.manifest_file("beep.wav"))
|
||||
|
||||
# noinspection PyBroadException
|
||||
def _crash_safe(self):
|
||||
self.gui.bot_started(True)
|
||||
try:
|
||||
self.run()
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
self.state = 0
|
||||
self.gui.bot_started(False)
|
||||
|
||||
@abstractmethod
|
||||
def run(self):
|
||||
...
|
||||
raise NotImplementedError
|
||||
|
Loading…
Reference in New Issue
Block a user