Dotnet version upgrade
Some checks failed
Build Mau & Deploy Mau / build (push) Failing after 1m2s
Build Mau & Deploy Mau / deploy (push) Has been skipped

This commit is contained in:
2024-07-13 00:01:43 +02:00
parent 25b7a8fb1e
commit 681cfa13bd
15 changed files with 173 additions and 142 deletions

View File

@@ -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

15
.gitignore vendored
View File

@@ -1,8 +1,7 @@
.idea
bin
obj
Properties
appsettings.Local.json
.git
*.DotSettings.user
*.sln
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
.idea

View File

@@ -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"]
ENTRYPOINT ["dotnet", "MauMau-Server.dll"]

View File

@@ -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);
}

View File

@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MauMau_Server2</RootNamespace>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.8.12" />
<PackageReference Include="Hangfire.MemoryStorage" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
</Project>

16
MauMau-Server.sln Normal file
View File

@@ -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

View File

@@ -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<IRoomManager, RoomManager>();
// var roomManager = services.BuildServiceProvider().GetRequiredService<IRoomManager>();
//
// services.AddHangfire((sp, config) =>
// {
// config.UseRecommendedSerializerSettings();
// config.UseMemoryStorage();
// });
// services.AddHangfireServer();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IRoomManager, RoomManager>();
var app = builder.Build();
@@ -37,9 +25,6 @@ var webSocketOptions = new WebSocketOptions()
app.UseWebSockets(webSocketOptions);
// var recurringJobManager = app.Services.GetRequiredService<IRecurringJobManagerV2>();
// 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();
app.Run();

View File

@@ -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"
}
}
}
}

View File

@@ -1 +0,0 @@
Mau

View File

@@ -2,6 +2,7 @@
public class ChatMessage
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Sender { get; set; }
public string Message { get; set; }

View File

@@ -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;
}
}

View File

@@ -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<ConnectionInstance> Connections = new();
public readonly List<ConnectionInstance> Connections = [];
public ConnectionInstance? Host;
public RoomType RoomType;

View File

@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using System.Text.Json;
namespace MauMau_Server.Room;
@@ -10,7 +10,7 @@ public class RoomMessage<T>
public RoomMessage(string type, T data)
{
Type = type;
Data = JsonConvert.SerializeObject(data);
Data = JsonSerializer.Serialize(data);
}
public RoomMessage()

View File

@@ -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;
/**
* <summary>
* Sends a message to the client. This method is asynchronous and formats the message to be ready to be sent.

View File

@@ -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