Automatic room closing (#6)
* Ace now lets you play again if there are two players * 1 line if :o * Better Ace * Feature/automautic room closing (#5) * Room now closes when nobody is in the room * cleanup
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
using MauMau_Server.Mau;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MauMau_Server.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class MauController : ControllerBase
|
||||
{
|
||||
[HttpGet("deck")]
|
||||
public IActionResult GetDeck()
|
||||
{
|
||||
return Ok(new Deck().GetUnusedDeck().Select(card => card.ToString()).ToList());
|
||||
}
|
||||
|
||||
[HttpGet("hand")]
|
||||
public IActionResult GetHand()
|
||||
{
|
||||
return Ok(new Deck().DrawCards(8).Select(card => card.ToString()).ToList());
|
||||
}
|
||||
}
|
||||
@@ -52,11 +52,4 @@ public class RoomController : ControllerBase
|
||||
var id = _roomManager.CreateRoom();
|
||||
return Ok(JsonSerializer.Serialize(id));
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
public IActionResult Delete()
|
||||
{
|
||||
_roomManager.RemoveAllRooms();
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -83,8 +83,11 @@ public class Game
|
||||
CurrentPlayer = GetNextPlayer(2);
|
||||
break;
|
||||
case CardValue.ACE:
|
||||
TurnDirection *= -1;
|
||||
CurrentPlayer = GetNextPlayer();
|
||||
if (Players.Count > 2)
|
||||
{
|
||||
TurnDirection *= -1;
|
||||
CurrentPlayer = GetNextPlayer();
|
||||
}
|
||||
break;
|
||||
case CardValue.THREE:
|
||||
case CardValue.FOUR:
|
||||
|
||||
@@ -7,9 +7,18 @@ namespace MauMau_Server.Websockets;
|
||||
|
||||
public class Room
|
||||
{
|
||||
private readonly IRoomManager _roomManager;
|
||||
private readonly string _roomId;
|
||||
private readonly Dictionary<string, WebSocket> _connections = new();
|
||||
private string _host;
|
||||
private readonly Game _game = new();
|
||||
|
||||
public Room(IRoomManager roomManager, string roomId)
|
||||
{
|
||||
_roomManager = roomManager;
|
||||
_roomId = roomId;
|
||||
}
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = AddConnection(socket);
|
||||
@@ -30,14 +39,19 @@ public class Room
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
|
||||
await socket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
HandleDisconnect(socketId);
|
||||
}
|
||||
|
||||
private string AddConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = Guid.NewGuid().ToString();
|
||||
if (_connections.Count == 0)
|
||||
{
|
||||
_host = socketId;
|
||||
}
|
||||
|
||||
_connections.Add(socketId, socket);
|
||||
_game.AddPlayerToGame(socketId, socket);
|
||||
return socketId;
|
||||
@@ -90,6 +104,20 @@ public class Room
|
||||
socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
private void HandleDisconnect(string socketId)
|
||||
{
|
||||
RemoveConnection(socketId);
|
||||
_game.RemovePlayer(socketId);
|
||||
if (IsEmpty())
|
||||
{
|
||||
_roomManager.RemoveRoom(_roomId);
|
||||
}
|
||||
else if (socketId == _host)
|
||||
{
|
||||
_host = _connections.First().Key;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] EmptyBuffer()
|
||||
{
|
||||
return new byte[4096];
|
||||
|
||||
@@ -7,7 +7,7 @@ public class RoomManager : IRoomManager
|
||||
public string CreateRoom()
|
||||
{
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
var room = new Room();
|
||||
var room = new Room(this, roomId);
|
||||
Rooms.Add(roomId, room);
|
||||
return roomId;
|
||||
}
|
||||
@@ -31,14 +31,6 @@ public class RoomManager : IRoomManager
|
||||
{
|
||||
return Rooms.ContainsKey(roomId);
|
||||
}
|
||||
|
||||
public void RemoveAllRooms()
|
||||
{
|
||||
foreach (var room in Rooms.Where(room => room.Value.IsEmpty()))
|
||||
{
|
||||
Rooms.Remove(room.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRoomManager
|
||||
@@ -48,5 +40,4 @@ public interface IRoomManager
|
||||
public List<string> GetAllRooms();
|
||||
public void RemoveRoom(string roomId);
|
||||
public bool RoomExists(string roomId);
|
||||
public void RemoveAllRooms();
|
||||
}
|
||||
Reference in New Issue
Block a user