Not force closing all rooms may be a good idea
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using MauMau_Server.Websockets;
|
||||
using System.Text.Json;
|
||||
using MauMau_Server.Websockets;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MauMau_Server.Controllers;
|
||||
@@ -49,7 +50,7 @@ public class RoomController : ControllerBase
|
||||
public IActionResult Post()
|
||||
{
|
||||
var id = _roomManager.CreateRoom();
|
||||
return Ok(id);
|
||||
return Ok(JsonSerializer.Serialize(id));
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
|
||||
48
Mau/Game.cs
48
Mau/Game.cs
@@ -33,7 +33,7 @@ public class Game
|
||||
Players.Remove(player);
|
||||
}
|
||||
|
||||
public void handleAction(string playerId, ActionDTO action)
|
||||
public void HandleAction(string playerId, ActionDTO action)
|
||||
{
|
||||
var player = GetPlayer(playerId);
|
||||
if (CurrentPlayer != player) return;
|
||||
@@ -49,7 +49,6 @@ public class Game
|
||||
DrawCard(player);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void PlayCard(Player player, Card card)
|
||||
@@ -59,7 +58,46 @@ public class Game
|
||||
Deck.AddCardToUsedDeck(card);
|
||||
hand.Remove(GetSameCardFromHand(hand, card));
|
||||
CurrentCard = card;
|
||||
HandleNextPlayer(card);
|
||||
}
|
||||
|
||||
private void HandleNextPlayer(Card card)
|
||||
{
|
||||
switch (card.CardValue)
|
||||
{
|
||||
case CardValue.TWO:
|
||||
{
|
||||
var nextPlayer = GetNextPlayer();
|
||||
var cardsToDraw = Deck.DrawCards(2);
|
||||
foreach (var drawnCard in cardsToDraw)
|
||||
{
|
||||
nextPlayer.Hand.Add(drawnCard);
|
||||
}
|
||||
CurrentPlayer = GetNextPlayer(2);
|
||||
break;
|
||||
}
|
||||
case CardValue.SEVEN:
|
||||
case CardValue.KING:
|
||||
break;
|
||||
case CardValue.EIGHT:
|
||||
CurrentPlayer = GetNextPlayer(2);
|
||||
break;
|
||||
case CardValue.ACE:
|
||||
TurnDirection *= -1;
|
||||
CurrentPlayer = GetNextPlayer();
|
||||
break;
|
||||
case CardValue.THREE:
|
||||
case CardValue.FOUR:
|
||||
case CardValue.FIVE:
|
||||
case CardValue.SIX:
|
||||
case CardValue.NINE:
|
||||
case CardValue.TEN:
|
||||
case CardValue.JACK:
|
||||
case CardValue.QUEEN:
|
||||
default:
|
||||
CurrentPlayer = GetNextPlayer();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCard(Player player)
|
||||
@@ -68,12 +106,16 @@ public class Game
|
||||
CurrentPlayer = GetNextPlayer();
|
||||
}
|
||||
|
||||
private Player GetNextPlayer()
|
||||
private Player GetNextPlayer(int numberOfPlayers = 1)
|
||||
{
|
||||
var index = Players.IndexOf(CurrentPlayer);
|
||||
for (var i = 0; i < numberOfPlayers; i++)
|
||||
{
|
||||
index += TurnDirection;
|
||||
if (index >= Players.Count) index = 0;
|
||||
if (index < 0) index = Players.Count - 1;
|
||||
}
|
||||
|
||||
return Players[index];
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Room
|
||||
{
|
||||
var slicedBuffer = buffer[0..result.Count];
|
||||
var action = JsonSerializer.Deserialize<ActionDTO>(slicedBuffer);
|
||||
_game.handleAction(socketId, action);
|
||||
_game.HandleAction(socketId, action);
|
||||
BroadcastGameState();
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
@@ -58,7 +58,7 @@ public class Room
|
||||
_connections.Remove(socketId);
|
||||
}
|
||||
|
||||
private async Task<WebSocketReceiveResult?> ReceiveAsync(WebSocket webSocket, byte[] buffer)
|
||||
private static async Task<WebSocketReceiveResult?> ReceiveAsync(WebSocket webSocket, byte[] buffer)
|
||||
{
|
||||
var arraySegment = new ArraySegment<byte>(buffer);
|
||||
var result = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None);
|
||||
@@ -83,7 +83,7 @@ public class Room
|
||||
}
|
||||
}
|
||||
|
||||
private void SendAsync(WebSocket socket, string message)
|
||||
private static void SendAsync(WebSocket socket, string message)
|
||||
{
|
||||
var bytes = Encoding.Default.GetBytes(message);
|
||||
var arraySegment = new ArraySegment<byte>(bytes);
|
||||
@@ -94,4 +94,9 @@ public class Room
|
||||
{
|
||||
return new byte[4096];
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _connections.Count == 0;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,10 @@ public class RoomManager : IRoomManager
|
||||
|
||||
public void RemoveAllRooms()
|
||||
{
|
||||
Rooms.Clear();
|
||||
foreach (var room in Rooms.Where(room => room.Value.IsEmpty()))
|
||||
{
|
||||
Rooms.Remove(room.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user