Partial rewrite, missing:
- Correctly parsing incoming messages - Sending the gamestate after relevant actions
This commit is contained in:
152
Room/Room.cs
152
Room/Room.cs
@@ -1,75 +1,55 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using MauMau_Server.Mau;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Websockets;
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
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 Game? _game;
|
||||
private RoomState _state = RoomState.LOBBY;
|
||||
public readonly List<ConnectionInstance> Connections = new();
|
||||
public ConnectionInstance? _host;
|
||||
private readonly Chat.Chat _chat;
|
||||
public RoomType _roomType;
|
||||
|
||||
public Room(IRoomManager roomManager, string roomId)
|
||||
{
|
||||
_roomManager = roomManager;
|
||||
_chat = new Chat(this);
|
||||
_chat = new Chat.Chat(this);
|
||||
_roomType = new Lobby(this);
|
||||
_roomId = roomId;
|
||||
}
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket, string name)
|
||||
{
|
||||
var connection = AddConnection(socket, name);
|
||||
_game?.AddPlayerToGame(connection);
|
||||
_roomType.OnConnect(connection);
|
||||
await HandleConnection(connection);
|
||||
}
|
||||
|
||||
private async Task HandleConnection(ConnectionInstance connection)
|
||||
{
|
||||
BroadcastState();
|
||||
var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<MessageDTO>(webSocketResponse.SlicedBuffer);
|
||||
switch (message.Type)
|
||||
if (message.Type == "CHAT")
|
||||
{
|
||||
case "GAME":
|
||||
{
|
||||
if (_state != RoomState.GAME) break;
|
||||
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
||||
_game.HandleAction(connection.ConnectionId, gameInput);
|
||||
break;
|
||||
}
|
||||
case "CHAT":
|
||||
{
|
||||
var cleanedMessage = StripHTML(message.Payload);
|
||||
if (string.IsNullOrWhiteSpace(cleanedMessage))
|
||||
{
|
||||
cleanedMessage = "Mau!";
|
||||
};
|
||||
_chat.SendChatMessage(connection, cleanedMessage);
|
||||
break;
|
||||
}
|
||||
case "LOBBY":
|
||||
{
|
||||
if (connection.ConnectionId == _host?.ConnectionId)
|
||||
{
|
||||
ChangeLobbyState(RoomState.GAME);
|
||||
}
|
||||
break;
|
||||
}
|
||||
var cleanedMessage = StripHTML(message.Payload);
|
||||
if (string.IsNullOrWhiteSpace(cleanedMessage)) cleanedMessage = "Mau!";
|
||||
_chat.SendChatMessage(connection, cleanedMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
_roomType.OnMessage(connection, message.Payload);
|
||||
}
|
||||
|
||||
BroadcastState();
|
||||
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||
}
|
||||
WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result);
|
||||
HandleDisconnect(connection.ConnectionId);
|
||||
HandleDisconnect(connection);
|
||||
}
|
||||
|
||||
private ConnectionInstance AddConnection(WebSocket socket, string name)
|
||||
@@ -77,108 +57,44 @@ public class Room
|
||||
var connectionId = Guid.NewGuid().ToString();
|
||||
var connection = new ConnectionInstance(name, connectionId, socket);
|
||||
if (IsEmpty()) _host = connection;
|
||||
_connections.Add(connection);
|
||||
Connections.Add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
private void RemoveConnection(string socketId)
|
||||
private void HandleDisconnect(ConnectionInstance connection)
|
||||
{
|
||||
_connections.RemoveAll(connection => connection.ConnectionId == socketId);
|
||||
}
|
||||
|
||||
private void BroadcastGameState()
|
||||
{
|
||||
if (_game == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
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 BroadcastState()
|
||||
{
|
||||
switch (_state)
|
||||
{
|
||||
case RoomState.LOBBY:
|
||||
var message = new MessageDTO("LOBBY", JsonSerializer.Serialize("a"));
|
||||
WebsocketManager.BroadcastAsync(GetWebsockets(), JsonSerializer.Serialize(message));
|
||||
break;
|
||||
case RoomState.GAME:
|
||||
BroadcastGameState();
|
||||
break;
|
||||
default:
|
||||
//
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangeLobbyState(RoomState targetState)
|
||||
{
|
||||
switch (targetState)
|
||||
{
|
||||
case RoomState.LOBBY:
|
||||
_state = RoomState.LOBBY;
|
||||
_game = null;
|
||||
break;
|
||||
case RoomState.GAME:
|
||||
{
|
||||
_state = RoomState.GAME;
|
||||
_game = new Game(this);
|
||||
foreach (var connectionInstance in _connections)
|
||||
{
|
||||
_game.AddPlayerToGame(connectionInstance);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(targetState), targetState, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDisconnect(string socketId)
|
||||
{
|
||||
RemoveConnection(socketId);
|
||||
_game?.RemovePlayer(socketId);
|
||||
Connections.Remove(connection);
|
||||
_roomType.OnDisconnect(connection);
|
||||
if (IsEmpty())
|
||||
{
|
||||
_roomManager.RemoveRoom(_roomId);
|
||||
}
|
||||
else if (socketId == _host.ConnectionId)
|
||||
else if (connection == _host)
|
||||
{
|
||||
_host = _connections.First();
|
||||
_host = Connections.First();
|
||||
}
|
||||
}
|
||||
|
||||
public void BroadCast(RoomMessage message)
|
||||
{
|
||||
foreach (var connection in Connections)
|
||||
{
|
||||
connection.SendMessageAsync(JsonSerializer.Serialize(message));
|
||||
}
|
||||
}
|
||||
|
||||
public List<WebSocket> GetWebsockets()
|
||||
{
|
||||
return _connections.Select(connection => connection.Socket).ToList();
|
||||
return Connections.Select(connection => connection.Socket).ToList();
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _connections.Count == 0;
|
||||
return Connections.Count == 0;
|
||||
}
|
||||
|
||||
private static string StripHTML(string input)
|
||||
{
|
||||
return Regex.Replace(input, "<.*?>", string.Empty);
|
||||
}
|
||||
|
||||
public void EndGame(Player player)
|
||||
{
|
||||
var message = new MessageDTO("END", JsonSerializer.Serialize(new PlayerDTO(player)));
|
||||
WebsocketManager.BroadcastAsync(GetWebsockets(), JsonSerializer.Serialize(message));
|
||||
ChangeLobbyState(RoomState.LOBBY);
|
||||
}
|
||||
}
|
||||
|
||||
public enum RoomState
|
||||
{
|
||||
LOBBY,
|
||||
GAME
|
||||
}
|
||||
Reference in New Issue
Block a user