refactor and chat base
This commit is contained in:
125
Room/Room.cs
Normal file
125
Room/Room.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System.Collections;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using MauMau_Server.Mau;
|
||||
|
||||
namespace MauMau_Server.Websockets;
|
||||
|
||||
public class Room
|
||||
{
|
||||
private readonly IRoomManager _roomManager;
|
||||
private readonly string _roomId;
|
||||
private readonly List<ConnectionInstance> _connections = new();
|
||||
private ConnectionInstance _host;
|
||||
private readonly Chat _chat;
|
||||
private readonly Game _game = new();
|
||||
|
||||
public Room(IRoomManager roomManager, string roomId)
|
||||
{
|
||||
_roomManager = roomManager;
|
||||
_chat = new Chat(this);
|
||||
_roomId = roomId;
|
||||
}
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
var connection = AddConnection(socket);
|
||||
if (IsEmpty()) _host = connection;
|
||||
_game.AddPlayerToGame(connection);
|
||||
await HandleConnection(connection.Socket, connection.ConnectionId);
|
||||
}
|
||||
|
||||
private async Task HandleConnection(WebSocket webSocket, string connectionId)
|
||||
{
|
||||
BroadcastGameState();
|
||||
var buffer = EmptyBuffer();
|
||||
var result = await ReceiveAsync(webSocket, buffer);
|
||||
while (!result.CloseStatus.HasValue)
|
||||
{
|
||||
var slicedBuffer = buffer[0..result.Count];
|
||||
Console.WriteLine(Encoding.UTF8.GetString(slicedBuffer));
|
||||
var message = JsonSerializer.Deserialize<MessageDTO>(slicedBuffer);
|
||||
switch (message.Type)
|
||||
{
|
||||
case "GAME":
|
||||
{
|
||||
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
||||
_game.HandleAction(connectionId, gameInput);
|
||||
break;
|
||||
}
|
||||
case "CHAT":
|
||||
{
|
||||
_chat.SendChatMessage(connectionId, message.Payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
BroadcastGameState();
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(webSocket, buffer);
|
||||
}
|
||||
|
||||
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription,
|
||||
CancellationToken.None);
|
||||
HandleDisconnect(connectionId);
|
||||
}
|
||||
|
||||
private ConnectionInstance AddConnection(WebSocket socket)
|
||||
{
|
||||
var connectionId = Guid.NewGuid().ToString();
|
||||
var connection = new ConnectionInstance(connectionId, socket);
|
||||
_connections.Add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void RemoveConnection(string socketId)
|
||||
{
|
||||
_connections.RemoveAll(connection => connection.ConnectionId == 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 connection in _connections)
|
||||
{
|
||||
var gameState = new GameState(_game, connection.ConnectionId);
|
||||
var message = new MessageDTO("GAME", JsonSerializer.Serialize(gameState));
|
||||
WebsocketManager.SendAsync(connection.Socket, JsonSerializer.Serialize(message));
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDisconnect(string socketId)
|
||||
{
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
if (IsEmpty())
|
||||
{
|
||||
_roomManager.RemoveRoom(_roomId);
|
||||
}
|
||||
else if (socketId == _host.ConnectionId)
|
||||
{
|
||||
_host = _connections.First();
|
||||
}
|
||||
}
|
||||
|
||||
public List<WebSocket> GetWebsockets()
|
||||
{
|
||||
return _connections.Select(connection => connection.Socket).ToList();
|
||||
}
|
||||
|
||||
private static byte[] EmptyBuffer()
|
||||
{
|
||||
return new byte[4096];
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _connections.Count == 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user