basic logic + multiplayer
This commit is contained in:
@@ -8,7 +8,7 @@ namespace MauMau_Server.Websockets;
|
||||
public class Room
|
||||
{
|
||||
private readonly Dictionary<string, WebSocket> _connections = new();
|
||||
private Game _game = new();
|
||||
private readonly Game _game = new();
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
@@ -18,26 +18,28 @@ public class Room
|
||||
|
||||
private async Task HandleConnection(WebSocket socket, string socketId)
|
||||
{
|
||||
BroadcastAsync(JsonSerializer.Serialize(new GameState(_game)));
|
||||
BroadcastGameState();
|
||||
var buffer = EmptyBuffer();
|
||||
var result = await ReceiveAsync(socket, buffer);
|
||||
while (!result.CloseStatus.HasValue)
|
||||
{
|
||||
var slicedBuffer = buffer[0..result.Count];
|
||||
var playedCard = JsonSerializer.Deserialize<CardDTO>(slicedBuffer).ToCard();
|
||||
_game.PlayCard(playedCard);
|
||||
BroadcastAsync(JsonSerializer.Serialize(new GameState(_game)));
|
||||
_game.PlayCard(socketId, playedCard);
|
||||
BroadcastGameState();
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
await socket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
}
|
||||
|
||||
private string AddConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = Guid.NewGuid().ToString();
|
||||
_connections.Add(socketId, socket);
|
||||
_game.AddPlayerToGame(socketId, socket);
|
||||
return socketId;
|
||||
}
|
||||
|
||||
@@ -63,6 +65,16 @@ public class Room
|
||||
return result;
|
||||
}
|
||||
|
||||
private void BroadcastGameState()
|
||||
{
|
||||
foreach (var (id, socket) in GetAllConnections())
|
||||
{
|
||||
var gameState = new GameState(_game, id);
|
||||
var message = JsonSerializer.Serialize(gameState);
|
||||
SendAsync(socket, message);
|
||||
}
|
||||
}
|
||||
|
||||
private void BroadcastAsync(string message)
|
||||
{
|
||||
foreach (var (id, socket) in GetAllConnections())
|
||||
|
||||
Reference in New Issue
Block a user