From f95c5308d0f3c8a2b23cb4b21e3785c06c346908 Mon Sep 17 00:00:00 2001 From: PintTheDragon Date: Tue, 27 Oct 2020 00:56:05 -0700 Subject: [PATCH] Updates to websocket --- SCPStats/EventHandler.cs | 41 +++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/SCPStats/EventHandler.cs b/SCPStats/EventHandler.cs index 1af89ee..77cac47 100644 --- a/SCPStats/EventHandler.cs +++ b/SCPStats/EventHandler.cs @@ -63,8 +63,10 @@ private static string HandleId(string id) return id.Split('@')[0]; } - private static async Task CreateConnection() + private static async Task CreateConnection(int delay = 0) { + if (delay != 0) await Task.Delay(delay); + if (ws != null && ws.IsAlive) return; Pinged = false; @@ -80,7 +82,7 @@ private static async Task CreateConnection() { if(ws != null && ws.IsAlive) ws?.Close(); - ws = new WebSocket("wss://scpstats.com/plugin"); + ws = new WebSocket("wss://scpstats.com/connect"); ws.OnMessage += (sender, e) => { @@ -119,6 +121,15 @@ private static async Task CreateConnection() CreateConnection(); }; + + ws.OnError += (sender, e) => + { + Log.Warn("An error occured in SCPStats. Reconnecting in 10 seconds..."); + Log.Warn(e.Message); + + ws?.CloseAsync(); + CreateConnection(10000); + }; ws.Connect(); } @@ -193,21 +204,35 @@ private static bool IsPlayerValid(Player p, bool dnt = true, bool role = true) return !(!SCPStats.Singleton.Config.RecordTutorialStats && p.Role == RoleType.Tutorial && !playerIsSh); } - internal static void OnRoundStart() + private static async Task ClearPlayers() { - Restarting = false; - DidRoundEnd = false; + await Task.Delay(30000); + + if (Exited) return; - SendRequest("00",null); - foreach (var player in Players) { if (Player.List.All(p => p.RawUserId != player)) { + var data = new Dictionary() + { + {"playerid", HandleId(player)}, + }; + + SendRequest("09", data); + Players.Remove(player); } } } + + internal static void OnRoundStart() + { + Restarting = false; + DidRoundEnd = false; + + SendRequest("00",null); + } internal static void OnRoundEnd(RoundEndedEventArgs ev) { @@ -226,6 +251,8 @@ internal static void OnRoundRestart() internal static void Waiting() { + ClearPlayers(); + Restarting = false; DidRoundEnd = false; }