From 4fe22e7703753bca9df4317867f04d0d3f509b8e Mon Sep 17 00:00:00 2001
From: Semjon Kerner <Semjon.Kerner@gmx.net>
Date: Sun, 14 Mar 2021 22:16:49 +0100
Subject: [PATCH] map fishy tightly to chalutier states

---
 fishy/engine/semifisher/engine.py        |   3 +-
 fishy/engine/semifisher/fishing_event.py | 102 +++++++++++++++--------
 fishy/engine/semifisher/fishing_mode.py  |  28 +++++--
 3 files changed, 86 insertions(+), 47 deletions(-)

diff --git a/fishy/engine/semifisher/engine.py b/fishy/engine/semifisher/engine.py
index 412defe..494e795 100644
--- a/fishy/engine/semifisher/engine.py
+++ b/fishy/engine/semifisher/engine.py
@@ -49,8 +49,7 @@ class SemiFisherEngine(IEngine):
                 continue
 
             self.fishPixWindow.crop = PixelLoc.val
-            hue_value = capture[0][0][0]
-            fishing_mode.loop(hue_value)
+            fishing_mode.loop(capture[0][0])
 
         logging.info("Fishing engine stopped")
         self.gui.bot_started(False)
diff --git a/fishy/engine/semifisher/fishing_event.py b/fishy/engine/semifisher/fishing_event.py
index e17e604..0381a19 100644
--- a/fishy/engine/semifisher/fishing_event.py
+++ b/fishy/engine/semifisher/fishing_event.py
@@ -73,24 +73,75 @@ def subscribe():
     if fisher_callback not in fishing_mode.subscribers:
         fishing_mode.subscribers.append(fisher_callback)
 
-        if FishingMode.CurrentMode == State.LOOK:
+        if FishingMode.CurrentMode == State.LOOKING:
             fisher_callback(FishingMode.CurrentMode)
 
 
 def fisher_callback(event: State):
-    callbacks_map = {State.HOOK: on_hook, State.LOOK: on_look, State.IDLE: on_idle, State.STICK: on_stick}
-    callbacks_map[event]()
-    FishEvent.previousState = event
+    callbacks_map = {
+        State.IDLE: on_idle,
+        State.LOOKAWAY: on_lookaway,
+        State.LOOKING: on_looking,
+        State.DEPLETED: on_depleted,
+        State.NOBAIT: on_dead,
+        State.FISHING: on_fishing,
+        State.REELIN: on_reelin,
+        State.LOOT: on_loot,
+        State.INVFULL: on_dead,
+        State.FIGHT: on_fight,
+        State.DEAD: on_dead
+    }
+    try:
+        callbacks_map[event]()
+        FishEvent.previousState = event
+    except KeyError as ex:
+        pass
+
+
+def on_idle():
+    if FishEvent.previousState in (State.FISHING, State.REELIN):
+        logging.info("FISHING INTERRUPTED")
+
+    if FishEvent.sound:
+        playsound(helper.manifest_file("sound.mp3"), False)
+
+
+def on_lookaway():
+    return
 
 
 @if_eso_is_focused
-def on_hook():
+def on_looking():
+    """
+    presses e to throw the fishing rod
+    """
+    _fishing_sleep(0.0)
+    keyboard.press_and_release(FishEvent.action_key)
+
+
+def on_depleted():
+    logging.info("HOLE DEPLETED")
+    if FishEvent.fishCaught > 0:
+        web.send_hole_deplete(FishEvent.fishCaught, time.time() - FishEvent.hole_start_time, FishEvent.fish_times)
+        FishEvent.fishCaught = 0
+
+
+def on_fishing():
+    FishEvent.stickInitTime = time.time()
+    FishEvent.FishingStarted = True
+
+    if FishEvent.fishCaught == 0:
+        FishEvent.hole_start_time = time.time()
+        FishEvent.fish_times = []
+
+
+@if_eso_is_focused
+def on_reelin():
     """
     called when the fish hook is detected
     increases the `fishCaught`  and `totalFishCaught`, calculates the time it took to catch
     presses e to catch the fish
     """
-
     FishEvent.fishCaught += 1
     FishEvent.totalFishCaught += 1
     time_to_hook = time.time() - FishEvent.stickInitTime
@@ -98,42 +149,21 @@ def on_hook():
     logging.info("HOOOOOOOOOOOOOOOOOOOOOOOK....... " + str(FishEvent.fishCaught) + " caught " + "in " + str(
         round(time_to_hook, 2)) + " secs.  " + "Total: " + str(FishEvent.totalFishCaught))
 
+    _fishing_sleep(0.0)
     keyboard.press_and_release(FishEvent.action_key)
+    _fishing_sleep(0.5)
 
+
+def on_loot():
     if FishEvent.collect_allow_auto:
         _fishing_sleep(0.15)
         keyboard.press_and_release(FishEvent.collect_key)
-        _fishing_sleep(0.1)
-    _fishing_sleep(0.0)
+        _fishing_sleep(0.3)
 
 
-@if_eso_is_focused
-def on_look():
-    """
-    presses e to throw the fishing rod
-    """
-    keyboard.press_and_release(FishEvent.action_key)
+def on_fight():
+    logging.info("FIGHTING")
 
 
-def on_idle():
-    if FishEvent.fishCaught > 0:
-        web.send_hole_deplete(FishEvent.fishCaught, time.time() - FishEvent.hole_start_time,
-                              FishEvent.fish_times)
-        FishEvent.fishCaught = 0
-
-    if FishEvent.previousState == State.HOOK:
-        logging.info("HOLE DEPLETED")
-    else:
-        logging.info("FISHING INTERRUPTED")
-
-    if FishEvent.sound:
-        playsound(helper.manifest_file("sound.mp3"), False)
-
-
-def on_stick():
-    FishEvent.stickInitTime = time.time()
-    FishEvent.FishingStarted = True
-
-    if FishEvent.fishCaught == 0:
-        FishEvent.hole_start_time = time.time()
-        FishEvent.fish_times = []
+def on_dead():
+    logging.info("DEAD or INVENTORY FULL or NO BAIT EQUIPPED")
diff --git a/fishy/engine/semifisher/fishing_mode.py b/fishy/engine/semifisher/fishing_mode.py
index 82ac186..d439b56 100644
--- a/fishy/engine/semifisher/fishing_mode.py
+++ b/fishy/engine/semifisher/fishing_mode.py
@@ -1,14 +1,21 @@
+import time
 from enum import Enum
 
 subscribers = []
 
 
 class State(Enum):
-    HOOK = 60,
-    STICK = 18,
-    LOOK = 100,
-    IDLE = -1
-
+    IDLE     = [  0,   0, 255]
+    LOOKAWAY = [150, 255,  76]
+    LOOKING  = [100, 255, 101]
+    DEPLETED = [ 30, 255,  76]
+    NOBAIT   = [  0,   0,   0]
+    FISHING  = [ 18, 165, 213]
+    REELIN   = [ 60, 255, 204]
+    LOOT     = [  0, 255, 204]
+    INVFULL  = [  0,   0,   0]
+    FIGHT    = [120, 255, 204]
+    DEAD     = [  0,   0,   0]
 
 def _notify(event):
     for subscriber in subscribers:
@@ -20,19 +27,22 @@ class FishingMode:
     PrevMode = State.IDLE
 
 
-def loop(hue_values):
+def loop(hsv):
     """
     Executed in the start of the main loop in fishy.py
     Changes modes, calls mode events (callbacks) when mode is changed
 
-    :param hue_values: hue_values read by the bot
+    :param hsv: hsv read by the bot
     """
     FishingMode.CurrentMode = State.IDLE
     for s in State:
-        if hue_values == s.value:
+        if all(hsv == s.value):
             FishingMode.CurrentMode = s
 
-    if FishingMode.CurrentMode != FishingMode.PrevMode:
+    if FishingMode.CurrentMode == State.LOOKING:
+        _notify(FishingMode.CurrentMode)
+        time.sleep(1)
+    elif FishingMode.CurrentMode != FishingMode.PrevMode:
         _notify(FishingMode.CurrentMode)
 
     FishingMode.PrevMode = FishingMode.CurrentMode