Custom names and time in chat
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using MauMau_Server.Websockets;
|
using MauMau_Server.Websockets;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
@@ -24,24 +25,27 @@ public class RoomController : ControllerBase
|
|||||||
return Ok(rooms);
|
return Ok(rooms);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}/{name}")]
|
||||||
public async Task ConnectToRoom(string id)
|
public async Task ConnectToRoom(string id, string name)
|
||||||
{
|
{
|
||||||
|
var response = HttpContext.Response;
|
||||||
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
HttpContext.Response.StatusCode = 400;
|
response.StatusCode = 400;
|
||||||
|
await response.BodyWriter.WriteAsync("Request is not a websocket request"u8.ToArray());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_roomManager.RoomExists(id))
|
if (!_roomManager.RoomExists(id))
|
||||||
{
|
{
|
||||||
HttpContext.Response.StatusCode = 404;
|
response.StatusCode = 404;
|
||||||
|
await response.BodyWriter.WriteAsync("Room could not be found"u8.ToArray());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
||||||
var room = _roomManager.GetRoom(id);
|
var room = _roomManager.GetRoom(id);
|
||||||
await room.InstantiateConnection(webSocket);
|
await room.InstantiateConnection(webSocket, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
|||||||
@@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
public class GameState
|
public class GameState
|
||||||
{
|
{
|
||||||
public string PlayerName { get; set; }
|
public PlayerDTO Me { get; set; }
|
||||||
public string CurrentState { get; set; }
|
public string CurrentState { get; set; }
|
||||||
public List<string> Hand { get; set; } = new();
|
public List<string> Hand { get; set; } = new();
|
||||||
public string CurrentCard { get; set; }
|
public string CurrentCard { get; set; }
|
||||||
public string CurrentPlayer { get; set; }
|
public PlayerDTO CurrentPlayer { get; set; }
|
||||||
public List<string> Players { get; set; } = new();
|
public List<PlayerDTO> Players { get; set; } = new();
|
||||||
|
|
||||||
public GameState(Game game, string playerId)
|
public GameState(Game game, string playerId)
|
||||||
{
|
{
|
||||||
var p = game.GetPlayer(playerId);
|
var p = game.GetPlayer(playerId);
|
||||||
PlayerName = p.Connection.ConnectionId;
|
Me = new PlayerDTO(game.GetPlayer(playerId));
|
||||||
CurrentState = p.State.ToString();
|
CurrentState = p.State.ToString();
|
||||||
foreach (var card in p.Hand)
|
foreach (var card in p.Hand)
|
||||||
{
|
{
|
||||||
@@ -21,10 +21,22 @@ public class GameState
|
|||||||
|
|
||||||
foreach (var player in game.Players)
|
foreach (var player in game.Players)
|
||||||
{
|
{
|
||||||
Players.Add(player.Connection.ConnectionId);
|
Players.Add(new PlayerDTO(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
CurrentCard = game.CurrentCard.ToString();
|
CurrentCard = game.CurrentCard.ToString();
|
||||||
CurrentPlayer = game.CurrentPlayer.Connection.ConnectionId;
|
CurrentPlayer = new PlayerDTO(game.CurrentPlayer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PlayerDTO
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
public PlayerDTO(Player player)
|
||||||
|
{
|
||||||
|
Name = player.Connection.Name;
|
||||||
|
Id = player.Connection.ConnectionId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,10 +13,24 @@ public class Chat
|
|||||||
_room = room;
|
_room = room;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendChatMessage(string connectionId, string message)
|
public void SendChatMessage(ConnectionInstance connection, string message)
|
||||||
{
|
{
|
||||||
var chatMessage = new ChatOutput(connectionId, message);
|
var chatMessage = new ChatMessage(DateTime.UtcNow, connection.Name, message);
|
||||||
var formattedMessage = new MessageDTO("CHAT", JsonSerializer.Serialize(chatMessage));
|
var formattedMessage = new MessageDTO("CHAT", JsonSerializer.Serialize(chatMessage));
|
||||||
WebsocketManager.BroadcastAsync(_room.GetWebsockets(), JsonSerializer.Serialize(formattedMessage));
|
WebsocketManager.BroadcastAsync(_room.GetWebsockets(), JsonSerializer.Serialize(formattedMessage));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ChatMessage
|
||||||
|
{
|
||||||
|
public DateTime Time { get; set; }
|
||||||
|
public string Sender { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
public ChatMessage(DateTime dateTime, string sender, string message)
|
||||||
|
{
|
||||||
|
Time = dateTime;
|
||||||
|
Sender = sender;
|
||||||
|
Message = message;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
10
Room/Room.cs
10
Room/Room.cs
@@ -20,9 +20,9 @@ public class Room
|
|||||||
_roomId = roomId;
|
_roomId = roomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task InstantiateConnection(WebSocket socket)
|
public async Task InstantiateConnection(WebSocket socket, string name)
|
||||||
{
|
{
|
||||||
var connection = AddConnection(socket);
|
var connection = AddConnection(socket, name);
|
||||||
if (IsEmpty()) _host = connection;
|
if (IsEmpty()) _host = connection;
|
||||||
_game.AddPlayerToGame(connection);
|
_game.AddPlayerToGame(connection);
|
||||||
await HandleConnection(connection);
|
await HandleConnection(connection);
|
||||||
@@ -45,7 +45,7 @@ public class Room
|
|||||||
}
|
}
|
||||||
case "CHAT":
|
case "CHAT":
|
||||||
{
|
{
|
||||||
_chat.SendChatMessage(connection.ConnectionId, message.Payload);
|
_chat.SendChatMessage(connection, message.Payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,10 +57,10 @@ public class Room
|
|||||||
HandleDisconnect(connection.ConnectionId);
|
HandleDisconnect(connection.ConnectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnectionInstance AddConnection(WebSocket socket)
|
private ConnectionInstance AddConnection(WebSocket socket, string name)
|
||||||
{
|
{
|
||||||
var connectionId = Guid.NewGuid().ToString();
|
var connectionId = Guid.NewGuid().ToString();
|
||||||
var connection = new ConnectionInstance(connectionId, socket);
|
var connection = new ConnectionInstance(name, connectionId, socket);
|
||||||
_connections.Add(connection);
|
_connections.Add(connection);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ namespace MauMau_Server.Websockets;
|
|||||||
|
|
||||||
public class ConnectionInstance
|
public class ConnectionInstance
|
||||||
{
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
public string ConnectionId { get; set; }
|
public string ConnectionId { get; set; }
|
||||||
public WebSocket Socket { get; set; }
|
public WebSocket Socket { get; set; }
|
||||||
|
|
||||||
public ConnectionInstance(string connectionId, WebSocket socket)
|
public ConnectionInstance(string name, string connectionId, WebSocket socket)
|
||||||
{
|
{
|
||||||
|
Name = name;
|
||||||
ConnectionId = connectionId;
|
ConnectionId = connectionId;
|
||||||
Socket = socket;
|
Socket = socket;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user