fishyboteso/fishy/engine/common/IEngine.py
Adam Saudagar 245493fbc4 fullauto reworked with the new parent class
- removed double beep on turnoff
- removed show crop feature in fullauto
- semi fisher is started only when needed, and turned off when the job is done by mode, instead of full auto engine
- use the last coord from recording isntead of getting a new one when using editor in recorder mode, to avoid recording failure when saving the recording
2022-02-01 16:57:14 +05:30

71 lines
1.6 KiB
Python

import logging
import traceback
import typing
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:
def __init__(self, gui_ref: 'Callable[[], GUI]'):
self.get_gui = gui_ref
# 0 - off, 1 - running, 2 - quitting
self.state = 0
self.window = None
self.thread = None
@property
def gui(self):
if self.get_gui is None:
return GUIFuncsMock()
return self.get_gui().funcs
@property
def start(self):
return self.state == 1
def toggle_start(self):
if self.state == 0:
self.turn_on()
else:
self.turn_on()
def turn_on(self):
self.state = 1
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
"""
if self.state == 1:
logging.info("turning off...")
self.state = 2
else:
logging.error("engine already signaled to turn off")
# todo: implement force turn off on repeated calls
# 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)
def run(self):
raise NotImplementedError