diff --git a/ProjectLighthouse/Helpers/RoomHelper.cs b/ProjectLighthouse/Helpers/RoomHelper.cs index cf22d8a4..0ae4343c 100644 --- a/ProjectLighthouse/Helpers/RoomHelper.cs +++ b/ProjectLighthouse/Helpers/RoomHelper.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Threading.Tasks; using Kettu; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types; @@ -23,6 +24,22 @@ public class RoomHelper private static int roomIdIncrement; + public static void StartCleanupThread() + { + // ReSharper disable once FunctionNeverReturns + Task.Factory.StartNew + ( + async () => + { + while (true) + { + CleanupRooms(); + await Task.Delay(10000); + } + } + ); + } + internal static int RoomIdIncrement => roomIdIncrement++; public static FindBestRoomResponse? FindBestRoom(User? user, GameVersion roomVersion, string? location) @@ -167,6 +184,8 @@ public class RoomHelper { lock(Rooms) { + int roomCountBeforeCleanup = Rooms.Count; + // Remove offline players from rooms foreach (Room room in Rooms) { @@ -198,6 +217,13 @@ public class RoomHelper Rooms.RemoveAll(r => r.Players.Count == 0); // Remove empty rooms Rooms.RemoveAll(r => r.Players.Count > 4); // Remove obviously bogus rooms + + int roomCountAfterCleanup = Rooms.Count; + + if (roomCountBeforeCleanup != roomCountAfterCleanup) + { + Logger.Log($"Cleaned up {roomCountBeforeCleanup - roomCountAfterCleanup} rooms.", LoggerLevelMatch.Instance); + } } } } \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 65a7077f..0fcfaab5 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -80,6 +80,9 @@ public static class Program FileHelper.ConvertAllTexturesToPng(); + Logger.Log("Starting room cleanup thread...", LoggerLevelStartup.Instance); + RoomHelper.StartCleanupThread(); + stopwatch.Stop(); Logger.Log($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance);