Make RoomHelper thread-safe

This commit is contained in:
jvyden 2021-12-22 17:56:39 -05:00
commit 75812988ff
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278

View file

@ -26,9 +26,15 @@ namespace LBPUnion.ProjectLighthouse.Helpers
public static FindBestRoomResponse? FindBestRoom(User user, string location) public static FindBestRoomResponse? FindBestRoom(User user, string location)
{ {
bool anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers); bool anyRoomsLookingForPlayers;
List<Room> rooms;
lock(Rooms)
{
anyRoomsLookingForPlayers = Rooms.Any(r => r.IsLookingForPlayers);
rooms = anyRoomsLookingForPlayers ? Rooms.Where(r => anyRoomsLookingForPlayers && r.IsLookingForPlayers).ToList() : Rooms;
}
List<Room> rooms = anyRoomsLookingForPlayers ? Rooms.Where(r => anyRoomsLookingForPlayers && r.IsLookingForPlayers).ToList() : Rooms;
foreach (Room room in rooms) foreach (Room room in rooms)
// Look for rooms looking for players before moving on to rooms that are idle. // Look for rooms looking for players before moving on to rooms that are idle.
{ {
@ -117,7 +123,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers
}; };
CleanupRooms(room.Host, room); CleanupRooms(room.Host, room);
Rooms.Add(room); lock(Rooms) Rooms.Add(room);
Logger.Log($"Created room (id: {room.RoomId}) for host {room.Host.Username} (id: {room.Host.UserId})", LoggerLevelMatch.Instance); Logger.Log($"Created room (id: {room.RoomId}) for host {room.Host.Username} (id: {room.Host.UserId})", LoggerLevelMatch.Instance);
return room; return room;
@ -125,7 +131,10 @@ namespace LBPUnion.ProjectLighthouse.Helpers
public static Room? FindRoomByUser(User user, bool createIfDoesNotExist = false) public static Room? FindRoomByUser(User user, bool createIfDoesNotExist = false)
{ {
foreach (Room room in Rooms.Where(room => room.Players.Any(player => user == player))) return room; lock(Rooms)
{
foreach (Room room in Rooms.Where(room => room.Players.Any(player => user == player))) return room;
}
return createIfDoesNotExist ? CreateRoom(user) : null; return createIfDoesNotExist ? CreateRoom(user) : null;
} }
@ -133,25 +142,28 @@ namespace LBPUnion.ProjectLighthouse.Helpers
[SuppressMessage("ReSharper", "InvertIf")] [SuppressMessage("ReSharper", "InvertIf")]
public static void CleanupRooms(User? host = null, Room? newRoom = null) public static void CleanupRooms(User? host = null, Room? newRoom = null)
{ {
// Delete old rooms based on host lock(Rooms)
if (host != null) {
try // Delete old rooms based on host
{ if (host != null)
Rooms.RemoveAll(r => r.Host == host); try
} {
catch Rooms.RemoveAll(r => r.Host == host);
{ }
// TODO: detect the room that failed and remove it catch
} {
// 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 (User newRoomPlayer in newRoom.Players) room.Players.RemoveAll(p => p == newRoomPlayer); foreach (User newRoomPlayer in newRoom.Players) room.Players.RemoveAll(p => p == newRoomPlayer);
} }
}
} }
} }
} }