mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-11 22:38:39 +00:00
Make RoomHelper thread-safe
This commit is contained in:
parent
b0ada585db
commit
75812988ff
1 changed files with 33 additions and 21 deletions
|
@ -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,21 +123,26 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Room? FindRoomByUser(User user, bool createIfDoesNotExist = false)
|
public static Room? FindRoomByUser(User user, bool createIfDoesNotExist = false)
|
||||||
|
{
|
||||||
|
lock(Rooms)
|
||||||
{
|
{
|
||||||
foreach (Room room in Rooms.Where(room => room.Players.Any(player => user == player))) return room;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
[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)
|
||||||
|
{
|
||||||
|
lock(Rooms)
|
||||||
{
|
{
|
||||||
// Delete old rooms based on host
|
// Delete old rooms based on host
|
||||||
if (host != null)
|
if (host != null)
|
||||||
|
@ -155,3 +166,4 @@ namespace LBPUnion.ProjectLighthouse.Helpers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue