draw works
This commit is contained in:
18
Mau/ActionDTO.cs
Normal file
18
Mau/ActionDTO.cs
Normal file
@@ -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()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
30
Mau/Game.cs
30
Mau/Game.cs
@@ -1,4 +1,5 @@
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace MauMau_Server.Mau;
|
namespace MauMau_Server.Mau;
|
||||||
|
|
||||||
@@ -32,10 +33,27 @@ public class Game
|
|||||||
Players.Remove(player);
|
Players.Remove(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayCard(string playerId, Card card)
|
public void handleAction(string playerId, ActionDTO action)
|
||||||
{
|
{
|
||||||
var player = GetPlayer(playerId);
|
var player = GetPlayer(playerId);
|
||||||
if (CurrentPlayer != player) return;
|
if (CurrentPlayer != player) return;
|
||||||
|
switch (action.Action)
|
||||||
|
{
|
||||||
|
case "PLAYCARD":
|
||||||
|
{
|
||||||
|
var card = JsonSerializer.Deserialize<CardDTO>(action.Data).ToCard();
|
||||||
|
PlayCard(player, card);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "DRAW":
|
||||||
|
DrawCard(player);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PlayCard(Player player, Card card)
|
||||||
|
{
|
||||||
var hand = player.Hand;
|
var hand = player.Hand;
|
||||||
if (!IsCardInHand(hand, card) || !IsCardPlayable(CurrentCard, card)) return;
|
if (!IsCardInHand(hand, card) || !IsCardPlayable(CurrentCard, card)) return;
|
||||||
Deck.AddCardToUsedDeck(card);
|
Deck.AddCardToUsedDeck(card);
|
||||||
@@ -43,8 +61,14 @@ public class Game
|
|||||||
CurrentCard = card;
|
CurrentCard = card;
|
||||||
CurrentPlayer = GetNextPlayer();
|
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);
|
var index = Players.IndexOf(CurrentPlayer);
|
||||||
index += TurnDirection;
|
index += TurnDirection;
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ public class Room
|
|||||||
while (!result.CloseStatus.HasValue)
|
while (!result.CloseStatus.HasValue)
|
||||||
{
|
{
|
||||||
var slicedBuffer = buffer[0..result.Count];
|
var slicedBuffer = buffer[0..result.Count];
|
||||||
var playedCard = JsonSerializer.Deserialize<CardDTO>(slicedBuffer).ToCard();
|
var action = JsonSerializer.Deserialize<ActionDTO>(slicedBuffer);
|
||||||
_game.PlayCard(socketId, playedCard);
|
_game.handleAction(socketId, action);
|
||||||
BroadcastGameState();
|
BroadcastGameState();
|
||||||
buffer = EmptyBuffer();
|
buffer = EmptyBuffer();
|
||||||
result = await ReceiveAsync(socket, buffer);
|
result = await ReceiveAsync(socket, buffer);
|
||||||
|
|||||||
Reference in New Issue
Block a user