Add automatic room cleanup

This commit is contained in:
jvyden 2022-02-17 16:16:55 -05:00
commit 3f29934628
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 29 additions and 0 deletions

View file

@ -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);
}
}
}
}

View file

@ -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);