From bae605da54e58fcdbf9a955ad41b0b54ead8012d Mon Sep 17 00:00:00 2001 From: DTieman Date: Tue, 21 Mar 2023 12:25:14 +0100 Subject: [PATCH] draw works --- Mau/ActionDTO.cs | 18 ++++++++++++++++++ Mau/Game.cs | 30 +++++++++++++++++++++++++++--- Websockets/Room.cs | 4 ++-- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 Mau/ActionDTO.cs diff --git a/Mau/ActionDTO.cs b/Mau/ActionDTO.cs new file mode 100644 index 0000000..496c375 --- /dev/null +++ b/Mau/ActionDTO.cs @@ -0,0 +1,18 @@ +namespace MauMau_Server.Mau; + +public class ActionDTO +{ + public string Action { get; set; } + public string Data { get; set; } + + public ActionDTO(string action, string data) + { + Action = action; + Data = data; + } + + public ActionDTO() + { + + } +} \ No newline at end of file diff --git a/Mau/Game.cs b/Mau/Game.cs index 1ba09f0..2cbf515 100644 --- a/Mau/Game.cs +++ b/Mau/Game.cs @@ -1,4 +1,5 @@ using System.Net.WebSockets; +using System.Text.Json; namespace MauMau_Server.Mau; @@ -32,10 +33,27 @@ public class Game Players.Remove(player); } - public void PlayCard(string playerId, Card card) + public void handleAction(string playerId, ActionDTO action) { var player = GetPlayer(playerId); if (CurrentPlayer != player) return; + switch (action.Action) + { + case "PLAYCARD": + { + var card = JsonSerializer.Deserialize(action.Data).ToCard(); + PlayCard(player, card); + break; + } + case "DRAW": + DrawCard(player); + break; + } + + } + + private void PlayCard(Player player, Card card) + { var hand = player.Hand; if (!IsCardInHand(hand, card) || !IsCardPlayable(CurrentCard, card)) return; Deck.AddCardToUsedDeck(card); @@ -43,8 +61,14 @@ public class Game CurrentCard = card; CurrentPlayer = GetNextPlayer(); } - - public Player GetNextPlayer() + + private void DrawCard(Player player) + { + player.Hand.Add(Deck.DrawCard()); + CurrentPlayer = GetNextPlayer(); + } + + private Player GetNextPlayer() { var index = Players.IndexOf(CurrentPlayer); index += TurnDirection; diff --git a/Websockets/Room.cs b/Websockets/Room.cs index b3003fd..513fb7e 100644 --- a/Websockets/Room.cs +++ b/Websockets/Room.cs @@ -24,8 +24,8 @@ public class Room while (!result.CloseStatus.HasValue) { var slicedBuffer = buffer[0..result.Count]; - var playedCard = JsonSerializer.Deserialize(slicedBuffer).ToCard(); - _game.PlayCard(socketId, playedCard); + var action = JsonSerializer.Deserialize(slicedBuffer); + _game.handleAction(socketId, action); BroadcastGameState(); buffer = EmptyBuffer(); result = await ReceiveAsync(socket, buffer);