From ec2a8a15fcdd0b7fca578f6a5bc9681cf1978a45 Mon Sep 17 00:00:00 2001
From: Liza Carvelli <liza@carvel.li>
Date: Sat, 10 Aug 2024 20:36:43 +0200
Subject: [PATCH] Improve logging for changes in QuestController.AutomationType
 and clean up some related methods

---
 Questionable/Configuration.cs                 |   1 -
 Questionable/Controller/CommandHandler.cs     |   2 +-
 .../Controller/ContextMenuController.cs       |   2 +-
 Questionable/Controller/GameUiController.cs   |   2 +-
 Questionable/Controller/QuestController.cs    | 117 ++++++++++++------
 .../QuestComponents/ActiveQuestComponent.cs   |  15 +--
 Questionable/Windows/QuestSelectionWindow.cs  |   2 +-
 7 files changed, 87 insertions(+), 54 deletions(-)

diff --git a/Questionable/Configuration.cs b/Questionable/Configuration.cs
index 4424db23..2e833995 100644
--- a/Questionable/Configuration.cs
+++ b/Questionable/Configuration.cs
@@ -14,7 +14,6 @@ internal sealed class Configuration : IPluginConfiguration
 
     internal sealed class GeneralConfiguration
     {
-        public bool AutoAcceptNextQuest { get; set; }
         public uint MountId { get; set; } = 71;
         public GrandCompany GrandCompany { get; set; } = GrandCompany.None;
         public bool HideInAllInstances { get; set; } = true;
diff --git a/Questionable/Controller/CommandHandler.cs b/Questionable/Controller/CommandHandler.cs
index 52f7fa4d..dbf8ade5 100644
--- a/Questionable/Controller/CommandHandler.cs
+++ b/Questionable/Controller/CommandHandler.cs
@@ -75,7 +75,7 @@ internal sealed class CommandHandler : IDisposable
 
             case "start":
                 _questWindow.IsOpen = true;
-                _questController.ExecuteNextStep(QuestController.EAutomationType.Automatic);
+                _questController.Start("Start command");
                 break;
 
             case "stop":
diff --git a/Questionable/Controller/ContextMenuController.cs b/Questionable/Controller/ContextMenuController.cs
index bd7aef2d..3f65d097 100644
--- a/Questionable/Controller/ContextMenuController.cs
+++ b/Questionable/Controller/ContextMenuController.cs
@@ -146,7 +146,7 @@ internal sealed class ContextMenuController : IDisposable
                 }
             ];
             _questController.SetGatheringQuest(quest);
-            _questController.ExecuteNextStep(QuestController.EAutomationType.CurrentQuestOnly);
+            _questController.StartSingleQuest("SatisfactionSupply prepare gathering");
         }
         else
             _chatGui.PrintError($"No associated quest ({info.QuestId}).", "Questionable");
diff --git a/Questionable/Controller/GameUiController.cs b/Questionable/Controller/GameUiController.cs
index e57aef5f..9e1bd62c 100644
--- a/Questionable/Controller/GameUiController.cs
+++ b/Questionable/Controller/GameUiController.cs
@@ -417,7 +417,7 @@ internal sealed class GameUiController : IDisposable
                             return null;
 
                         _questController.GatheringQuest.SetSequence(1);
-                        _questController.ExecuteNextStep(QuestController.EAutomationType.CurrentQuestOnly);
+                        _questController.StartSingleQuest("SatisfactionSupply turn in");
                     }
 
                     return i;
diff --git a/Questionable/Controller/QuestController.cs b/Questionable/Controller/QuestController.cs
index 420c2360..562c7e52 100644
--- a/Questionable/Controller/QuestController.cs
+++ b/Questionable/Controller/QuestController.cs
@@ -93,6 +93,20 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
         _toastGui.ErrorToast += OnErrorToast;
     }
 
+    public EAutomationType AutomationType
+    {
+        get => _automationType;
+        set
+        {
+            if (value == _automationType)
+                return;
+
+            _logger.LogInformation("Setting automation type to {NewAutomationType} (previous: {OldAutomationType})",
+                value, _automationType);
+            _automationType = value;
+        }
+    }
+
     public (QuestProgress Progress, ECurrentQuestType Type)? CurrentQuestDetails
     {
         get
@@ -185,7 +199,7 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
         if (CurrentQuest != null && CurrentQuest.Quest.Root.TerritoryBlacklist.Contains(_clientState.TerritoryType))
             return;
 
-        if (_automationType == EAutomationType.Automatic &&
+        if (AutomationType == EAutomationType.Automatic &&
             ((_currentTask == null && _taskQueue.Count == 0) ||
              _currentTask is WaitAtEnd.WaitQuestAccepted)
             && CurrentQuest is { Sequence: 0, Step: 0 } or { Sequence: 0, Step: 255 }
@@ -197,7 +211,7 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                 CurrentQuest.SetStep(0);
             }
 
-            ExecuteNextStep(_automationType);
+            ExecuteNextStep();
             return;
         }
 
@@ -221,7 +235,7 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                 {
                     _startedQuest = _pendingQuest;
                     _pendingQuest = null;
-                    Stop("Pending quest accepted", continueIfAutomatic: true);
+                    CheckNextTasks("Pending quest accepted");
                 }
             }
 
@@ -260,8 +274,8 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                 if (_nextQuest.Step == 0 &&
                     _currentTask == null &&
                     _taskQueue.Count == 0 &&
-                    _automationType == EAutomationType.Automatic)
-                    ExecuteNextStep(_automationType);
+                    AutomationType == EAutomationType.Automatic)
+                    ExecuteNextStep();
             }
             else if (_gatheringQuest != null)
             {
@@ -270,8 +284,8 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                 if (_gatheringQuest.Step == 0 &&
                     _currentTask == null &&
                     _taskQueue.Count == 0 &&
-                    _automationType == EAutomationType.Automatic)
-                    ExecuteNextStep(_automationType);
+                    AutomationType == EAutomationType.Automatic)
+                    ExecuteNextStep();
             }
             else
             {
@@ -294,12 +308,14 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                         _logger.LogInformation("New quest: {QuestName}", quest.Info.Name);
                         _startedQuest = new QuestProgress(quest, currentSequence);
 
-                        bool continueAutomatically = _configuration.General.AutoAcceptNextQuest;
-
-                        if (_clientState.LocalPlayer?.Level < quest.Info.Level)
-                            continueAutomatically = false;
-
-                        Stop("Different Quest", continueAutomatically);
+                        if (_clientState.LocalPlayer!.Level < quest.Info.Level)
+                        {
+                            _logger.LogInformation("Stopping automation, player level ({PlayerLevel}) < quest level ({QuestLevel}",
+                                _clientState.LocalPlayer!.Level, quest.Info.Level);
+                            Stop("Quest level too high");
+                        }
+                        else
+                            CheckNextTasks("Different Quest");
                     }
                     else if (_startedQuest != null)
                     {
@@ -348,8 +364,7 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
             if (questToRun.Sequence != currentSequence)
             {
                 questToRun.SetSequence(currentSequence);
-                Stop($"New sequence {questToRun == _startedQuest}/{_questFunctions.GetCurrentQuestInternal()}",
-                    continueIfAutomatic: true);
+                CheckNextTasks($"New sequence {questToRun == _startedQuest}/{_questFunctions.GetCurrentQuestInternal()}");
             }
 
             var q = questToRun.Quest;
@@ -365,7 +380,7 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
             {
                 DebugState = "Step completed";
                 if (_currentTask != null || _taskQueue.Count > 0)
-                    Stop("Step complete", continueIfAutomatic: true);
+                    CheckNextTasks("Step complete");
                 return;
             }
 
@@ -429,8 +444,9 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
                 CurrentQuest.SetStep(255);
         }
 
-        if (shouldContinue && _automationType != EAutomationType.Manual)
-            ExecuteNextStep(_automationType);
+        using var scope = _logger.BeginScope("IncStepCt");
+        if (shouldContinue && AutomationType != EAutomationType.Manual)
+            ExecuteNextStep();
     }
 
     private void ClearTasksInternal()
@@ -446,33 +462,38 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
         _gatheringController.Stop("ClearTasksInternal");
     }
 
-    public void Stop(string label, bool continueIfAutomatic)
+    public override void Stop(string label)
     {
-        using var scope = _logger.BeginScope(label);
-
-        ClearTasksInternal();
-
-        // reset task queue
-        if (continueIfAutomatic && _automationType == EAutomationType.Automatic)
-        {
-            if (CurrentQuest?.Step is >= 0 and < 255)
-                ExecuteNextStep(_automationType);
-            else
-                _logger.LogInformation("Couldn't execute next step during Stop() call");
-
-            _lastTaskUpdate = DateTime.Now;
-        }
-        else if (_automationType != EAutomationType.Manual)
+        using var scope = _logger.BeginScope($"Stop/{label}");
+        if (IsRunning || AutomationType != EAutomationType.Manual)
         {
+            ClearTasksInternal();
             _logger.LogInformation("Stopping automatic questing");
-            _automationType = EAutomationType.Manual;
+            AutomationType = EAutomationType.Manual;
             _nextQuest = null;
             _gatheringQuest = null;
             _lastTaskUpdate = DateTime.Now;
         }
     }
 
-    public override void Stop(string label) => Stop(label, false);
+    private void CheckNextTasks(string label)
+    {
+        if (AutomationType == EAutomationType.Automatic)
+        {
+            using var scope = _logger.BeginScope(label);
+
+            ClearTasksInternal();
+
+            if (CurrentQuest?.Step is >= 0 and < 255)
+                ExecuteNextStep();
+            else
+                _logger.LogInformation("Couldn't execute next step during Stop() call");
+
+            _lastTaskUpdate = DateTime.Now;
+        }
+        else
+            Stop(label);
+    }
 
     public void SimulateQuest(Quest? quest)
     {
@@ -526,10 +547,30 @@ internal sealed class QuestController : MiniTaskController<QuestController>, IDi
         IncreaseStepCount(task.ElementId, task.Sequence, true);
     }
 
-    public void ExecuteNextStep(EAutomationType automatic)
+    public void Start(string label)
+    {
+        using var scope = _logger.BeginScope($"Q/{label}");
+        AutomationType = EAutomationType.Automatic;
+        ExecuteNextStep();
+    }
+
+    public void StartSingleQuest(string label)
+    {
+        using var scope = _logger.BeginScope($"SQ/{label}");
+        AutomationType = EAutomationType.CurrentQuestOnly;
+        ExecuteNextStep();
+    }
+
+    public void StartSingleStep(string label)
+    {
+        using var scope = _logger.BeginScope($"SS/{label}");
+        AutomationType = EAutomationType.Manual;
+        ExecuteNextStep();
+    }
+
+    private void ExecuteNextStep()
     {
         ClearTasksInternal();
-        _automationType = automatic;
 
         if (TryPickPriorityQuest())
             _logger.LogInformation("Using priority quest over current quest");
diff --git a/Questionable/Windows/QuestComponents/ActiveQuestComponent.cs b/Questionable/Windows/QuestComponents/ActiveQuestComponent.cs
index e36fe2a1..2e7b3e76 100644
--- a/Questionable/Windows/QuestComponents/ActiveQuestComponent.cs
+++ b/Questionable/Windows/QuestComponents/ActiveQuestComponent.cs
@@ -221,7 +221,7 @@ internal sealed class ActiveQuestComponent
             if (questProgressInfo == null)
                 _questController.SetNextQuest(currentQuest.Quest);
 
-            _questController.ExecuteNextStep(QuestController.EAutomationType.Automatic);
+            _questController.Start("UI start");
         }
 
         if (!isMinimized)
@@ -230,7 +230,7 @@ internal sealed class ActiveQuestComponent
 
             if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.StepForward, "Step"))
             {
-                _questController.ExecuteNextStep(QuestController.EAutomationType.Manual);
+                _questController.StartSingleStep("UI step");
             }
         }
 
@@ -240,8 +240,8 @@ internal sealed class ActiveQuestComponent
         if (ImGuiComponents.IconButton(FontAwesomeIcon.Stop))
         {
             _movementController.Stop();
-            _questController.Stop("Manual");
-            _gatheringController.Stop("Manual");
+            _questController.Stop("UI stop");
+            _gatheringController.Stop("UI stop");
         }
 
         if (isMinimized)
@@ -279,13 +279,6 @@ internal sealed class ActiveQuestComponent
                     _commandManager.DispatchCommand("/questinfo",
                         currentQuest.Quest.Id.ToString() ?? string.Empty, commandInfo);
             }
-
-            bool autoAcceptNextQuest = _configuration.General.AutoAcceptNextQuest;
-            if (ImGui.Checkbox("Automatically accept next quest", ref autoAcceptNextQuest))
-            {
-                _configuration.General.AutoAcceptNextQuest = autoAcceptNextQuest;
-                _pluginInterface.SavePluginConfig(_configuration);
-            }
         }
     }
 
diff --git a/Questionable/Windows/QuestSelectionWindow.cs b/Questionable/Windows/QuestSelectionWindow.cs
index 1f7c4705..48e0b333 100644
--- a/Questionable/Windows/QuestSelectionWindow.cs
+++ b/Questionable/Windows/QuestSelectionWindow.cs
@@ -237,7 +237,7 @@ internal sealed class QuestSelectionWindow : LWindow
                     if (startNextQuest)
                     {
                         _questController.SetNextQuest(knownQuest);
-                        _questController.ExecuteNextStep(QuestController.EAutomationType.Automatic);
+                        _questController.Start("QuestSelectionWindow");
                     }
 
                     ImGui.SameLine();