finished player code

This commit is contained in:
Adam Saudagar 2020-10-15 07:53:15 +05:30
parent 66e6a70fba
commit b88cb8567c
5 changed files with 89 additions and 50 deletions

View File

@ -80,8 +80,8 @@ def callibrate(engine):
time_to_reach_bottom = time.time() - y_cal_start_time
engine.factors = move_factor, rot_factor, time_to_reach_bottom
engine.config.set("full_auto_factors", engine.factors)
logging.info(engine.factors)
engine._factors = move_factor, rot_factor, time_to_reach_bottom
engine.config.set("full_auto_factors", engine._factors)
logging.info(engine._factors)
hotkey.free_key(Key.F8)

View File

@ -7,6 +7,10 @@ import numpy as np
import pywintypes
import pytesseract
from fishy.engine.fullautofisher.player import Player
from fishy.engine.fullautofisher.recorder import Recorder
from fishy.engine.semifisher import fishing_event
from fishy.engine.IEngine import IEngine
from fishy.engine.fullautofisher.calibrate import callibrate
from fishy.engine.window import Window
@ -81,15 +85,16 @@ class FullAuto(IEngine):
def __init__(self, config, gui_ref):
super().__init__(config, gui_ref)
self.factors = self.config.get("full_auto_factors", None)
self.tesseract_dir = None
self.target = None
self._factors = self.config.get("full_auto_factors", None)
self._tesseract_dir = None
self._target = None
self.callibrate_state = -1
if self.factors is None:
if self._factors is None:
logging.warning("Please callibrate first")
self._hole_found_flag = False
self._curr_rotate_y = 0
def run(self):
logging.info("Loading please wait...")
self.initalize_keys()
@ -103,9 +108,9 @@ class FullAuto(IEngine):
self.window = Window(color=cv2.COLOR_RGB2GRAY)
self.window.crop = get_crop_coods(self.window)
self.tesseract_dir = self.config.get("tesseract_dir", None)
self._tesseract_dir = self.config.get("tesseract_dir", None)
if self.tesseract_dir is None:
if self._tesseract_dir is None:
logging.warning("Can't start without Tesseract Directory")
self.gui.bot_started(False)
self.toggle_start()
@ -124,26 +129,26 @@ class FullAuto(IEngine):
unassign_keys()
def get_coods(self):
return get_values_from_image(self.window.processed_image(func=image_pre_process), self.tesseract_dir)
return get_values_from_image(self.window.processed_image(func=image_pre_process), self._tesseract_dir)
def move_to(self, target):
if target is None:
logging.error("set target first")
return
if self.factors is None:
if self._factors is None:
logging.error("you need to callibrate first")
return
current = self.get_coods()
print(f"Moving from {(current[0], current[1])} to {self.target}")
print(f"Moving from {(current[0], current[1])} to {self._target}")
move_vec = target[0] - current[0], target[1] - current[1]
target_angle = math.degrees(math.atan2(-move_vec[1], move_vec[0]))
from_angle = current[2]
self.rotate_to(target_angle, from_angle)
walking_time = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) / self.factors[0]
walking_time = math.sqrt(move_vec[0] ** 2 + move_vec[1] ** 2) / self._factors[0]
print(f"walking for {walking_time}")
kb.press('w')
time.sleep(walking_time)
@ -166,7 +171,7 @@ class FullAuto(IEngine):
if abs(angle_diff) > 180:
angle_diff = (360 - abs(angle_diff)) * sign(angle_diff) * -1
rotate_times = int(angle_diff / self.factors[1]) * -1
rotate_times = int(angle_diff / self._factors[1]) * -1
print(f"rotate_times: {rotate_times}")
@ -174,17 +179,43 @@ class FullAuto(IEngine):
mse.move(sign(rotate_times) * FullAuto.rotate_by * -1, 0)
time.sleep(0.05)
def look_for_hole(self):
self._hole_found_flag = False
def found_hole(e):
if e == "look":
self._hole_found_flag = True
fishing_event.subscribers.append(found_hole)
t = 0
while not self._hole_found_flag or t <= self._factors[2]/2:
mse.move(0, FullAuto.rotate_by)
time.sleep(0.05)
t += 0.05
while not self._hole_found_flag or t > 0:
mse.move(0, -FullAuto.rotate_by)
time.sleep(0.05)
t -= 0.05
self._curr_rotate_y = t
fishing_event.subscribers.remove(found_hole)
return self._hole_found_flag
def initalize_keys(self):
hotkey.set_hotkey(Key.LEFT, lambda: logging.info(self.get_coods()))
# hotkey.set_hotkey(Key.LEFT, lambda: logging.info(self.get_coods()))
hotkey.set_hotkey(Key.UP, lambda: callibrate(self))
def down():
t = self.get_coods()[:-1]
self.config.set("target", t)
print(f"target_coods are {t}")
hotkey.set_hotkey(Key.DOWN, down)
# def down():
# t = self.get_coods()[:-1]
# self.config.set("target", t)
# print(f"target_coods are {t}")
# hotkey.set_hotkey(Key.DOWN, down)
hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None)))
# hotkey.set_hotkey(Key.RIGHT, lambda: self.move_to(self.config.get("target", None)))
hotkey.set_hotkey(Key.LEFT, lambda: Recorder(self).start)
hotkey.set_hotkey(Key.DOWN, lambda: Player(self).start)
if __name__ == '__main__':

View File

@ -1,11 +1,12 @@
import json
import logging
import time
from threading import Thread
from tkinter.filedialog import asksaveasfile, askopenfile
from fishy.engine.fullautofisher.engine import FullAuto
from fishy.helper import hotkey
from fishy.helper import hotkey, helper
from fishy.helper.hotkey import Key
@ -14,6 +15,7 @@ class Player:
self.recording = False
self.engine = engine
self.timeline = []
self.hole_complete_flag = False
def _mark_hole(self):
coods = self.engine.get_coods()
@ -22,7 +24,14 @@ class Player:
def _stop_recording(self):
self.recording = False
def start_recording(self):
def _hole_complete_callback(self, e):
if e == "idle":
self.hole_complete_flag = True
def start(self):
Thread(target=self.start_route).start()
def start_route(self):
file = askopenfile(mode='r', filetypes=[('Python Files', '*.py')])
if not file:
logging.error("file not selected")
@ -41,31 +50,9 @@ class Player:
self.engine.move_to(action[1])
self.engine.rotate_to(action[1][2])
# scan for fish hole
if self.engine.look_for_hole():
self.hole_complete_flag = False
helper.wait_until(lambda: self.hole_complete_flag)
# if found start fishing and wait for hole to complete
# contine when hole completes
logging.info("f7 for marking hole, f8 to stop recording")
hotkey.set_hotkey(Key.F7, self._mark_hole)
hotkey.set_hotkey(Key.F8, self._stop_recording)
self.recording = True
self.timeline = []
while self.recording:
start_time = time.time()
coods = self.engine.get_coods()
self.timeline.append(("goto", (coods[0], coods[1])))
time_took = time.time() - start_time
if time_took <= Recorder.recording_fps:
time.sleep(Recorder.recording_fps - time_took)
else:
logging.warning("Took too much time to record")
file = None
while not file:
file = asksaveasfile(mode='wb', filetypes=[('Fishy File', '*.fishy')])
data = {"full_auto_path": self.timeline}
json.dump(data, file)
file.close()

View File

@ -1,6 +1,7 @@
import json
import logging
import time
from threading import Thread
from tkinter.filedialog import asksaveasfile
from fishy.engine.fullautofisher.engine import FullAuto
@ -24,6 +25,9 @@ class Recorder:
def _stop_recording(self):
self.recording = False
def start(self):
Thread(target=self.start_recording).start()
def start_recording(self):
logging.info("f7 for marking hole, f8 to stop recording")
hotkey.set_hotkey(Key.F7, self._mark_hole)

View File

@ -20,6 +20,13 @@ _fish_times = []
_hole_start_time = 0
_FishingStarted = False
subscribers = []
def _notify(event):
for subscriber in subscribers:
subscriber(event)
class FishEvent(ABC):
@abstractmethod
@ -32,6 +39,7 @@ class FishEvent(ABC):
class HookEvent(FishEvent):
def __init__(self, action_key: str, collect_r: bool):
self.action_key = action_key
self.collect_r = collect_r
@ -60,6 +68,10 @@ class HookEvent(FishEvent):
keyboard.press_and_release('r')
time.sleep(0.1)
_notify("hook")
def on_exit_callback(self, current_mode):
pass
@ -78,6 +90,7 @@ class LookEvent(FishEvent):
:param previous_mode: previous mode in the state machine
"""
keyboard.press_and_release(self.action_key)
_notify("look")
def on_exit_callback(self, current_mode):
pass
@ -113,6 +126,8 @@ class IdleEvent(FishEvent):
else:
logging.info("FISHING INTERRUPTED")
_notify("idle")
def on_exit_callback(self, current_mode):
pass
@ -136,5 +151,7 @@ class StickEvent(FishEvent):
_hole_start_time = time.time()
_fish_times = []
_notify("stick")
def on_exit_callback(self, current_mode):
pass