Dotnet version upgrade
This commit is contained in:
67
Mau/Game.cs
67
Mau/Game.cs
@@ -1,9 +1,9 @@
|
||||
using MauMau_Server.Mau.GameMessages;
|
||||
using System.Text.Json;
|
||||
using MauMau_Server.Mau.GameMessages;
|
||||
using MauMau_Server.Mau.Managers;
|
||||
using MauMau_Server.Websockets;
|
||||
using MauMau_Server.Room;
|
||||
using MauMau_Server.Room.Messages;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MauMau_Server.Mau;
|
||||
|
||||
@@ -12,11 +12,11 @@ public class Game : RoomType
|
||||
// Helpers
|
||||
private readonly Deck _deck = new();
|
||||
private readonly TurnManager _turnManager = new();
|
||||
|
||||
|
||||
// Game state
|
||||
private readonly List<Card> _mauCardBuffer = new();
|
||||
private CardType? NextAllowedCardType { get; set; }
|
||||
|
||||
|
||||
// Variables
|
||||
private const int NumberOfFaultcards = 5;
|
||||
private const int NumberOfStartCards = 8;
|
||||
@@ -34,7 +34,7 @@ public class Game : RoomType
|
||||
NextAllowedCardType = (CardType?)cardTypes.GetValue(randomIndex) ?? CardType.SPADES;
|
||||
} while (NextAllowedCardType == CardType.JOKER);
|
||||
}
|
||||
|
||||
|
||||
// Convert all the connections to players
|
||||
List<Player> players = new();
|
||||
foreach (var player in connections.Select(connection => new Player(connection)))
|
||||
@@ -47,7 +47,7 @@ public class Game : RoomType
|
||||
|
||||
// Add all the players to the turn manager
|
||||
_turnManager.Initialize(players);
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class Game : RoomType
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Get the player that sent the message
|
||||
var player = _turnManager.Players.FirstOrDefault(x => x.IsMe(sender.Id));
|
||||
|
||||
@@ -92,13 +92,13 @@ public class Game : RoomType
|
||||
// Broadcast that a player joined
|
||||
var joinMessage = new JoinMessage(_room.Connections, connection);
|
||||
_room.BroadCast(new RoomMessage<JoinMessage>("JOIN", joinMessage));
|
||||
|
||||
|
||||
// Create a new player, give them a new hand and add them to the game
|
||||
var player = new Player(connection);
|
||||
var initialHand = _deck.DrawCards(8);
|
||||
player.GiveCards(initialHand);
|
||||
_turnManager.Players.Add(player);
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -111,15 +111,15 @@ public class Game : RoomType
|
||||
// Broadcast that the player left
|
||||
var leaveMessage = new LeaveMessage(connection);
|
||||
_room.BroadCast(new RoomMessage<LeaveMessage>("LEAVE", leaveMessage));
|
||||
|
||||
|
||||
// Get the player that left
|
||||
var player = _turnManager.Players.FirstOrDefault(x => x.IsMe(connection.Id));
|
||||
if (player is null) return;
|
||||
|
||||
|
||||
// Add the player's hand to the used deck
|
||||
var playerHand = player.Hand;
|
||||
_deck.AddCardsToUsedDeck(playerHand);
|
||||
|
||||
|
||||
// Change the turn if the player that left was the current player
|
||||
if (player == _turnManager.CurrentPlayer)
|
||||
{
|
||||
@@ -128,7 +128,7 @@ public class Game : RoomType
|
||||
|
||||
// Remove the player from the game
|
||||
_turnManager.Players.Remove(player);
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -142,6 +142,8 @@ public class Game : RoomType
|
||||
*/
|
||||
private void Choose(Player player, string data)
|
||||
{
|
||||
// TODO: Validate if choosing a card is allowed
|
||||
|
||||
// Convert the data to a CardType, if it fails, ignore the message
|
||||
if (!Enum.TryParse(data, out CardType cardType))
|
||||
{
|
||||
@@ -153,7 +155,7 @@ public class Game : RoomType
|
||||
|
||||
// Change the turns
|
||||
_turnManager.ChangeTurnTo();
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -168,7 +170,6 @@ public class Game : RoomType
|
||||
* When there are multiple mau cards played, the player has to draw the combined amount of mau cards played.
|
||||
* </summary>
|
||||
* <param name="player">The player that drew a card</param>
|
||||
* <param name="data">A string that can be serialized to a drawcard instance</param>
|
||||
*/
|
||||
private void Draw(Player player)
|
||||
{
|
||||
@@ -178,13 +179,13 @@ public class Game : RoomType
|
||||
{
|
||||
// Count the amount of cards that need to be drawn
|
||||
var totalCards = CountMauCardBuffer();
|
||||
|
||||
|
||||
// Draw the cards from the deck
|
||||
var drawnCards = _deck.DrawCards(totalCards);
|
||||
|
||||
|
||||
// Give the cards to the player
|
||||
player.GiveCards(drawnCards);
|
||||
|
||||
|
||||
// Change the turn
|
||||
_turnManager.ChangeTurnTo();
|
||||
}
|
||||
@@ -202,7 +203,7 @@ public class Game : RoomType
|
||||
_turnManager.ChangeTurnTo();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -228,7 +229,7 @@ public class Game : RoomType
|
||||
}
|
||||
|
||||
// Convert the data to a Card instance
|
||||
var cardData = JsonConvert.DeserializeObject<PlayCard>(data).ToCard();
|
||||
var cardData = JsonSerializer.Deserialize<PlayCard>(data).ToCard();
|
||||
|
||||
// Check if the player indeed has the card they claim to have
|
||||
var playerCard = player.TakeCardFromHand(cardData);
|
||||
@@ -238,7 +239,7 @@ public class Game : RoomType
|
||||
}
|
||||
|
||||
if (!CardCanBePlayed(playerCard)) return;
|
||||
|
||||
|
||||
// Remove the card from the player's hand
|
||||
player.Hand.Remove(playerCard);
|
||||
|
||||
@@ -251,7 +252,7 @@ public class Game : RoomType
|
||||
|
||||
// Add the card to the used deck
|
||||
_deck.AddCardToUsedDeck(playerCard);
|
||||
|
||||
|
||||
// Reset the allowed card type, so the next player has the normal same type and same value rules
|
||||
NextAllowedCardType = null;
|
||||
|
||||
@@ -264,7 +265,7 @@ public class Game : RoomType
|
||||
|
||||
// Change the turn based on the played card
|
||||
HandleNextPlayer(playerCard);
|
||||
|
||||
|
||||
// Broadcast new game state
|
||||
SendGameState();
|
||||
}
|
||||
@@ -304,6 +305,7 @@ public class Game : RoomType
|
||||
_turnManager.ChangeDirection();
|
||||
_turnManager.ChangeTurnTo();
|
||||
}
|
||||
|
||||
break;
|
||||
case CardValue.JACK:
|
||||
_turnManager.CurrentPlayer.State = PlayerState.CHOOSE;
|
||||
@@ -345,9 +347,10 @@ public class Game : RoomType
|
||||
if (NextAllowedCardType != null)
|
||||
{
|
||||
// If so, the card must be the allowed card type, a joker or the same value as the current card
|
||||
return card.CardType == NextAllowedCardType || card.CardType == CardType.JOKER || card.CardValue == _deck.CurrentCard.CardValue;
|
||||
return card.CardType == NextAllowedCardType || card.CardType == CardType.JOKER ||
|
||||
card.CardValue == _deck.CurrentCard.CardValue;
|
||||
}
|
||||
|
||||
|
||||
// Otherwise, use the normal rules
|
||||
return card.CanBePlayedOn(_deck.CurrentCard);
|
||||
}
|
||||
@@ -366,15 +369,13 @@ public class Game : RoomType
|
||||
if (card.CardType == CardType.JOKER)
|
||||
{
|
||||
totalCards += 5;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (card.CardValue == CardValue.TWO)
|
||||
else if (card.CardValue == CardValue.TWO)
|
||||
{
|
||||
totalCards += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
_mauCardBuffer.Clear();
|
||||
return totalCards;
|
||||
}
|
||||
@@ -389,8 +390,10 @@ public class Game : RoomType
|
||||
{
|
||||
foreach (var player in _turnManager.Players)
|
||||
{
|
||||
var gameState = new GameState(player, _deck.CurrentCard, NextAllowedCardType, _turnManager.CurrentPlayer, _turnManager.Players);
|
||||
player.Connection.SendMessageAsync(JsonConvert.SerializeObject(new RoomMessage<GameState>("GAME", gameState)));
|
||||
var gameState = new GameState(player, _deck.CurrentCard, NextAllowedCardType, _turnManager.CurrentPlayer,
|
||||
_turnManager.Players);
|
||||
player.Connection.SendMessageAsync(
|
||||
JsonSerializer.Serialize(new RoomMessage<GameState>("GAME", gameState)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,7 +405,7 @@ public class Game : RoomType
|
||||
*/
|
||||
private void EndGame(Player winner)
|
||||
{
|
||||
var winMessage = new EndMessage(winner.Connection.Id, winner.Connection.Name);
|
||||
var winMessage = new EndMessage(winner.Connection);
|
||||
_room.BroadCast(new RoomMessage<EndMessage>("END", winMessage));
|
||||
_room.RoomType = new Lobby(_room);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user