Game seems to work
This commit is contained in:
50
Room/Room.cs
50
Room/Room.cs
@@ -1,6 +1,7 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using MauMau_Server.Room.Messages;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room;
|
||||
@@ -10,22 +11,24 @@ public class Room
|
||||
private readonly IRoomManager _roomManager;
|
||||
private readonly string _roomId;
|
||||
public readonly List<ConnectionInstance> Connections = new();
|
||||
public ConnectionInstance? _host;
|
||||
private readonly Chat.Chat _chat;
|
||||
public RoomType _roomType;
|
||||
public ConnectionInstance? Host;
|
||||
public RoomType RoomType;
|
||||
|
||||
public Room(IRoomManager roomManager, string roomId)
|
||||
{
|
||||
_roomManager = roomManager;
|
||||
_chat = new Chat.Chat(this);
|
||||
_roomType = new Lobby(this);
|
||||
RoomType = new Lobby(this);
|
||||
_roomId = roomId;
|
||||
}
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket, string name)
|
||||
{
|
||||
var connection = AddConnection(socket, name);
|
||||
_roomType.OnConnect(connection);
|
||||
var connectionId = Guid.NewGuid();
|
||||
var connection = new ConnectionInstance(name, connectionId, socket);
|
||||
if (IsEmpty()) Host = connection;
|
||||
Connections.Add(connection);
|
||||
|
||||
RoomType.OnConnect(connection);
|
||||
await HandleConnection(connection);
|
||||
}
|
||||
|
||||
@@ -34,16 +37,17 @@ public class Room
|
||||
var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<MessageDTO>(webSocketResponse.SlicedBuffer);
|
||||
var message = JsonSerializer.Deserialize<RoomMessage<string>>(webSocketResponse.SlicedBuffer);
|
||||
if (message.Type == "CHAT")
|
||||
{
|
||||
var cleanedMessage = StripHTML(message.Payload);
|
||||
var cleanedMessage = StripHTML(message.Data);
|
||||
if (string.IsNullOrWhiteSpace(cleanedMessage)) cleanedMessage = "Mau!";
|
||||
_chat.SendChatMessage(connection, cleanedMessage);
|
||||
var chatMessage = new ChatMessage(connection.Name, cleanedMessage);
|
||||
BroadCast(new RoomMessage<ChatMessage>("CHAT", chatMessage));
|
||||
}
|
||||
else
|
||||
{
|
||||
_roomType.OnMessage(connection, message.Payload);
|
||||
RoomType.OnMessage(connection, message);
|
||||
}
|
||||
|
||||
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||
@@ -52,30 +56,23 @@ public class Room
|
||||
HandleDisconnect(connection);
|
||||
}
|
||||
|
||||
private ConnectionInstance AddConnection(WebSocket socket, string name)
|
||||
{
|
||||
var connectionId = Guid.NewGuid().ToString();
|
||||
var connection = new ConnectionInstance(name, connectionId, socket);
|
||||
if (IsEmpty()) _host = connection;
|
||||
Connections.Add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
private void HandleDisconnect(ConnectionInstance connection)
|
||||
{
|
||||
Connections.Remove(connection);
|
||||
_roomType.OnDisconnect(connection);
|
||||
RoomType.OnDisconnect(connection);
|
||||
if (IsEmpty())
|
||||
{
|
||||
_roomManager.RemoveRoom(_roomId);
|
||||
return;
|
||||
}
|
||||
else if (connection == _host)
|
||||
|
||||
if (connection == Host)
|
||||
{
|
||||
_host = Connections.First();
|
||||
Host = Connections.First();
|
||||
}
|
||||
}
|
||||
|
||||
public void BroadCast(RoomMessage message)
|
||||
public void BroadCast<T>(RoomMessage<T> message)
|
||||
{
|
||||
foreach (var connection in Connections)
|
||||
{
|
||||
@@ -83,11 +80,6 @@ public class Room
|
||||
}
|
||||
}
|
||||
|
||||
public List<WebSocket> GetWebsockets()
|
||||
{
|
||||
return Connections.Select(connection => connection.Socket).ToList();
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return Connections.Count == 0;
|
||||
|
||||
Reference in New Issue
Block a user