Reorganize RoomHelper

This commit is contained in:
jvyden 2022-05-15 16:19:57 -04:00
commit 92d8512090
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 18 additions and 23 deletions

View file

@ -18,14 +18,6 @@ public class RoomHelper
{ {
public static readonly StorableList<Room> Rooms = RoomStore.GetRooms(); public static readonly StorableList<Room> Rooms = RoomStore.GetRooms();
public static readonly RoomSlot PodSlot = new()
{
SlotType = SlotType.Pod,
SlotId = 0,
};
private static int roomIdIncrement;
public static void StartCleanupThread() public static void StartCleanupThread()
{ {
// ReSharper disable once FunctionNeverReturns // ReSharper disable once FunctionNeverReturns
@ -42,6 +34,7 @@ public class RoomHelper
); );
} }
private static int roomIdIncrement;
internal static int RoomIdIncrement => roomIdIncrement++; internal static int RoomIdIncrement => roomIdIncrement++;
public static FindBestRoomResponse? FindBestRoom(User? user, GameVersion roomVersion, RoomSlot? slot, Platform? platform, string? location) public static FindBestRoomResponse? FindBestRoom(User? user, GameVersion roomVersion, RoomSlot? slot, Platform? platform, string? location)
@ -163,7 +156,7 @@ public class RoomHelper
RoomId = RoomIdIncrement, RoomId = RoomIdIncrement,
PlayerIds = users, PlayerIds = users,
State = RoomState.Idle, State = RoomState.Idle,
Slot = slot ?? PodSlot, Slot = slot ?? RoomSlot.PodSlot,
RoomVersion = roomVersion, RoomVersion = roomVersion,
RoomPlatform = roomPlatform, RoomPlatform = roomPlatform,
}; };
@ -177,7 +170,6 @@ public class RoomHelper
public static Room? FindRoomByUser(int userId, GameVersion roomVersion, Platform roomPlatform, bool createIfDoesNotExist = false) public static Room? FindRoomByUser(int userId, GameVersion roomVersion, Platform roomPlatform, bool createIfDoesNotExist = false)
{ {
lock(Rooms)
foreach (Room room in Rooms.Where(room => room.PlayerIds.Any(player => userId == player))) foreach (Room room in Rooms.Where(room => room.PlayerIds.Any(player => userId == player)))
return room; return room;
@ -186,7 +178,7 @@ public class RoomHelper
public static Room? FindRoomByUserId(int userId) public static Room? FindRoomByUserId(int userId)
{ {
lock(Rooms) // ReSharper disable once LoopCanBeConvertedToQuery
foreach (Room room in Rooms) foreach (Room room in Rooms)
{ {
if (room.PlayerIds.Any(p => p == userId)) if (room.PlayerIds.Any(p => p == userId))
@ -200,8 +192,6 @@ public class RoomHelper
[SuppressMessage("ReSharper", "InvertIf")] [SuppressMessage("ReSharper", "InvertIf")]
public static void CleanupRooms(int? hostId = null, Room? newRoom = null, Database? database = null) public static void CleanupRooms(int? hostId = null, Room? newRoom = null, Database? database = null)
{
lock(Rooms)
{ {
int roomCountBeforeCleanup = Rooms.Count(); int roomCountBeforeCleanup = Rooms.Count();
@ -246,4 +236,3 @@ public class RoomHelper
} }
} }
} }
}

View file

@ -6,4 +6,10 @@ public class RoomSlot
{ {
public int SlotId { get; set; } public int SlotId { get; set; }
public SlotType SlotType { get; set; } public SlotType SlotType { get; set; }
public static readonly RoomSlot PodSlot = new()
{
SlotType = SlotType.Pod,
SlotId = 0,
};
} }