Partial rewrite, missing:
- Correctly parsing incoming messages - Sending the gamestate after relevant actions
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Mau;
|
||||
namespace MauMau_Server.Room.Chat;
|
||||
|
||||
public class Chat
|
||||
{
|
||||
|
||||
39
Room/Lobby.cs
Normal file
39
Room/Lobby.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using MauMau_Server.Mau;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
public class Lobby : RoomType
|
||||
{
|
||||
private readonly Room _room;
|
||||
|
||||
public Lobby(Room room)
|
||||
{
|
||||
_room = room;
|
||||
}
|
||||
|
||||
public Lobby(Room room, ConnectionInstance connection)
|
||||
{
|
||||
_room = room;
|
||||
Console.WriteLine(connection.Name + " won the game!");
|
||||
}
|
||||
|
||||
public void OnMessage(ConnectionInstance sender, string message)
|
||||
{
|
||||
// TODO: Add a way to change game settings
|
||||
if (sender == _room._host)
|
||||
{
|
||||
_room._roomType = new Game(_room, _room.Connections);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnConnect(ConnectionInstance connection)
|
||||
{
|
||||
var roomMessage = new RoomMessage("LOBBY", MessageType.INFO, _room.Connections, connection.Name + " joined!");
|
||||
}
|
||||
|
||||
public void OnDisconnect(ConnectionInstance connection)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MauMau_Server.Mau;
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
public class MessageDTO
|
||||
{
|
||||
|
||||
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
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace MauMau_Server.Websockets;
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
public class RoomManager : IRoomManager
|
||||
{
|
||||
|
||||
39
Room/RoomMessage.cs
Normal file
39
Room/RoomMessage.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
public class RoomMessage
|
||||
{
|
||||
public string CurrentRoomType;
|
||||
public string? MessageType;
|
||||
public List<string> JoinedPlayers;
|
||||
public string? Message;
|
||||
|
||||
public RoomMessage(string currentRoomType, IEnumerable<ConnectionInstance> joinedPlayers, string? message)
|
||||
{
|
||||
CurrentRoomType = currentRoomType;
|
||||
JoinedPlayers = GetNamesFromConnections(joinedPlayers);
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public RoomMessage(string currentRoomType, MessageType messageType, IEnumerable<ConnectionInstance> joinedPlayers, string? message)
|
||||
{
|
||||
CurrentRoomType = currentRoomType;
|
||||
MessageType = messageType.ToString();
|
||||
JoinedPlayers = GetNamesFromConnections(joinedPlayers);
|
||||
Message = message;
|
||||
}
|
||||
|
||||
private static List<string> GetNamesFromConnections(IEnumerable<ConnectionInstance> connections)
|
||||
{
|
||||
return connections.Select(player => player.Name).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public enum MessageType
|
||||
{
|
||||
INFO,
|
||||
SUCCES,
|
||||
WARNING,
|
||||
ERROR
|
||||
}
|
||||
38
Room/RoomType.cs
Normal file
38
Room/RoomType.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
public abstract class RoomType
|
||||
{
|
||||
protected readonly Room _room;
|
||||
|
||||
protected RoomType(Room room)
|
||||
{
|
||||
_room = room;
|
||||
}
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* This method is called when a message is received from a ConnectionInstance
|
||||
* </summary>
|
||||
* <param name="sender">The ConnectionInstance that sent the message</param>
|
||||
* <param name="message">The message received</param>
|
||||
*/
|
||||
public abstract void OnMessage(ConnectionInstance sender, string message);
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* This method is called when a new ConnectionInstance is added to the Room.
|
||||
* </summary>
|
||||
* <param name="connection">The ConnectionInstance that was added to the Room</param>
|
||||
*/
|
||||
public abstract void OnConnect(ConnectionInstance connection);
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* This method is called when a ConnectionInstance is either removed from the Room or the ConnectionInstance's WebSocket is closed.
|
||||
* </summary>
|
||||
* <param name="connection">The ConnectionInstance that was removed from the Room</param>
|
||||
*/
|
||||
public abstract void OnDisconnect(ConnectionInstance connection);
|
||||
}
|
||||
Reference in New Issue
Block a user