From 0f35faa59fd3df62833f98480c6e0fb3c461077b Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Mon, 31 Jan 2022 23:18:51 +0530 Subject: [PATCH] reworked engine parent class to impement common things in both the engine --- fishy/engine/common/IEngine.py | 44 ++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/fishy/engine/common/IEngine.py b/fishy/engine/common/IEngine.py index 7e4e24a..f38ad89 100644 --- a/fishy/engine/common/IEngine.py +++ b/fishy/engine/common/IEngine.py @@ -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