From 0918b2b3cc52e31b8e4ce85f8290ddc8bc2504a1 Mon Sep 17 00:00:00 2001 From: DTieman Date: Wed, 12 Jun 2024 15:57:09 +0200 Subject: [PATCH 1/6] Fixed some bugs and added new features that came up while testing: - Check for allowed card type was before the check for the mau cards, which caused incorrect cards to be played and mau cards to be passed to wrong players - Ending with a special card is now a faulty move, which grants the player 5 fault cards now - Grabbed cards could be registered as playable as it didnt include next allowed card type - If the first card is a joker, set the next allowed card type to a random other card type to prevent an instant softlock - Other cleanup :) --- Mau/Card.cs | 25 +++++++++--- Mau/Deck.cs | 68 ++++++++++++++++---------------- Mau/Game.cs | 111 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 126 insertions(+), 78 deletions(-) diff --git a/Mau/Card.cs b/Mau/Card.cs index 204a1ae..45e384b 100644 --- a/Mau/Card.cs +++ b/Mau/Card.cs @@ -31,22 +31,37 @@ public static class CardExtensions { return card1.IsSameCardType(card2) && card1.IsSameCardValue(card2); } - + public static bool IsSameCardType(this Card card1, Card card2) { return card1.CardType == card2.CardType; } - + public static bool IsSameCardValue(this Card card1, Card card2) { return card1.CardValue == card2.CardValue; } - + public static bool CanBePlayedOn(this Card playedCard, Card currentCard) { return playedCard.IsSameCardType(currentCard) - || playedCard.IsSameCardValue(currentCard) - || playedCard.CardType == CardType.JOKER; + || playedCard.IsSameCardValue(currentCard) + || playedCard.CardType == CardType.JOKER; + } + + public static bool IsSpecialCard(this Card card) + { + return card.IsMauCard() || card.CardValue is + CardValue.SEVEN or + CardValue.EIGHT or + CardValue.JACK or + CardValue.KING or + CardValue.ACE; + } + + public static bool IsMauCard(this Card card) + { + return card.CardType == CardType.JOKER || card.CardValue is CardValue.TWO; } } diff --git a/Mau/Deck.cs b/Mau/Deck.cs index e5129e8..a45b1ec 100644 --- a/Mau/Deck.cs +++ b/Mau/Deck.cs @@ -2,9 +2,9 @@ public class Deck { - private List _unusedDeck = new(); - private List _usedDeck = new(); - public Card CurrentCard; + private readonly List _unusedDeck = new(); + private readonly List _usedDeck = new(); + public Card CurrentCard { get; private set; } /** * @@ -15,23 +15,20 @@ public class Deck { CreateSet(); ShuffleDeck(); - CurrentCard = DrawCard(); - _usedDeck.Add(CurrentCard); - - if (CurrentCard.CardType != CardType.JOKER) return; - CurrentCard = DrawCard(); - _usedDeck.Add(CurrentCard); + var initialCard = DrawCard(); + CurrentCard = initialCard; } /** * - * Adds the given card to the used cards deck. + * Adds the given card to the used cards deck and sets the current card to the given card. * * The card to add to the used cards deck. */ public void AddCardToUsedDeck(Card card) { _usedDeck.Add(card); + CurrentCard = card; } /** @@ -44,29 +41,6 @@ public class Deck { _usedDeck.AddRange(cards); } - - /** - * - * Creates a new deck of cards and adds them to the unused deck. - * - */ - private void CreateSet() - { - foreach (CardType cardType in Enum.GetValues(typeof(CardType))) - { - if (cardType == CardType.JOKER) - { - _unusedDeck.Add(new Card(cardType, CardValue.RED)); - _unusedDeck.Add(new Card(cardType, CardValue.BLACK)); - continue; - } - foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue))) - { - if (cardValue is CardValue.RED or CardValue.BLACK) continue; - _unusedDeck.Add(new Card(cardType, cardValue)); - } - } - } /** * @@ -97,6 +71,29 @@ public class Deck } return cards; } + + /** + * + * Creates a new deck of cards and adds them to the unused deck. + * + */ + private void CreateSet() + { + foreach (CardType cardType in Enum.GetValues(typeof(CardType))) + { + if (cardType == CardType.JOKER) + { + _unusedDeck.Add(new Card(cardType, CardValue.RED)); + _unusedDeck.Add(new Card(cardType, CardValue.BLACK)); + continue; + } + foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue))) + { + if (cardValue is CardValue.RED or CardValue.BLACK) continue; + _unusedDeck.Add(new Card(cardType, cardValue)); + } + } + } /** * @@ -124,6 +121,9 @@ public class Deck */ private void ShuffleDeck() { - _unusedDeck = _unusedDeck.OrderBy(x => Guid.NewGuid()).ToList(); + var unusedDeckCopy = new List(_unusedDeck); + _unusedDeck.Clear(); + unusedDeckCopy = unusedDeckCopy.OrderBy(x => Guid.NewGuid()).ToList(); + _unusedDeck.AddRange(unusedDeckCopy); } } \ No newline at end of file diff --git a/Mau/Game.cs b/Mau/Game.cs index 8bd9804..01e36b0 100644 --- a/Mau/Game.cs +++ b/Mau/Game.cs @@ -9,19 +9,38 @@ namespace MauMau_Server.Mau; public class Game : RoomType { + // Helpers private readonly Deck _deck = new(); private readonly TurnManager _turnManager = new(); - public readonly List MauCardBuffer = new(); - public CardType? NextAllowedCardType { get; set; } + + // Game state + private readonly List _mauCardBuffer = new(); + private CardType? NextAllowedCardType { get; set; } + + // Variables + private const int NumberOfFaultcards = 5; + private const int NumberOfStartCards = 8; public Game(Room.Room room, IEnumerable connections) : base(room) { + // If the current card is a joker, set the next allowed card type to a random card type + if (_deck.CurrentCard.CardType == CardType.JOKER) + { + // In the case the random card type is a joker, try again until it is not + do + { + var cardTypes = Enum.GetValues(typeof(CardType)); + var randomIndex = new Random().Next(cardTypes.Length); + NextAllowedCardType = (CardType?)cardTypes.GetValue(randomIndex) ?? CardType.SPADES; + } while (NextAllowedCardType == CardType.JOKER); + } + // Convert all the connections to players List players = new(); foreach (var player in connections.Select(connection => new Player(connection))) { // Give the new player a hand of cards - var initialHand = _deck.DrawCards(8); + var initialHand = _deck.DrawCards(NumberOfStartCards); player.GiveCards(initialHand); players.Add(player); } @@ -155,7 +174,7 @@ public class Game : RoomType { // If there are cards in the MauCardBuffer, this means there are multiple cards that need to be drawn // Otherwise, just draw a single card - if (MauCardBuffer.Count > 0) + if (_mauCardBuffer.Count > 0) { // Count the amount of cards that need to be drawn var totalCards = CountMauCardBuffer(); @@ -178,7 +197,7 @@ public class Game : RoomType player.GiveCard(drawnCard); // Change the player if the drawn card cannot be played - if (!drawnCard.CanBePlayedOn(_deck.CurrentCard)) + if (!CardCanBePlayed(drawnCard)) { _turnManager.ChangeTurnTo(); } @@ -217,43 +236,26 @@ public class Game : RoomType { return; } - - // Check if there is a specific card type that is allowed to be played - if (NextAllowedCardType != null) - { - // If the card is not the allowed card type, not the same value or a joker, ignore the message - if (playerCard.CardType != NextAllowedCardType && playerCard.CardType != CardType.JOKER && playerCard.CardValue != _deck.CurrentCard.CardValue) - { - return; - } - // Reset the allowed card type, so the next player has the normal same type and same value rules - NextAllowedCardType = null; - } else if (MauCardBuffer.Count > 0) - { - // If there are queued mau cards, the player can only another mau card (or draw) - if (playerCard.CardType != CardType.JOKER && playerCard.CardValue != CardValue.TWO) - { - return; - } - } - else - { - // Check if the card can be played on the current card, if not, ignore the message - if (!playerCard.CanBePlayedOn(_deck.CurrentCard)) - { - return; - } - } + + if (!CardCanBePlayed(playerCard)) return; // Remove the card from the player's hand player.Hand.Remove(playerCard); + // If the player's last played card is a special card, give the player 5 fault cards + if (player.Hand.Count < 1 && playerCard.IsSpecialCard()) + { + var faultCards = _deck.DrawCards(NumberOfFaultcards); + player.GiveCards(faultCards); + } + // 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; - // Set the new current card - _deck.CurrentCard = playerCard; - + // If the player has no cards left, end the game with player as winner if (player.Hand.Count == 0) { EndGame(player); @@ -280,13 +282,13 @@ public class Game : RoomType case CardValue.RED: case CardValue.BLACK: { - MauCardBuffer.Add(card); + _mauCardBuffer.Add(card); _turnManager.CurrentPlayer.State = PlayerState.CHOOSE; break; } case CardValue.TWO: { - MauCardBuffer.Add(card); + _mauCardBuffer.Add(card); _turnManager.ChangeTurnTo(); break; } @@ -319,6 +321,37 @@ public class Game : RoomType } } + /** + * + * Check if the card can be played with the current game state. + * + * The player can only play a mau card if there are pending mau cards (or draw) + * If there is a next allowed card type, the player can only play that card type, a joker or a card with the same value as the current card + * Otherwise, the player can play a card that has the same type, same value or is a joker + * + * + * True if the given card could be played + */ + private bool CardCanBePlayed(Card card) + { + // Check if there are pending mau cards played + if (_mauCardBuffer.Count > 0) + { + // If so, the card must be a mau card + return card.IsMauCard(); + } + + // Check if there is a next allowed card type + 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; + } + + // Otherwise, use the normal rules + return card.CanBePlayedOn(_deck.CurrentCard); + } + /** * * Count the amount of cards that need to be drawn from the MauCardBuffer. This method also clears the buffer. @@ -328,7 +361,7 @@ public class Game : RoomType private int CountMauCardBuffer() { var totalCards = 0; - foreach (var card in MauCardBuffer) + foreach (var card in _mauCardBuffer) { if (card.CardType == CardType.JOKER) { @@ -342,7 +375,7 @@ public class Game : RoomType continue; } } - MauCardBuffer.Clear(); + _mauCardBuffer.Clear(); return totalCards; } -- 2.49.1 From 25b7a8fb1e823a1bee6000f606cd38b3816817a6 Mon Sep 17 00:00:00 2001 From: DTieman Date: Wed, 12 Jun 2024 16:07:31 +0200 Subject: [PATCH 2/6] Solved sonarlint issue and some more code documentation --- Mau/Deck.cs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Mau/Deck.cs b/Mau/Deck.cs index a45b1ec..3faaec8 100644 --- a/Mau/Deck.cs +++ b/Mau/Deck.cs @@ -13,9 +13,16 @@ public class Deck */ public Deck() { + // Create a new set of cards CreateSet(); + + // Shuffle the deck ShuffleDeck(); + + // Draw the first card var initialCard = DrawCard(); + + // Set the current card to the first card CurrentCard = initialCard; } @@ -27,7 +34,10 @@ public class Deck */ public void AddCardToUsedDeck(Card card) { + // Add the card to the used deck _usedDeck.Add(card); + + // Set the current card to the given card CurrentCard = card; } @@ -50,9 +60,13 @@ public class Deck */ public Card DrawCard() { + // Check if the deck is empty, if so, reshuffle it if (_unusedDeck.Count == 0) ReshuffleDeck(); + + // Grab the first card from the deck and remove it var card = _unusedDeck[0]; _unusedDeck.RemoveAt(0); + return card; } @@ -64,6 +78,7 @@ public class Deck */ public IEnumerable DrawCards(int amount) { + // Create a list of cards and add the drawn cards to it var cards = new List(); for (var i = 0; i < amount; i++) { @@ -103,14 +118,17 @@ public class Deck */ private void ReshuffleDeck() { + // Move all used cards back to the unused deck _unusedDeck.AddRange(_usedDeck); _usedDeck.Clear(); - if (_unusedDeck.Count == 0) + // If there are no cards left, create a new set and add it + if (!_unusedDeck.Any()) { CreateSet(); } + // Shuffle the deck ShuffleDeck(); } @@ -121,9 +139,14 @@ public class Deck */ private void ShuffleDeck() { + // Clear the unused ceck var unusedDeckCopy = new List(_unusedDeck); _unusedDeck.Clear(); + + // Shuffle the deck unusedDeckCopy = unusedDeckCopy.OrderBy(x => Guid.NewGuid()).ToList(); + + // Add the shuffled deck back to the unused deck _unusedDeck.AddRange(unusedDeckCopy); } } \ No newline at end of file -- 2.49.1 From 681cfa13bd424b5e380361e413ddad00509038a2 Mon Sep 17 00:00:00 2001 From: DTieman Date: Sat, 13 Jul 2024 00:01:43 +0200 Subject: [PATCH 3/6] Dotnet version upgrade --- .dockerignore | 30 ++++++++++++-- .gitignore | 15 ++++--- Dockerfile | 40 +++++++------------ Mau/Game.cs | 67 +++++++++++++++++--------------- MauMau-Server.csproj | 24 ++++++------ MauMau-Server.sln | 16 ++++++++ Program.cs | 26 +++---------- Properties/launchSettings.json | 41 +++++++++++++++++++ README.md | 1 - Room/Messages/ChatMessage.cs | 1 + Room/Messages/EndMessage.cs | 12 +++--- Room/Room.cs | 3 +- Room/RoomMessage.cs | 4 +- Websockets/ConnectionInstance.cs | 20 ++++------ docker-compose.yml | 15 ------- 15 files changed, 173 insertions(+), 142 deletions(-) create mode 100644 MauMau-Server.sln create mode 100644 Properties/launchSettings.json delete mode 100644 README.md delete mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index 7b2c3e5..29cdcd6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,26 @@ -.idea/ -.git/ -.gitignore -README.md +**/.dockerignore +**/.env +**/.git +**/.gitea +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/.gitignore b/.gitignore index d7d1dc8..cd8c0c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ -.idea -bin -obj -Properties -appsettings.Local.json -.git -*.DotSettings.user -*.sln \ No newline at end of file +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ + +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 24f9269..7aeef06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,23 @@ -FROM mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER $APP_UID WORKDIR /app -EXPOSE 5000 - -FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build -ARG TARGETARCH -ARG BUILDPLATFORM +EXPOSE 8080 +EXPOSE 8081 +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release WORKDIR /src -COPY *.csproj . -RUN dotnet restore "MauMau-Server.csproj" +COPY ["MauMau-Server/MauMau-Server.csproj", "MauMau-Server/"] +RUN dotnet restore "MauMau-Server/MauMau-Server.csproj" COPY . . -WORKDIR "/src" -RUN dotnet build "MauMau-Server.csproj" -c Release -o /app/build -a $TARGETARCH +WORKDIR "/src/MauMau-Server" +RUN dotnet build "MauMau-Server.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish -RUN dotnet publish "MauMau-Server.csproj" -c Release -o /app/publish \ - --self-contained true \ - /p:PublishTrimmed=true \ - /p:PublishSingleFile=true \ - -a $TARGETARCH +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "MauMau-Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false -FROM --platform=$BUILDPLATFORM base AS final -ARG TARGETARCH -ARG BUILDPLATFORM - -RUN adduser --disabled-password \ - --home /app \ - --gecos '' dotnetuser && chown -R dotnetuser /app - -USER dotnetuser +FROM base AS final WORKDIR /app - COPY --from=publish /app/publish . -ENTRYPOINT ["./MauMau-Server"] \ No newline at end of file +ENTRYPOINT ["dotnet", "MauMau-Server.dll"] diff --git a/Mau/Game.cs b/Mau/Game.cs index 01e36b0..5cc4d1d 100644 --- a/Mau/Game.cs +++ b/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 _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 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("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("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. * * The player that drew a card - * A string that can be serialized to a drawcard instance */ 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(data).ToCard(); + var cardData = JsonSerializer.Deserialize(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("GAME", gameState))); + var gameState = new GameState(player, _deck.CurrentCard, NextAllowedCardType, _turnManager.CurrentPlayer, + _turnManager.Players); + player.Connection.SendMessageAsync( + JsonSerializer.Serialize(new RoomMessage("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("END", winMessage)); _room.RoomType = new Lobby(_room); } diff --git a/MauMau-Server.csproj b/MauMau-Server.csproj index d1cec05..dfb91e7 100644 --- a/MauMau-Server.csproj +++ b/MauMau-Server.csproj @@ -1,16 +1,16 @@ - - net7.0 - enable - enable - + + net8.0 + enable + enable + MauMau_Server2 + Linux + - - - - - - - + + + + + diff --git a/MauMau-Server.sln b/MauMau-Server.sln new file mode 100644 index 0000000..cf46a2c --- /dev/null +++ b/MauMau-Server.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauMau-Server", "MauMau-Server.csproj", "{44311559-F848-4D9B-9DB6-372042C8E6DA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {44311559-F848-4D9B-9DB6-372042C8E6DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44311559-F848-4D9B-9DB6-372042C8E6DA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44311559-F848-4D9B-9DB6-372042C8E6DA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44311559-F848-4D9B-9DB6-372042C8E6DA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs index 98f6017..32c96f7 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,13 @@ -using Hangfire; -using Hangfire.MemoryStorage; using MauMau_Server.Room; -using MauMau_Server.Websockets; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -var services = builder.Services; -services.AddControllers(); +builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -services.AddEndpointsApiExplorer(); -services.AddSwaggerGen(); -services.AddScoped(); -// var roomManager = services.BuildServiceProvider().GetRequiredService(); -// -// services.AddHangfire((sp, config) => -// { -// config.UseRecommendedSerializerSettings(); -// config.UseMemoryStorage(); -// }); -// services.AddHangfireServer(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddScoped(); var app = builder.Build(); @@ -37,9 +25,6 @@ var webSocketOptions = new WebSocketOptions() app.UseWebSockets(webSocketOptions); -// var recurringJobManager = app.Services.GetRequiredService(); -// recurringJobManager.AddOrUpdate("1", () => roomManager.ClearGhostRooms(), Cron.Hourly); - app.UseCors(policyBuilder => { policyBuilder.AllowAnyOrigin(); @@ -48,6 +33,5 @@ app.UseCors(policyBuilder => }); app.UseHttpsRedirection(); -app.UseAuthorization(); app.MapControllers(); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..cbb442c --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:65148", + "sslPort": 44331 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5039", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7037;http://localhost:5039", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/README.md b/README.md deleted file mode 100644 index 5dfe9d7..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -Mau \ No newline at end of file diff --git a/Room/Messages/ChatMessage.cs b/Room/Messages/ChatMessage.cs index 56b85dc..98142a2 100644 --- a/Room/Messages/ChatMessage.cs +++ b/Room/Messages/ChatMessage.cs @@ -2,6 +2,7 @@ public class ChatMessage { + public Guid Id { get; set; } = Guid.NewGuid(); public string Sender { get; set; } public string Message { get; set; } diff --git a/Room/Messages/EndMessage.cs b/Room/Messages/EndMessage.cs index 47e88c4..6c73994 100644 --- a/Room/Messages/EndMessage.cs +++ b/Room/Messages/EndMessage.cs @@ -1,13 +1,13 @@ -namespace MauMau_Server.Room.Messages; +using MauMau_Server.Websockets; + +namespace MauMau_Server.Room.Messages; public class EndMessage { - public Guid WinnerId { get; set; } - public string WinnerName { get; set; } + public ConnectionInstance Winner { get; set; } - public EndMessage(Guid winnerId, string winnerName) + public EndMessage(ConnectionInstance winner) { - WinnerId = winnerId; - WinnerName = winnerName; + Winner = winner; } } \ No newline at end of file diff --git a/Room/Room.cs b/Room/Room.cs index 2b5f338..5acebe9 100644 --- a/Room/Room.cs +++ b/Room/Room.cs @@ -3,7 +3,6 @@ using System.Text.Json; using System.Text.RegularExpressions; using MauMau_Server.Room.Messages; using MauMau_Server.Websockets; -using Microsoft.AspNet.SignalR.Messaging; namespace MauMau_Server.Room; @@ -11,7 +10,7 @@ public class Room { private readonly IRoomManager _roomManager; private readonly string _roomId; - public readonly List Connections = new(); + public readonly List Connections = []; public ConnectionInstance? Host; public RoomType RoomType; diff --git a/Room/RoomMessage.cs b/Room/RoomMessage.cs index f0c9309..db26d2b 100644 --- a/Room/RoomMessage.cs +++ b/Room/RoomMessage.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using System.Text.Json; namespace MauMau_Server.Room; @@ -10,7 +10,7 @@ public class RoomMessage public RoomMessage(string type, T data) { Type = type; - Data = JsonConvert.SerializeObject(data); + Data = JsonSerializer.Serialize(data); } public RoomMessage() diff --git a/Websockets/ConnectionInstance.cs b/Websockets/ConnectionInstance.cs index 9a5c58c..8a99a6e 100644 --- a/Websockets/ConnectionInstance.cs +++ b/Websockets/ConnectionInstance.cs @@ -1,23 +1,17 @@ using System.Net.WebSockets; using System.Text; -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace MauMau_Server.Websockets; -public class ConnectionInstance +public class ConnectionInstance(string name, Guid id, WebSocket socket) { - public Guid Id { get; set; } - public string Name { get; set; } + public Guid Id { get; set; } = id; + public string Name { get; set; } = name; + [JsonIgnore] - public WebSocket Socket { get; set; } - - public ConnectionInstance(string name, Guid id, WebSocket socket) - { - Name = name; - Id = id; - Socket = socket; - } - + public WebSocket Socket { get; set; } = socket; + /** * * Sends a message to the client. This method is asynchronous and formats the message to be ready to be sent. diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 279407b..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: '3.9' -services: - server: - build: - context: . - dockerfile: Dockerfile - container_name: 'MauMau-Server' - restart: always - ports: - - "5000:5000" - networks: - - MauMau -networks: - MauMau: - driver: bridge \ No newline at end of file -- 2.49.1 From b1083be1e77e708d07acbb091cc7943cf5dda9ab Mon Sep 17 00:00:00 2001 From: DTieman Date: Sat, 13 Jul 2024 00:04:40 +0200 Subject: [PATCH 4/6] Do not open browser on startup --- Properties/launchSettings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index cbb442c..bb92f46 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -12,7 +12,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "launchUrl": "swagger", "applicationUrl": "http://localhost:5039", "environmentVariables": { @@ -22,7 +22,7 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "launchUrl": "swagger", "applicationUrl": "https://localhost:7037;http://localhost:5039", "environmentVariables": { @@ -31,7 +31,7 @@ }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, + "launchBrowser": false, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" -- 2.49.1 From 401e9fbf8aa1b38fae075cd22b8bb294b63fd10d Mon Sep 17 00:00:00 2001 From: DTieman Date: Sat, 13 Jul 2024 00:09:43 +0200 Subject: [PATCH 5/6] Workflow dotnet version to 8.0.x --- .gitea/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/main.yaml b/.gitea/workflows/main.yaml index 1df1a47..96dc0cb 100644 --- a/.gitea/workflows/main.yaml +++ b/.gitea/workflows/main.yaml @@ -22,7 +22,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v3 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Install dependencies run: dotnet restore -- 2.49.1 From 094b5488a1c3437b2f379bb18b1b4ba286c9e118 Mon Sep 17 00:00:00 2001 From: DTieman Date: Sun, 14 Jul 2024 15:04:59 +0200 Subject: [PATCH 6/6] Removed subfolder from dockerfile --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7aeef06..91bb842 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,10 +7,10 @@ EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src -COPY ["MauMau-Server/MauMau-Server.csproj", "MauMau-Server/"] -RUN dotnet restore "MauMau-Server/MauMau-Server.csproj" +COPY MauMau-Server.csproj . +RUN dotnet restore "MauMau-Server.csproj" COPY . . -WORKDIR "/src/MauMau-Server" +WORKDIR "/src" RUN dotnet build "MauMau-Server.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish -- 2.49.1