From 0f242526aa7f3e7e532d402d823b51291bb6a90d Mon Sep 17 00:00:00 2001
From: baletiballo <75846481+baletiballo@users.noreply.github.com>
Date: Thu, 5 Dec 2024 16:00:17 +0100
Subject: [PATCH] Checking for the local Server also uses
 CommunicationEvents.ServerSlots

---
 .../InteractionEngine/CommunicationEvents.cs  | 27 ++++++-------
 Assets/Scripts/StartServer.cs                 | 30 ++++++---------
 .../Scripts/UI/NetwMenu/WaitingForLocalMMT.cs | 38 +++++--------------
 3 files changed, 36 insertions(+), 59 deletions(-)

diff --git a/Assets/Scripts/InteractionEngine/CommunicationEvents.cs b/Assets/Scripts/InteractionEngine/CommunicationEvents.cs
index 6b493ec1..f0f8233e 100644
--- a/Assets/Scripts/InteractionEngine/CommunicationEvents.cs
+++ b/Assets/Scripts/InteractionEngine/CommunicationEvents.cs
@@ -34,11 +34,9 @@ public static class CommunicationEvents
     public static bool ServerRunning = true;
 
     //CHANGE HERE PORT OF SERVER
-    public static string ServerPortDefault = "8085"; //used for Local
-
-    public static string ServerAddressLocalhost = "http://localhost"; //Without Port               //Kann das weg?
-    public static string ServerAddressLocal = "http://localhost:8085"; // "http://localhost:8085"  //Kann das weg?
-    public static string ServerAdress = "http://localhost:8085"; //need "http://" //used by dispalyScrolls.cs //http://10.231.4.95:8085"; //IMPORTANT for MAINMENU
+    public static readonly string ServerPortDefault = "8085"; //used for Local
+    public static readonly string ServerAddressLocal = "localhost:" + ServerPortDefault; // there is currently never a reason to change this.
+    public static string ServerAdress = "http://localhost:8085"; //need "http://" //used by displayScrolls.cs //http://10.231.4.95:8085"; //IMPORTANT for MAINMENU
 
     public static Process process_mmt_frameIT_server;
 
@@ -55,14 +53,15 @@ public static class CommunicationEvents
     [System.Serializable]
     public enum ServerSlot
     {// The numbers are due to legacy code, specifically ServerRunningA indices
-        last  =1,
-        newIP =2,
-        slot1 =3,
-        slot2 =4,
-        slot3 =5,
-        slot4 =7,
-        slot5 =8,
-        selecIP=6 
+        last = 1,
+        newIP = 2,
+        slot1 = 3,
+        slot2 = 4,
+        slot3 = 5,
+        selecIP = 6,
+        slot4 = 7,
+        slot5 = 8,
+        localServer
     }
 
     /// <summary>
@@ -151,6 +150,8 @@ public IEnumerator UpdateServerStatus(bool skipIfAlreadyChecked = false)
         {ServerSlot.slot4, new ServerSlotData("-   -> 'Reset Options'") },
         {ServerSlot.slot5, new ServerSlotData("-   -> PRESS: 'Reset Configurations'") },
         {ServerSlot.selecIP, new ServerSlotData("") }
+        { ServerSlot.selecIP, new ServerSlotData("") },
+        { ServerSlot.localServer, new ServerSlotData(ServerAddressLocal) }
     };
 
     /// <summary>
diff --git a/Assets/Scripts/StartServer.cs b/Assets/Scripts/StartServer.cs
index afb57aa8..bf5ad6af 100644
--- a/Assets/Scripts/StartServer.cs
+++ b/Assets/Scripts/StartServer.cs
@@ -97,16 +97,14 @@ IEnumerator ServerRoutine1()
 
     IEnumerator ServerRoutine()
     {
-        CommunicationEvents.ServerAddressLocal = ServerAddressLocalhost + ":" + ServerPortDefault;
-        //print(ServerAdress);
-        //UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/scroll/list");
-        UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/fact/list");
-        yield return request.SendWebRequest();
-
-        if (request.result == UnityWebRequest.Result.ConnectionError
-         || request.result == UnityWebRequest.Result.ProtocolError)
+        // Checking the Server status got unified in CommunicationEvents
+            // UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/fact/list");
+            // yield return request.SendWebRequest();
+
+        // Make sure the server status has been checked
+        yield return ServerSlots[ServerSlot.localServer].UpdateServerStatus(skipIfAlreadyChecked: true);
+        if (ServerSlots[ServerSlot.localServer].currentStatus == ServerStatus.offline)
         {
-            UnityEngine.Debug.Log("no running server " + request.error);
 
 
 #if !UNITY_WEBGL
@@ -137,15 +135,11 @@ IEnumerator ServerRoutine()
                 yield return new WaitForSecondsRealtime(2f);
                 print("waiting");
 
-                //request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/scroll/list");
-                request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/fact/list");
-                yield return request.SendWebRequest();
-                if (request.result == UnityWebRequest.Result.ConnectionError
-                 || request.result == UnityWebRequest.Result.ProtocolError)
-                {
-                    // UnityEngine.Debug.Log("no running server");
-                }
-                else
+                yield return ServerSlots[ServerSlot.localServer].UpdateServerStatus(skipIfAlreadyChecked: false);
+
+                // The address of the localServer currently cannot change, so it is never empty/illegal
+                // If this can happen in the future, this condition may not suffice
+                if (ServerSlots[ServerSlot.localServer].currentStatus == ServerStatus.online)
                 {
                     break;
                 }
diff --git a/Assets/Scripts/UI/NetwMenu/WaitingForLocalMMT.cs b/Assets/Scripts/UI/NetwMenu/WaitingForLocalMMT.cs
index 36862caa..4ad57bf5 100644
--- a/Assets/Scripts/UI/NetwMenu/WaitingForLocalMMT.cs
+++ b/Assets/Scripts/UI/NetwMenu/WaitingForLocalMMT.cs
@@ -2,6 +2,7 @@
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.SceneManagement;
+using static CommunicationEvents;
 
 public class WaitingForLocalMMT : MonoBehaviour
 {
@@ -33,51 +34,32 @@ private void OnDisable()
 
     void PrepareGame()
     {
-        if (true)
+        //This condition should always be true if PrepareGame gets called, but it never hurts to make sure.
+        if (CommunicationEvents.ServerRunning)
         {
-
-            CommunicationEvents.ServerRunning = true;
-            //UnityEngine.Debug.Log("set server runs");
-        }
-        if (CommunicationEvents.ServerRunning == true)
-        {
-            CommunicationEvents.ServerAdress = CommunicationEvents.ServerAddressLocal;
+            CommunicationEvents.ServerAdress = "http://" + CommunicationEvents.ServerAddressLocal; //ServerAddress needs the "http://"
             //UnityEngine.Debug.Log("StartMainMenu");
             SceneManager.LoadScene("MainMenu");
         }
-
-
     }
 
     IEnumerator ServerRoutine()
     {
         while (true)
         {
+            yield return ServerSlots[ServerSlot.localServer].UpdateServerStatus(skipIfAlreadyChecked: false);
 
-
-
-            using UnityWebRequest request = pingL();
-            yield return request.SendWebRequest();
-            if (request.result == UnityWebRequest.Result.ConnectionError
-             || request.result == UnityWebRequest.Result.ProtocolError)
+            // The address of the localServer currently cannot change, so it is never empty/illegal
+            // If this can happen in the future, this condition may not suffice
+            if (ServerSlots[ServerSlot.localServer].currentStatus == ServerStatus.online)
             {
-                // UnityEngine.Debug.Log("no running server");
-            }
-            else
-            {
-                //break;
+                CommunicationEvents.ServerRunning = true;
                 PrepareGame();
             }
 
-
-
-
-
             //Wait for 2 seconds
             yield return new WaitForSecondsRealtime(2f);
             print("waiting");
-
-            yield return null;
         }
 
 
@@ -87,7 +69,7 @@ public UnityWebRequest pingL()
     {
         //UnityWebRequest request = UnityWebRequest.Get("http://" + NetwAddress + "/scroll/list");
         //using UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/scroll/list");
-        UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAddressLocal + "/fact/list");
+        UnityWebRequest request = UnityWebRequest.Get("http://" + CommunicationEvents.ServerAddressLocal + "/fact/list");
         return request;
     }
 }
\ No newline at end of file
-- 
GitLab