diff --git a/Controllers/MauController.cs b/Controllers/MauController.cs new file mode 100644 index 0000000..acfc789 --- /dev/null +++ b/Controllers/MauController.cs @@ -0,0 +1,21 @@ +using MauMau_Server.Mau; +using Microsoft.AspNetCore.Mvc; + +namespace MauMau_Server.Controllers; + +[ApiController] +[Route("[controller]")] +public class DeckController : 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()); + } +} \ No newline at end of file diff --git a/Mau/Card.cs b/Mau/Card.cs new file mode 100644 index 0000000..7e5ad7e --- /dev/null +++ b/Mau/Card.cs @@ -0,0 +1,51 @@ +namespace MauMau_Server.Mau; + +public class Card +{ + public readonly CardType CardType; + public readonly CardValue CardValue; + + public Card(CardType cardType, CardValue cardValue) + { + CardType = cardType; + CardValue = cardValue; + } + + public override string ToString() + { + return $"{CardType} {CardValue}"; + } + + public Card parseCard(string card) + { + var cardType = card.Split(" ")[0]; + var cardValue = card.Split(" ")[1]; + return new Card((CardType)Enum.Parse(typeof(CardType), cardType), + (CardValue)Enum.Parse(typeof(CardValue), cardValue)); + } +} + +public enum CardType +{ + SPADES, + HEARTS, + DIAMONDS, + CLUBS +} + +public enum CardValue +{ + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + TEN, + JACK, + QUEEN, + KING, + ACE +} \ No newline at end of file diff --git a/Mau/CardDTO.cs b/Mau/CardDTO.cs new file mode 100644 index 0000000..91334b7 --- /dev/null +++ b/Mau/CardDTO.cs @@ -0,0 +1,30 @@ +namespace MauMau_Server.Mau; + +public class CardDTO +{ + public string CardType { get; set; } + public string CardValue { get; set; } + + public CardDTO(Card card) + { + CardType = card.CardType.ToString(); + CardValue = card.CardValue.ToString(); + } + + public CardDTO(string cardType, string cardValue) + { + CardType = cardType; + CardValue = cardValue; + } + + public CardDTO() + { + + } + + public Card ToCard() + { + return new Card((CardType)Enum.Parse(typeof(CardType), CardType), + (CardValue)Enum.Parse(typeof(CardValue), CardValue)); + } +} \ No newline at end of file diff --git a/Mau/Deck.cs b/Mau/Deck.cs new file mode 100644 index 0000000..13ddd16 --- /dev/null +++ b/Mau/Deck.cs @@ -0,0 +1,40 @@ +namespace MauMau_Server.Mau; + +public class Deck +{ + public List UnusedDeck = new(); + + public Deck() + { + foreach (CardType cardType in Enum.GetValues(typeof(CardType))) + { + foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue))) + { + UnusedDeck.Add(new Card(cardType, cardValue)); + } + } + UnusedDeck = UnusedDeck.OrderBy(x => Guid.NewGuid()).ToList(); + } + + public List GetUnusedDeck() + { + return UnusedDeck; + } + + public Card DrawCard() + { + var card = UnusedDeck[0]; + UnusedDeck.RemoveAt(0); + return card; + } + + public List DrawCards(int amount) + { + var cards = new List(); + for (var i = 0; i < amount; i++) + { + cards.Add(DrawCard()); + } + return cards; + } +} \ No newline at end of file diff --git a/Mau/HandDTO.cs b/Mau/HandDTO.cs new file mode 100644 index 0000000..ca69185 --- /dev/null +++ b/Mau/HandDTO.cs @@ -0,0 +1,19 @@ +namespace MauMau_Server.Mau; + +public class HandDTO +{ + public List _cards { get; set; } = new(); + + public HandDTO(List cards) + { + foreach (var card in cards) + { + _cards.Add(card.ToString()); + } + } + + public HandDTO() + { + + } +} \ No newline at end of file diff --git a/Websockets/Room.cs b/Websockets/Room.cs index c511148..af12bde 100644 --- a/Websockets/Room.cs +++ b/Websockets/Room.cs @@ -1,11 +1,16 @@ using System.Net.WebSockets; using System.Text; +using System.Text.Json; +using MauMau_Server.Mau; namespace MauMau_Server.Websockets; public class Room { private readonly Dictionary _connections = new(); + private readonly Deck _deck = new(); + private Card currentCard; + private List hand = new(); public async Task InstantiateConnection(WebSocket socket) { @@ -15,12 +20,16 @@ public class Room private async Task HandleConnection(WebSocket socket, string socketId) { + hand = _deck.DrawCards(8); + SendAsync(socket, JsonSerializer.Serialize(new HandDTO(hand))); var buffer = EmptyBuffer(); var result = await ReceiveAsync(socket, buffer); while (!result.CloseStatus.HasValue) { - var message = $"{socketId}: {Encoding.Default.GetString(buffer)}"; - BroadcastAsync(message); + var slicedBuffer = buffer[0..result.Count]; + var playedCard = JsonSerializer.Deserialize(slicedBuffer).ToCard(); + BroadcastAsync(JsonSerializer.Serialize(playedCard.ToString())); + SendAsync(socket, JsonSerializer.Serialize(new HandDTO(hand))); buffer = EmptyBuffer(); result = await ReceiveAsync(socket, buffer); } @@ -59,13 +68,18 @@ public class Room private void BroadcastAsync(string message) { - var bytes = Encoding.Default.GetBytes(message); - var arraySegment = new ArraySegment(bytes); foreach (var (id, socket) in GetAllConnections()) { - socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None); + SendAsync(socket, message); } } + + private void SendAsync(WebSocket socket, string message) + { + var bytes = Encoding.Default.GetBytes(message); + var arraySegment = new ArraySegment(bytes); + socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None); + } private static byte[] EmptyBuffer() { diff --git a/mau.json b/mau.json new file mode 100644 index 0000000..60c5d52 --- /dev/null +++ b/mau.json @@ -0,0 +1 @@ +{"CardType":"SPADES","CardValue":"THREE"} \ No newline at end of file