Feature/automautic room closing (#5)
* Room now closes when nobody is in the room * cleanup
This commit is contained in:
@@ -7,9 +7,18 @@ namespace MauMau_Server.Websockets;
|
||||
|
||||
public class Room
|
||||
{
|
||||
private readonly IRoomManager _roomManager;
|
||||
private readonly string _roomId;
|
||||
private readonly Dictionary<string, WebSocket> _connections = new();
|
||||
private string _host;
|
||||
private readonly Game _game = new();
|
||||
|
||||
public Room(IRoomManager roomManager, string roomId)
|
||||
{
|
||||
_roomManager = roomManager;
|
||||
_roomId = roomId;
|
||||
}
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = AddConnection(socket);
|
||||
@@ -30,41 +39,46 @@ public class Room
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
|
||||
await socket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
HandleDisconnect(socketId);
|
||||
}
|
||||
|
||||
private string AddConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = Guid.NewGuid().ToString();
|
||||
if (_connections.Count == 0)
|
||||
{
|
||||
_host = socketId;
|
||||
}
|
||||
|
||||
_connections.Add(socketId, socket);
|
||||
_game.AddPlayerToGame(socketId, socket);
|
||||
return socketId;
|
||||
}
|
||||
|
||||
|
||||
public WebSocket GetConnection(string socketId)
|
||||
{
|
||||
return _connections[socketId];
|
||||
}
|
||||
|
||||
|
||||
public Dictionary<string, WebSocket> GetAllConnections()
|
||||
{
|
||||
return _connections;
|
||||
}
|
||||
|
||||
|
||||
public void RemoveConnection(string socketId)
|
||||
{
|
||||
_connections.Remove(socketId);
|
||||
}
|
||||
|
||||
|
||||
private static async Task<WebSocketReceiveResult?> ReceiveAsync(WebSocket webSocket, byte[] buffer)
|
||||
{
|
||||
var arraySegment = new ArraySegment<byte>(buffer);
|
||||
var result = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private void BroadcastGameState()
|
||||
{
|
||||
foreach (var (id, socket) in GetAllConnections())
|
||||
@@ -74,7 +88,7 @@ public class Room
|
||||
SendAsync(socket, message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void BroadcastAsync(string message)
|
||||
{
|
||||
foreach (var (id, socket) in GetAllConnections())
|
||||
@@ -82,7 +96,7 @@ public class Room
|
||||
SendAsync(socket, message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void SendAsync(WebSocket socket, string message)
|
||||
{
|
||||
var bytes = Encoding.Default.GetBytes(message);
|
||||
@@ -90,11 +104,25 @@ public class Room
|
||||
socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
private void HandleDisconnect(string socketId)
|
||||
{
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
if (IsEmpty())
|
||||
{
|
||||
_roomManager.RemoveRoom(_roomId);
|
||||
}
|
||||
else if (socketId == _host)
|
||||
{
|
||||
_host = _connections.First().Key;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] EmptyBuffer()
|
||||
{
|
||||
return new byte[4096];
|
||||
}
|
||||
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _connections.Count == 0;
|
||||
|
||||
Reference in New Issue
Block a user