fishyboteso/fishy/engine/semifisher/engine.py

110 lines
3.5 KiB
Python
Raw Normal View History

2021-05-09 07:05:51 +00:00
import logging
import time
import typing
from threading import Thread
2021-05-09 07:05:51 +00:00
from typing import Callable, Optional
from fishy.engine.semifisher.fishing_mode import FishingMode
from fishy.engine.common.IEngine import IEngine
from fishy.engine.common.qr_detection import get_qr_location, get_values_from_image, image_pre_process
2021-05-09 07:05:51 +00:00
from fishy.engine.common.window import WindowClient
from fishy.engine.semifisher import fishing_event, fishing_mode
2021-04-15 18:26:35 +00:00
from fishy.engine.semifisher.fishing_event import FishEvent
from fishy.helper.helper import print_exc
2021-04-09 18:50:53 +00:00
if typing.TYPE_CHECKING:
from fishy.gui import GUI
2020-06-01 12:58:18 +00:00
class SemiFisherEngine(IEngine):
def __init__(self, gui_ref: Optional['Callable[[], GUI]']):
2020-10-17 19:06:07 +00:00
super().__init__(gui_ref)
self.window = None
2022-02-20 17:01:46 +00:00
self.values = None
2022-02-03 00:21:31 +00:00
self.name = "SemiFisher"
2020-06-01 12:58:18 +00:00
def run(self):
"""
Starts the fishing
code explained in comments in detail
"""
if self.get_gui:
logging.info("Starting the bot engine, look at the fishing hole to start fishing")
Thread(target=self._wait_and_check).start()
time.sleep(0.2)
capture = self.window.get_capture()
if capture is None:
logging.error("couldn't get game capture")
return
self.window.crop = get_qr_location(capture)
if not self.window.crop:
logging.error("FishyQR not found, try to drag it around and try again")
return
fishing_event.init()
# noinspection PyBroadException
try:
self._engine_loop()
except Exception:
logging.error("exception occurred while running engine loop")
print_exc()
fishing_event.unsubscribe()
def _engine_loop(self):
skip_count = 0
while self.state == 1 and WindowClient.running():
capture = self.window.processed_image(func=image_pre_process)
# if window server crashed
if capture is None:
logging.error("Couldn't capture window stopping engine")
return
# crop qr and get the values from it
2022-02-20 17:01:46 +00:00
self.values = get_values_from_image(capture)
# if fishyqr fails to get read multiple times, stop the bot
2022-02-20 17:01:46 +00:00
if not self.values:
if skip_count >= 5:
logging.error("Couldn't read values from FishyQR, Stopping engine...")
return
skip_count += 1
time.sleep(0.1)
else:
skip_count = 0
2022-02-20 17:01:46 +00:00
if self.values:
fishing_mode.loop(self.values[3])
time.sleep(0.1)
def _wait_and_check(self):
time.sleep(10)
if not FishEvent.FishingStarted and self.state == 1:
2021-05-09 09:44:19 +00:00
logging.warning("Doesn't look like fishing has started \n"
"Check out #faqs on our discord channel to troubleshoot the issue")
# TODO: remove this, no longer needed
2022-02-20 17:01:46 +00:00
def show_qr_vals(self):
def show():
freq = 0.5
t = 0
2022-02-20 17:01:46 +00:00
while t < 25.0:
t += freq
2022-02-20 17:01:46 +00:00
logging.info(str(self.values))
time.sleep(freq)
2022-02-20 17:01:46 +00:00
logging.info("Displaying QR values stopped")
2022-02-20 17:01:46 +00:00
logging.info("Will display QR values for 25 seconds")
time.sleep(5)
Thread(target=show, args=()).start()
if __name__ == '__main__':
logging.getLogger("").setLevel(logging.DEBUG)
# noinspection PyTypeChecker
2020-10-17 19:06:07 +00:00
fisher = SemiFisherEngine(None)
fisher.toggle_start()