Skip to content

Commit

Permalink
Updates to websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Oct 27, 2020
1 parent 5c4e3d5 commit f95c530
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions SCPStats/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) =>
{
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<string, string>()
{
{"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)
{
Expand All @@ -226,6 +251,8 @@ internal static void OnRoundRestart()

internal static void Waiting()
{
ClearPlayers();

Restarting = false;
DidRoundEnd = false;
}
Expand Down

0 comments on commit f95c530

Please sign in to comment.