54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
namespace MauMau_Server.Room;
|
|
|
|
public class RoomManager : IRoomManager
|
|
{
|
|
private static readonly Dictionary<string, Room> Rooms = new();
|
|
|
|
public string CreateRoom()
|
|
{
|
|
var roomId = Guid.NewGuid().ToString();
|
|
var room = new Room(this, roomId);
|
|
Rooms.Add(roomId, room);
|
|
return roomId;
|
|
}
|
|
|
|
public Room GetRoom(string roomId)
|
|
{
|
|
return Rooms[roomId];
|
|
}
|
|
|
|
public List<string> GetAllRooms()
|
|
{
|
|
return Rooms.Keys.ToList();
|
|
}
|
|
|
|
public void RemoveRoom(string roomId)
|
|
{
|
|
Rooms.Remove(roomId);
|
|
}
|
|
|
|
public bool RoomExists(string roomId)
|
|
{
|
|
return Rooms.ContainsKey(roomId);
|
|
}
|
|
|
|
public void ClearGhostRooms()
|
|
{
|
|
var ghostRooms = Rooms.Where(room => room.Value.IsEmpty());
|
|
foreach (var room in ghostRooms)
|
|
{
|
|
GC.Collect(GC.GetGeneration(room.Value));
|
|
RemoveRoom(room.Key);
|
|
}
|
|
}
|
|
}
|
|
|
|
public interface IRoomManager
|
|
{
|
|
public string CreateRoom();
|
|
public Room GetRoom(string roomId);
|
|
public List<string> GetAllRooms();
|
|
public void RemoveRoom(string roomId);
|
|
public bool RoomExists(string roomId);
|
|
public void ClearGhostRooms();
|
|
} |