mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
Fix redis room cleanups
This commit is contained in:
parent
84065db61c
commit
c231af0936
3 changed files with 45 additions and 10 deletions
|
@ -43,7 +43,18 @@ public class Room
|
||||||
public bool IsLookingForPlayers => this.State == RoomState.PlayingLevel || this.State == RoomState.DivingInWaiting;
|
public bool IsLookingForPlayers => this.State == RoomState.PlayingLevel || this.State == RoomState.DivingInWaiting;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public int HostId => this.PlayerIds[0];
|
public int HostId {
|
||||||
|
get {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return this.PlayerIds[0];
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#nullable enable
|
#nullable enable
|
||||||
public override bool Equals(object? obj)
|
public override bool Equals(object? obj)
|
||||||
|
|
|
@ -201,45 +201,58 @@ public class RoomHelper
|
||||||
#endif
|
#endif
|
||||||
lock(RoomLock)
|
lock(RoomLock)
|
||||||
{
|
{
|
||||||
|
StorableList<Room> rooms = Rooms; // cache rooms so we dont gen a new one every time
|
||||||
|
List<Room> roomsToUpdate = new();
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Logger.Debug($"Cleaning up rooms... (took {stopwatch.ElapsedMilliseconds}ms to get lock on {nameof(RoomLock)})", LogArea.Match);
|
Logger.Debug($"Cleaning up rooms... (took {stopwatch.ElapsedMilliseconds}ms to get lock on {nameof(RoomLock)})", LogArea.Match);
|
||||||
#endif
|
#endif
|
||||||
int roomCountBeforeCleanup = Rooms.Count();
|
int roomCountBeforeCleanup = rooms.Count();
|
||||||
|
|
||||||
// Remove offline players from rooms
|
// Remove offline players from rooms
|
||||||
foreach (Room room in Rooms)
|
foreach (Room room in rooms)
|
||||||
{
|
{
|
||||||
List<User> players = room.GetPlayers(database ?? new Database());
|
List<User> players = room.GetPlayers(database ?? new Database());
|
||||||
|
|
||||||
List<int> playersToRemove = players.Where(player => player.Status.StatusType == StatusType.Offline).Select(player => player.UserId).ToList();
|
List<int> playersToRemove = players.Where(player => player.Status.StatusType == StatusType.Offline).Select(player => player.UserId).ToList();
|
||||||
|
|
||||||
foreach (int player in playersToRemove) room.PlayerIds.Remove(player);
|
foreach (int player in playersToRemove) room.PlayerIds.Remove(player);
|
||||||
|
|
||||||
|
roomsToUpdate.Add(room);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old rooms based on host
|
// Delete old rooms based on host
|
||||||
if (hostId != null)
|
if (hostId != null)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Rooms.RemoveAll(r => r.HostId == hostId);
|
rooms.RemoveAll(r => r.HostId == hostId);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// TODO: detect the room that failed and remove it
|
// TODO: detect the room that failed and remove it
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove players in this new room from other rooms
|
// Remove players in this new room from other rooms
|
||||||
if (newRoom != null)
|
if (newRoom != null)
|
||||||
foreach (Room room in Rooms)
|
foreach (Room room in rooms)
|
||||||
{
|
{
|
||||||
if (room == newRoom) continue;
|
if (room == newRoom) continue;
|
||||||
|
|
||||||
foreach (int newRoomPlayer in newRoom.PlayerIds) room.PlayerIds.RemoveAll(p => p == newRoomPlayer);
|
foreach (int newRoomPlayer in newRoom.PlayerIds) room.PlayerIds.RemoveAll(p => p == newRoomPlayer);
|
||||||
|
roomsToUpdate.Add(room);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rooms.RemoveAll(r => r.PlayerIds.Count == 0); // Remove empty rooms
|
foreach (Room room in roomsToUpdate)
|
||||||
Rooms.RemoveAll(r => r.PlayerIds.Count > 4); // Remove obviously bogus rooms
|
{
|
||||||
|
rooms.Update(room);
|
||||||
|
}
|
||||||
|
|
||||||
int roomCountAfterCleanup = Rooms.Count();
|
rooms.RemoveAll(r => r.PlayerIds.Count == 0); // Remove empty rooms
|
||||||
|
rooms.RemoveAll(r => r.HostId == -1); // Remove rooms with broken hosts
|
||||||
|
rooms.RemoveAll(r => r.PlayerIds.Count > 4); // Remove obviously bogus rooms
|
||||||
|
|
||||||
|
int roomCountAfterCleanup = rooms.Count();
|
||||||
|
|
||||||
// Log the amount of rooms cleaned up.
|
// Log the amount of rooms cleaned up.
|
||||||
// If we didnt clean any rooms, it's not useful to log in a
|
// If we didnt clean any rooms, it's not useful to log in a
|
||||||
|
@ -248,7 +261,7 @@ public class RoomHelper
|
||||||
// So, we handle that case here:
|
// So, we handle that case here:
|
||||||
int roomsCleanedUp = roomCountBeforeCleanup - roomCountAfterCleanup;
|
int roomsCleanedUp = roomCountBeforeCleanup - roomCountAfterCleanup;
|
||||||
string logText = $"Cleaned up {roomsCleanedUp} rooms.";
|
string logText = $"Cleaned up {roomsCleanedUp} rooms.";
|
||||||
|
|
||||||
if (roomsCleanedUp == 0)
|
if (roomsCleanedUp == 0)
|
||||||
{
|
{
|
||||||
Logger.Debug(logText, LogArea.Match);
|
Logger.Debug(logText, LogArea.Match);
|
||||||
|
@ -257,6 +270,16 @@ public class RoomHelper
|
||||||
{
|
{
|
||||||
Logger.Info(logText, LogArea.Match);
|
Logger.Info(logText, LogArea.Match);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logText = $"Updated {roomsToUpdate.Count} rooms.";
|
||||||
|
if (roomsToUpdate.Count == 0)
|
||||||
|
{
|
||||||
|
Logger.Debug(logText, LogArea.Match);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger.Info(logText, LogArea.Match);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Redis.OM.Searching;
|
using Redis.OM.Searching;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue