1st json over websocket test
This commit is contained in:
21
Controllers/MauController.cs
Normal file
21
Controllers/MauController.cs
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
51
Mau/Card.cs
Normal file
51
Mau/Card.cs
Normal file
@@ -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
|
||||
}
|
||||
30
Mau/CardDTO.cs
Normal file
30
Mau/CardDTO.cs
Normal file
@@ -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));
|
||||
}
|
||||
}
|
||||
40
Mau/Deck.cs
Normal file
40
Mau/Deck.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
namespace MauMau_Server.Mau;
|
||||
|
||||
public class Deck
|
||||
{
|
||||
public List<Card> 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<Card> GetUnusedDeck()
|
||||
{
|
||||
return UnusedDeck;
|
||||
}
|
||||
|
||||
public Card DrawCard()
|
||||
{
|
||||
var card = UnusedDeck[0];
|
||||
UnusedDeck.RemoveAt(0);
|
||||
return card;
|
||||
}
|
||||
|
||||
public List<Card> DrawCards(int amount)
|
||||
{
|
||||
var cards = new List<Card>();
|
||||
for (var i = 0; i < amount; i++)
|
||||
{
|
||||
cards.Add(DrawCard());
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
}
|
||||
19
Mau/HandDTO.cs
Normal file
19
Mau/HandDTO.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace MauMau_Server.Mau;
|
||||
|
||||
public class HandDTO
|
||||
{
|
||||
public List<string> _cards { get; set; } = new();
|
||||
|
||||
public HandDTO(List<Card> cards)
|
||||
{
|
||||
foreach (var card in cards)
|
||||
{
|
||||
_cards.Add(card.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public HandDTO()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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<string, WebSocket> _connections = new();
|
||||
private readonly Deck _deck = new();
|
||||
private Card currentCard;
|
||||
private List<Card> 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<CardDTO>(slicedBuffer).ToCard();
|
||||
BroadcastAsync(JsonSerializer.Serialize(playedCard.ToString()));
|
||||
SendAsync(socket, JsonSerializer.Serialize(new HandDTO(hand)));
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
@@ -59,14 +68,19 @@ public class Room
|
||||
|
||||
private void BroadcastAsync(string message)
|
||||
{
|
||||
var bytes = Encoding.Default.GetBytes(message);
|
||||
var arraySegment = new ArraySegment<byte>(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<byte>(bytes);
|
||||
socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
private static byte[] EmptyBuffer()
|
||||
{
|
||||
return new byte[4096];
|
||||
|
||||
Reference in New Issue
Block a user