From dd95382d08e8735e3f5489eac440a28c9671b866 Mon Sep 17 00:00:00 2001
From: CommonLoon102 <321850+CommonLoon102@users.noreply.github.com>
Date: Sat, 25 Jan 2020 15:13:36 +0000
Subject: [PATCH] remove server from the list when it quits (#1)

* remove server from the list when it quits

* remove whitespace
---
 Supervisor/NBloodServerListener.cs         | 25 ++++++++++++++++++++++
 Supervisor/{ => Packets}/Frags.cs          |  0
 Supervisor/{ => Packets}/PacketData.cs     |  0
 Supervisor/{ => Packets}/PlayerCounts.cs   |  0
 Supervisor/{ => Packets}/PlayerNames.cs    |  0
 Supervisor/Packets/RemoveServer.cs         | 14 ++++++++++++
 WebInterface/Supervisor.runtimeconfig.json |  9 ++++++++
 WebInterface/WebInterface.csproj           |  1 +
 8 files changed, 49 insertions(+)
 rename Supervisor/{ => Packets}/Frags.cs (100%)
 rename Supervisor/{ => Packets}/PacketData.cs (100%)
 rename Supervisor/{ => Packets}/PlayerCounts.cs (100%)
 rename Supervisor/{ => Packets}/PlayerNames.cs (100%)
 create mode 100644 Supervisor/Packets/RemoveServer.cs
 create mode 100644 WebInterface/Supervisor.runtimeconfig.json

diff --git a/Supervisor/NBloodServerListener.cs b/Supervisor/NBloodServerListener.cs
index 33c0268..a384266 100644
--- a/Supervisor/NBloodServerListener.cs
+++ b/Supervisor/NBloodServerListener.cs
@@ -5,6 +5,7 @@ using System.Linq;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Threading;
 
 namespace Supervisor
 {
@@ -37,6 +38,9 @@ namespace Supervisor
                 case 'C':
                     ProcessFragsPacket(buffer);
                     break;
+                case 'D':
+                    ProcessRemovePacket(message);
+                    break;
                 default:
                     break;
             }
@@ -150,6 +154,27 @@ namespace Supervisor
             });
         }
 
+        private static void ProcessRemovePacket(string message)
+        {
+            string[] splitMessage = message.SplitMessage();
+            var packetData = new RemoveServer()
+            {
+                Port = int.Parse(splitMessage[0])
+            };
+
+            UpdateState(packetData);
+        }
+
+        private static void UpdateState(RemoveServer packetData)
+        {
+            bool isSuccess;
+            do
+            {
+                isSuccess = Program.State.Servers.TryRemove(packetData.Port, out _);
+                Thread.Sleep(TimeSpan.FromSeconds(1));
+            } while (!isSuccess);
+        }
+
         private static string GetGameType(int gameType)
         {
             return gameType switch
diff --git a/Supervisor/Frags.cs b/Supervisor/Packets/Frags.cs
similarity index 100%
rename from Supervisor/Frags.cs
rename to Supervisor/Packets/Frags.cs
diff --git a/Supervisor/PacketData.cs b/Supervisor/Packets/PacketData.cs
similarity index 100%
rename from Supervisor/PacketData.cs
rename to Supervisor/Packets/PacketData.cs
diff --git a/Supervisor/PlayerCounts.cs b/Supervisor/Packets/PlayerCounts.cs
similarity index 100%
rename from Supervisor/PlayerCounts.cs
rename to Supervisor/Packets/PlayerCounts.cs
diff --git a/Supervisor/PlayerNames.cs b/Supervisor/Packets/PlayerNames.cs
similarity index 100%
rename from Supervisor/PlayerNames.cs
rename to Supervisor/Packets/PlayerNames.cs
diff --git a/Supervisor/Packets/RemoveServer.cs b/Supervisor/Packets/RemoveServer.cs
new file mode 100644
index 0000000..b26f0b3
--- /dev/null
+++ b/Supervisor/Packets/RemoveServer.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Supervisor
+{
+    class RemoveServer : PacketData
+    {
+        public RemoveServer()
+        {
+            IsStarted = false;
+        }
+    }
+}
diff --git a/WebInterface/Supervisor.runtimeconfig.json b/WebInterface/Supervisor.runtimeconfig.json
new file mode 100644
index 0000000..a3b9e3d
--- /dev/null
+++ b/WebInterface/Supervisor.runtimeconfig.json
@@ -0,0 +1,9 @@
+{
+    "runtimeOptions": {
+        "tfm": "netcoreapp3.1",
+        "framework": {
+            "name": "Microsoft.NETCore.App",
+            "version": "3.1.0"
+        }
+    }
+}
\ No newline at end of file
diff --git a/WebInterface/WebInterface.csproj b/WebInterface/WebInterface.csproj
index c254cf2..16d315c 100644
--- a/WebInterface/WebInterface.csproj
+++ b/WebInterface/WebInterface.csproj
@@ -12,6 +12,7 @@
   <ItemGroup>
     <ProjectReference Include="..\Common\Common.csproj" />
     <ProjectReference Include="..\Model\Model.csproj" />
+    <ProjectReference Include="..\Supervisor\Supervisor.csproj" />
   </ItemGroup>