Merge pull request 'developmaunt -> mauster' (#1) from developmaunt into mauster
Reviewed-on: https://git.mau-mau.nl/MauMau/MauMau-Server/pulls/1
This commit was merged in pull request #1.
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,26 +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)
|
||||||
{
|
{
|
||||||
if (HttpContext.WebSockets.IsWebSocketRequest)
|
var response = HttpContext.Response;
|
||||||
|
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
if (_roomManager.RoomExists(id))
|
response.StatusCode = 400;
|
||||||
|
await response.BodyWriter.WriteAsync("Request is not a websocket request"u8.ToArray());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_roomManager.RoomExists(id))
|
||||||
{
|
{
|
||||||
|
response.StatusCode = 404;
|
||||||
|
await response.BodyWriter.WriteAsync("Room could not be found"u8.ToArray());
|
||||||
|
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);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HttpContext.Response.StatusCode = 404;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
HttpContext.Response.StatusCode = 400;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ public enum CardType
|
|||||||
SPADES,
|
SPADES,
|
||||||
HEARTS,
|
HEARTS,
|
||||||
DIAMONDS,
|
DIAMONDS,
|
||||||
CLUBS
|
CLUBS,
|
||||||
|
JOKER
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CardValue
|
public enum CardValue
|
||||||
@@ -47,5 +48,7 @@ public enum CardValue
|
|||||||
JACK,
|
JACK,
|
||||||
QUEEN,
|
QUEEN,
|
||||||
KING,
|
KING,
|
||||||
ACE
|
ACE,
|
||||||
|
RED,
|
||||||
|
BLACK
|
||||||
}
|
}
|
||||||
20
Mau/Deck.cs
20
Mau/Deck.cs
@@ -6,15 +6,27 @@ public class Deck
|
|||||||
public List<Card> UsedDeck = new();
|
public List<Card> UsedDeck = new();
|
||||||
|
|
||||||
public Deck()
|
public Deck()
|
||||||
|
{
|
||||||
|
CreateSet();
|
||||||
|
ShuffleDeck();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateSet()
|
||||||
{
|
{
|
||||||
foreach (CardType cardType in Enum.GetValues(typeof(CardType)))
|
foreach (CardType cardType in Enum.GetValues(typeof(CardType)))
|
||||||
{
|
{
|
||||||
|
if (cardType == CardType.JOKER)
|
||||||
|
{
|
||||||
|
UnusedDeck.Add(new Card(cardType, CardValue.RED));
|
||||||
|
UnusedDeck.Add(new Card(cardType, CardValue.BLACK));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue)))
|
foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue)))
|
||||||
{
|
{
|
||||||
|
if (cardValue is CardValue.RED or CardValue.BLACK) continue;
|
||||||
UnusedDeck.Add(new Card(cardType, cardValue));
|
UnusedDeck.Add(new Card(cardType, cardValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ShuffleDeck();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Card> GetUnusedDeck()
|
public List<Card> GetUnusedDeck()
|
||||||
@@ -49,6 +61,12 @@ public class Deck
|
|||||||
{
|
{
|
||||||
UnusedDeck.AddRange(UsedDeck);
|
UnusedDeck.AddRange(UsedDeck);
|
||||||
UsedDeck.Clear();
|
UsedDeck.Clear();
|
||||||
|
|
||||||
|
if (UnusedDeck.Count == 0)
|
||||||
|
{
|
||||||
|
CreateSet();
|
||||||
|
}
|
||||||
|
|
||||||
ShuffleDeck();
|
ShuffleDeck();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
70
Mau/Game.cs
70
Mau/Game.cs
@@ -1,5 +1,4 @@
|
|||||||
using System.Net.WebSockets;
|
using System.Text.Json;
|
||||||
using System.Text.Json;
|
|
||||||
using MauMau_Server.Websockets;
|
using MauMau_Server.Websockets;
|
||||||
|
|
||||||
namespace MauMau_Server.Mau;
|
namespace MauMau_Server.Mau;
|
||||||
@@ -11,9 +10,11 @@ public class Game
|
|||||||
public List<Player> Players = new();
|
public List<Player> Players = new();
|
||||||
public Player CurrentPlayer;
|
public Player CurrentPlayer;
|
||||||
public int TurnDirection = 1;
|
public int TurnDirection = 1;
|
||||||
|
private readonly Room _room;
|
||||||
|
|
||||||
public Game()
|
public Game(Room room)
|
||||||
{
|
{
|
||||||
|
_room = room;
|
||||||
CurrentCard = Deck.DrawCard();
|
CurrentCard = Deck.DrawCard();
|
||||||
Deck.AddCardToUsedDeck(CurrentCard);
|
Deck.AddCardToUsedDeck(CurrentCard);
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,9 @@ public class Game
|
|||||||
Hand = Deck.DrawCards(8)
|
Hand = Deck.DrawCards(8)
|
||||||
};
|
};
|
||||||
Players.Add(player);
|
Players.Add(player);
|
||||||
if (Players.Count == 1) CurrentPlayer = player;
|
if (Players.Count > 1) return;
|
||||||
|
CurrentPlayer = player;
|
||||||
|
CurrentPlayer.State = PlayerState.TURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemovePlayer(string playerId)
|
public void RemovePlayer(string playerId)
|
||||||
@@ -42,11 +45,30 @@ public class Game
|
|||||||
{
|
{
|
||||||
case "PLAYCARD":
|
case "PLAYCARD":
|
||||||
{
|
{
|
||||||
|
if (player.State != PlayerState.TURN)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
var card = JsonSerializer.Deserialize<CardDTO>(action.Data).ToCard();
|
var card = JsonSerializer.Deserialize<CardDTO>(action.Data).ToCard();
|
||||||
PlayCard(player, card);
|
PlayCard(player, card);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "CHOOSE":
|
||||||
|
var choice = action.Data;
|
||||||
|
if (!Enum.TryParse(choice, out CardType cardType))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
CurrentPlayer.State = PlayerState.WAIT;
|
||||||
|
CurrentPlayer = CurrentCard.CardType == CardType.JOKER ? GetNextPlayer(2) : GetNextPlayer();
|
||||||
|
CurrentPlayer.State = PlayerState.TURN;
|
||||||
|
CurrentCard = new Card(cardType, CardValue.JACK);
|
||||||
|
break;
|
||||||
case "DRAW":
|
case "DRAW":
|
||||||
|
if (player.State != PlayerState.TURN)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
DrawCard(player);
|
DrawCard(player);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -59,6 +81,11 @@ public class Game
|
|||||||
Deck.AddCardToUsedDeck(card);
|
Deck.AddCardToUsedDeck(card);
|
||||||
hand.Remove(GetSameCardFromHand(hand, card));
|
hand.Remove(GetSameCardFromHand(hand, card));
|
||||||
CurrentCard = card;
|
CurrentCard = card;
|
||||||
|
if (hand.Count == 0)
|
||||||
|
{
|
||||||
|
_room.EndGame(player);
|
||||||
|
return;
|
||||||
|
}
|
||||||
HandleNextPlayer(card);
|
HandleNextPlayer(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,6 +93,18 @@ public class Game
|
|||||||
{
|
{
|
||||||
switch (card.CardValue)
|
switch (card.CardValue)
|
||||||
{
|
{
|
||||||
|
case CardValue.RED:
|
||||||
|
case CardValue.BLACK:
|
||||||
|
{
|
||||||
|
var nextPlayer = GetNextPlayer();
|
||||||
|
var cardsToDraw = Deck.DrawCards(5);
|
||||||
|
foreach (var drawnCard in cardsToDraw)
|
||||||
|
{
|
||||||
|
nextPlayer.Hand.Add(drawnCard);
|
||||||
|
}
|
||||||
|
CurrentPlayer.State = PlayerState.CHOOSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
case CardValue.TWO:
|
case CardValue.TWO:
|
||||||
{
|
{
|
||||||
var nextPlayer = GetNextPlayer();
|
var nextPlayer = GetNextPlayer();
|
||||||
@@ -74,40 +113,49 @@ public class Game
|
|||||||
{
|
{
|
||||||
nextPlayer.Hand.Add(drawnCard);
|
nextPlayer.Hand.Add(drawnCard);
|
||||||
}
|
}
|
||||||
CurrentPlayer = GetNextPlayer(2);
|
HandleNextPlayer(CurrentPlayer, GetNextPlayer(2));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CardValue.SEVEN:
|
case CardValue.SEVEN:
|
||||||
case CardValue.KING:
|
case CardValue.KING:
|
||||||
break;
|
break;
|
||||||
case CardValue.EIGHT:
|
case CardValue.EIGHT:
|
||||||
CurrentPlayer = GetNextPlayer(2);
|
HandleNextPlayer(CurrentPlayer, GetNextPlayer(2));
|
||||||
break;
|
break;
|
||||||
case CardValue.ACE:
|
case CardValue.ACE:
|
||||||
if (Players.Count > 2)
|
if (Players.Count > 2)
|
||||||
{
|
{
|
||||||
TurnDirection *= -1;
|
TurnDirection *= -1;
|
||||||
CurrentPlayer = GetNextPlayer();
|
HandleNextPlayer(CurrentPlayer, GetNextPlayer());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case CardValue.JACK:
|
||||||
|
CurrentPlayer.State = PlayerState.CHOOSE;
|
||||||
|
break;
|
||||||
case CardValue.THREE:
|
case CardValue.THREE:
|
||||||
case CardValue.FOUR:
|
case CardValue.FOUR:
|
||||||
case CardValue.FIVE:
|
case CardValue.FIVE:
|
||||||
case CardValue.SIX:
|
case CardValue.SIX:
|
||||||
case CardValue.NINE:
|
case CardValue.NINE:
|
||||||
case CardValue.TEN:
|
case CardValue.TEN:
|
||||||
case CardValue.JACK:
|
|
||||||
case CardValue.QUEEN:
|
case CardValue.QUEEN:
|
||||||
default:
|
default:
|
||||||
CurrentPlayer = GetNextPlayer();
|
HandleNextPlayer(CurrentPlayer, GetNextPlayer());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void HandleNextPlayer(Player current, Player next)
|
||||||
|
{
|
||||||
|
current.State = PlayerState.WAIT;
|
||||||
|
next.State = PlayerState.TURN;
|
||||||
|
CurrentPlayer = next;
|
||||||
|
}
|
||||||
|
|
||||||
private void DrawCard(Player player)
|
private void DrawCard(Player player)
|
||||||
{
|
{
|
||||||
player.Hand.Add(Deck.DrawCard());
|
player.Hand.Add(Deck.DrawCard());
|
||||||
CurrentPlayer = GetNextPlayer();
|
HandleNextPlayer(player, GetNextPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Player GetNextPlayer(int numberOfPlayers = 1)
|
private Player GetNextPlayer(int numberOfPlayers = 1)
|
||||||
@@ -135,7 +183,7 @@ public class Game
|
|||||||
|
|
||||||
private static bool IsCardPlayable(Card currentCard, Card playedCard)
|
private static bool IsCardPlayable(Card currentCard, Card playedCard)
|
||||||
{
|
{
|
||||||
return IsSameCardType(currentCard, playedCard) || IsSameCardValue(currentCard, playedCard);
|
return IsSameCardType(currentCard, playedCard) || IsSameCardValue(currentCard, playedCard) || playedCard.CardType == CardType.JOKER;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsCardInHand(IEnumerable<Card> hand, Card card)
|
private static bool IsCardInHand(IEnumerable<Card> hand, Card card)
|
||||||
|
|||||||
@@ -2,16 +2,18 @@
|
|||||||
|
|
||||||
public class GameState
|
public class GameState
|
||||||
{
|
{
|
||||||
public string PlayerName { get; set; }
|
public PlayerDTO Me { 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();
|
||||||
foreach (var card in p.Hand)
|
foreach (var card in p.Hand)
|
||||||
{
|
{
|
||||||
Hand.Add(card.ToString());
|
Hand.Add(card.ToString());
|
||||||
@@ -19,10 +21,24 @@ 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 int CardsLeft { get; set; }
|
||||||
|
|
||||||
|
public PlayerDTO(Player player)
|
||||||
|
{
|
||||||
|
Name = player.Connection.Name;
|
||||||
|
Id = player.Connection.ConnectionId;
|
||||||
|
CardsLeft = player.Hand.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
using System.Net.WebSockets;
|
using MauMau_Server.Websockets;
|
||||||
using MauMau_Server.Websockets;
|
|
||||||
|
|
||||||
namespace MauMau_Server.Mau;
|
namespace MauMau_Server.Mau;
|
||||||
|
|
||||||
public class Player
|
public class Player
|
||||||
{
|
{
|
||||||
public ConnectionInstance Connection { get; set; }
|
public ConnectionInstance Connection { get; set; }
|
||||||
|
public PlayerState State { get; set; } = PlayerState.WAIT;
|
||||||
public List<Card> Hand { get; set; } = new();
|
public List<Card> Hand { get; set; } = new();
|
||||||
|
|
||||||
public Player(ConnectionInstance connection)
|
public Player(ConnectionInstance connection)
|
||||||
|
|||||||
8
Mau/PlayerState.cs
Normal file
8
Mau/PlayerState.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace MauMau_Server.Mau;
|
||||||
|
|
||||||
|
public enum PlayerState
|
||||||
|
{
|
||||||
|
TURN,
|
||||||
|
CHOOSE,
|
||||||
|
WAIT
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Hangfire.Core" Version="1.8.12" />
|
||||||
|
<PackageReference Include="Hangfire.MemoryStorage" Version="1.8.0" />
|
||||||
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.3" />
|
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.3" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
|||||||
13
Program.cs
13
Program.cs
@@ -1,3 +1,5 @@
|
|||||||
|
using Hangfire;
|
||||||
|
using Hangfire.MemoryStorage;
|
||||||
using MauMau_Server.Websockets;
|
using MauMau_Server.Websockets;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -9,6 +11,14 @@ services.AddControllers();
|
|||||||
services.AddEndpointsApiExplorer();
|
services.AddEndpointsApiExplorer();
|
||||||
services.AddSwaggerGen();
|
services.AddSwaggerGen();
|
||||||
services.AddScoped<IRoomManager, RoomManager>();
|
services.AddScoped<IRoomManager, RoomManager>();
|
||||||
|
// var roomManager = services.BuildServiceProvider().GetRequiredService<IRoomManager>();
|
||||||
|
//
|
||||||
|
// services.AddHangfire((sp, config) =>
|
||||||
|
// {
|
||||||
|
// config.UseRecommendedSerializerSettings();
|
||||||
|
// config.UseMemoryStorage();
|
||||||
|
// });
|
||||||
|
// services.AddHangfireServer();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
@@ -28,6 +38,9 @@ var webSocketOptions = new WebSocketOptions()
|
|||||||
|
|
||||||
app.UseWebSockets(webSocketOptions);
|
app.UseWebSockets(webSocketOptions);
|
||||||
|
|
||||||
|
// var recurringJobManager = app.Services.GetRequiredService<IRecurringJobManagerV2>();
|
||||||
|
// recurringJobManager.AddOrUpdate("1", () => roomManager.ClearGhostRooms(), Cron.Hourly);
|
||||||
|
|
||||||
app.UseCors(policyBuilder =>
|
app.UseCors(policyBuilder =>
|
||||||
{
|
{
|
||||||
policyBuilder.AllowAnyOrigin();
|
policyBuilder.AllowAnyOrigin();
|
||||||
|
|||||||
@@ -13,10 +13,22 @@ 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(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 string Sender { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
|
||||||
|
public ChatMessage(string sender, string message)
|
||||||
|
{
|
||||||
|
Sender = sender;
|
||||||
|
Message = message;
|
||||||
|
}
|
||||||
|
}
|
||||||
104
Room/Room.cs
104
Room/Room.cs
@@ -1,5 +1,6 @@
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using MauMau_Server.Mau;
|
using MauMau_Server.Mau;
|
||||||
|
|
||||||
namespace MauMau_Server.Websockets;
|
namespace MauMau_Server.Websockets;
|
||||||
@@ -9,9 +10,10 @@ public class Room
|
|||||||
private readonly IRoomManager _roomManager;
|
private readonly IRoomManager _roomManager;
|
||||||
private readonly string _roomId;
|
private readonly string _roomId;
|
||||||
private readonly List<ConnectionInstance> _connections = new();
|
private readonly List<ConnectionInstance> _connections = new();
|
||||||
private ConnectionInstance _host;
|
private ConnectionInstance? _host;
|
||||||
private readonly Chat _chat;
|
private readonly Chat _chat;
|
||||||
private readonly Game _game = new();
|
private Game? _game;
|
||||||
|
private RoomState _state = RoomState.LOBBY;
|
||||||
|
|
||||||
public Room(IRoomManager roomManager, string roomId)
|
public Room(IRoomManager roomManager, string roomId)
|
||||||
{
|
{
|
||||||
@@ -20,17 +22,16 @@ 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;
|
_game?.AddPlayerToGame(connection);
|
||||||
_game.AddPlayerToGame(connection);
|
|
||||||
await HandleConnection(connection);
|
await HandleConnection(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleConnection(ConnectionInstance connection)
|
private async Task HandleConnection(ConnectionInstance connection)
|
||||||
{
|
{
|
||||||
BroadcastGameState();
|
BroadcastState();
|
||||||
var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||||
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
||||||
{
|
{
|
||||||
@@ -39,28 +40,43 @@ public class Room
|
|||||||
{
|
{
|
||||||
case "GAME":
|
case "GAME":
|
||||||
{
|
{
|
||||||
|
if (_state != RoomState.GAME) break;
|
||||||
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
||||||
_game.HandleAction(connection.ConnectionId, gameInput);
|
_game.HandleAction(connection.ConnectionId, gameInput);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "CHAT":
|
case "CHAT":
|
||||||
{
|
{
|
||||||
_chat.SendChatMessage(connection.ConnectionId, message.Payload);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BroadcastGameState();
|
BroadcastState();
|
||||||
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||||
}
|
}
|
||||||
WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result);
|
WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result);
|
||||||
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);
|
||||||
|
if (IsEmpty()) _host = connection;
|
||||||
_connections.Add(connection);
|
_connections.Add(connection);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
@@ -72,6 +88,10 @@ public class Room
|
|||||||
|
|
||||||
private void BroadcastGameState()
|
private void BroadcastGameState()
|
||||||
{
|
{
|
||||||
|
if (_game == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
foreach (var connection in _connections)
|
foreach (var connection in _connections)
|
||||||
{
|
{
|
||||||
var gameState = new GameState(_game, connection.ConnectionId);
|
var gameState = new GameState(_game, connection.ConnectionId);
|
||||||
@@ -80,10 +100,50 @@ public class Room
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
private void HandleDisconnect(string socketId)
|
||||||
{
|
{
|
||||||
RemoveConnection(socketId);
|
RemoveConnection(socketId);
|
||||||
_game.RemovePlayer(socketId);
|
_game?.RemovePlayer(socketId);
|
||||||
if (IsEmpty())
|
if (IsEmpty())
|
||||||
{
|
{
|
||||||
_roomManager.RemoveRoom(_roomId);
|
_roomManager.RemoveRoom(_roomId);
|
||||||
@@ -99,8 +159,26 @@ public class Room
|
|||||||
return _connections.Select(connection => connection.Socket).ToList();
|
return _connections.Select(connection => connection.Socket).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsEmpty()
|
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
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,16 @@ public class RoomManager : IRoomManager
|
|||||||
{
|
{
|
||||||
return Rooms.ContainsKey(roomId);
|
return Rooms.ContainsKey(roomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClearGhostRooms()
|
||||||
|
{
|
||||||
|
var ghostRooms = Rooms.Where(room => room.Value.IsEmpty());
|
||||||
|
foreach (var room in ghostRooms)
|
||||||
|
{
|
||||||
|
GC.Collect(GC.GetGeneration(room.Value));
|
||||||
|
RemoveRoom(room.Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IRoomManager
|
public interface IRoomManager
|
||||||
@@ -40,4 +50,5 @@ public interface IRoomManager
|
|||||||
public List<string> GetAllRooms();
|
public List<string> GetAllRooms();
|
||||||
public void RemoveRoom(string roomId);
|
public void RemoveRoom(string roomId);
|
||||||
public bool RoomExists(string roomId);
|
public bool RoomExists(string roomId);
|
||||||
|
public void ClearGhostRooms();
|
||||||
}
|
}
|
||||||
@@ -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