diff --git a/SlipItIn.AppHost/AppHost.cs b/SlipItIn.AppHost/AppHost.cs index 1e99437..cdbb743 100644 --- a/SlipItIn.AppHost/AppHost.cs +++ b/SlipItIn.AppHost/AppHost.cs @@ -1,7 +1,8 @@ var builder = DistributedApplication.CreateBuilder(args); var db = builder.AddPostgres("pgsql") - .AddDatabase("postgresdb"); + .AddDatabase("postgresdb") + ; var server = builder.AddProject("server") .WithReference(db) diff --git a/SlipItIn.Server/Services/GameService.cs b/SlipItIn.Server/Services/GameService.cs index eda875c..a7f55de 100644 --- a/SlipItIn.Server/Services/GameService.cs +++ b/SlipItIn.Server/Services/GameService.cs @@ -29,11 +29,15 @@ public class GameService : IGameService Status = GameStatus.Lobby }; - context.Games.Add(game); // Host wird automatisch als erster Spieler hinzugefügt - var hostPlayer = new Player { UserId = host.Id, GameId = game.Id, ConnectionId = connectionId }; - context.Players.Add(hostPlayer); + var hostPlayer = new Player { UserId = host.Id, ConnectionId = connectionId }; + + // Spieler direkt zur Liste des Spiels hinzufügen + game.Players.Add(hostPlayer); + + // Es reicht, nur das Game hinzuzufügen — EF Core fügt den Player automatisch mit hinzu + context.Games.Add(game); await context.SaveChangesAsync(); diff --git a/SlipItIn/App.xaml.cs b/SlipItIn/App.xaml.cs index 2c5d9dd..9574392 100644 --- a/SlipItIn/App.xaml.cs +++ b/SlipItIn/App.xaml.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using SlipItIn.Infrastructure; +using SlipItIn.Services.Interfaces; namespace SlipItIn; @@ -7,10 +8,34 @@ public partial class App : Application public App() { InitializeComponent(); + _ = InitializeAsync(); } protected override Window CreateWindow(IActivationState? activationState) { - return new Window(new AppShell()); + var window = new Window(new AppShell()); + window.Resumed += async (_, _) => + { + var gameStateService = ServiceHelper.GetRequiredService(); + await gameStateService.ResyncAsync(); + }; + return window; } -} \ No newline at end of file + + private static async Task InitializeAsync() + { + var authSession = ServiceHelper.GetRequiredService(); + var signalR = ServiceHelper.GetRequiredService(); + var gameStateService = ServiceHelper.GetRequiredService(); + + await authSession.InitializeAsync(); + await gameStateService.InitializeAsync(); + + if (!string.IsNullOrWhiteSpace(authSession.AccessToken)) + { + var connected = await signalR.ConnectAsync(authSession.AccessToken); + if (connected) + await gameStateService.ResyncAsync(); + } + } +} diff --git a/SlipItIn/AppShell.xaml b/SlipItIn/AppShell.xaml index 12da11a..2d8af88 100644 --- a/SlipItIn/AppShell.xaml +++ b/SlipItIn/AppShell.xaml @@ -3,12 +3,22 @@ x:Class="SlipItIn.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" - xmlns:local="clr-namespace:SlipItIn" - Title="Slip It In"> + xmlns:views="clr-namespace:SlipItIn.Views"> - + + + + + + + diff --git a/SlipItIn/AppShell.xaml.cs b/SlipItIn/AppShell.xaml.cs index a985cd7..5f8c199 100644 --- a/SlipItIn/AppShell.xaml.cs +++ b/SlipItIn/AppShell.xaml.cs @@ -1,4 +1,6 @@ -namespace SlipItIn; +using SlipItIn.Views; + +namespace SlipItIn; public partial class AppShell : Shell { diff --git a/SlipItIn/Infrastructure/ServiceHelper.cs b/SlipItIn/Infrastructure/ServiceHelper.cs new file mode 100644 index 0000000..942c99d --- /dev/null +++ b/SlipItIn/Infrastructure/ServiceHelper.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace SlipItIn.Infrastructure; + +public static class ServiceHelper +{ + public static IServiceProvider? Services { get; set; } + + public static T GetRequiredService() where T : notnull + { + if (Services is null) + throw new InvalidOperationException("Service provider is not initialized."); + + return Services.GetRequiredService(); + } +} diff --git a/SlipItIn/MainPage.xaml b/SlipItIn/MainPage.xaml deleted file mode 100644 index 9747740..0000000 --- a/SlipItIn/MainPage.xaml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - -