diff --git a/KoogleApp/Hub/SharedModelHub.cs b/KoogleApp/Hub/SharedModelHub.cs index 8c8cbc7..5e625ef 100644 --- a/KoogleApp/Hub/SharedModelHub.cs +++ b/KoogleApp/Hub/SharedModelHub.cs @@ -11,88 +11,7 @@ using System.Diagnostics; namespace KoogleApp.Hub { - public class HubConnectionService : IAsyncDisposable - { - private readonly NavigationManager _navigationManager; - private readonly AuthenticationStateProvider _authenticationStateProvider; - private HubConnection? _hubConnection; - private string? _userName; - - public HubConnectionService(NavigationManager navigationManager, - AuthenticationStateProvider authenticationStateProvider) - { - _navigationManager = navigationManager; - _authenticationStateProvider = authenticationStateProvider; - } - - public HubConnection Connection => _hubConnection - ?? throw new InvalidOperationException("Connection not initialized"); - - public async ValueTask DisposeAsync() - { - if (_hubConnection != null) - await _hubConnection.DisposeAsync(); - } - - public async Task ConnectToHub(IDispatcher dispatcher) - { - var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); - var user = authState.User; - var isAuthenticated = user.Identity?.IsAuthenticated ?? false; - _userName = isAuthenticated ? user.Identity?.Name : "Gast"; - - - _hubConnection = new HubConnectionBuilder() - .WithUrl(_navigationManager.ToAbsoluteUri("/sharedmodelhub")) - .WithAutomaticReconnect() - .Build(); - - - - _hubConnection.On("ReceiveThrowPanelState", state => - { - dispatcher.Dispatch(new ReceiveStateFromServerAction(state)); - }); - - // _hubConnection.On("ReceiveTextUpdate", (newText) => - // { - // _sharedText = newText; - // InvokeAsync(StateHasChanged); - // }); - - await _hubConnection.StartAsync(); - } - - public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) - { - if (_hubConnection?.State != HubConnectionState.Connected) - { - //_logger.LogWarning("Cannot add activity: not connected"); - return; - } - - try - { - if (_hubConnection is not null) - { - await _hubConnection.SendAsync("BroadcastThrowPanelState", action.State); - } - - //await Clients.Others.SendAsync("ReceiveThrowPanelState", new ThrowPanelState()); - - //await _hubConnection.InvokeAsync("StartStopAction", stopAction); - - // Der Server wird ActivityAdded an alle Clients senden, - // einschließlich diesem. Dann wird ActivityAddedAction dispatched. - } - catch (Exception ex) - { - //_logger.LogError(ex, "Failed to add activity"); - // Optional: Error-Handling Action dispatchen - } - } - } - + public class SharedModelHub : Microsoft.AspNetCore.SignalR.Hub { private readonly IGameStatusDataService _dataService; @@ -111,8 +30,6 @@ namespace KoogleApp.Hub _authenticationStateProvider = authenticationStateProvider; } - - public async Task UpdateThrowPanelState(ThrowPanelState panelState, string username) { await Clients.All.SendAsync("UpdateThrowPanelState", panelState); diff --git a/KoogleApp/Model/GameStatus.cs b/KoogleApp/Model/GameStatus.cs index 71e3e9c..6d2bae7 100644 --- a/KoogleApp/Model/GameStatus.cs +++ b/KoogleApp/Model/GameStatus.cs @@ -11,7 +11,7 @@ public class GameStatus { - public string Content { get; set; } + public ThrowPanelState? ThrowPanelState { get; set; } } //public class DataSnapshot diff --git a/KoogleApp/Services/GameStatusDataService.cs b/KoogleApp/Services/GameStatusDataService.cs index 5e24a5a..dcf023b 100644 --- a/KoogleApp/Services/GameStatusDataService.cs +++ b/KoogleApp/Services/GameStatusDataService.cs @@ -32,7 +32,7 @@ namespace KoogleApp.Services { Status = new GameStatus { - Content = "Willkommen! Starte die Zusammenarbeit.", + }, Version = 1, LastModified = DateTime.Now, diff --git a/KoogleApp/Services/HubConnectionService.cs b/KoogleApp/Services/HubConnectionService.cs new file mode 100644 index 0000000..0f3dab6 --- /dev/null +++ b/KoogleApp/Services/HubConnectionService.cs @@ -0,0 +1,92 @@ +using Fluxor; +using KoogleApp.Model; +using KoogleApp.Store.Game.ThrowPanel; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.SignalR.Client; + +namespace KoogleApp.Services +{ + public class HubConnectionService : IAsyncDisposable + { + private readonly NavigationManager _navigationManager; + private readonly AuthenticationStateProvider _authenticationStateProvider; + private HubConnection? _hubConnection; + private string? _userName; + + public HubConnectionService(NavigationManager navigationManager, + AuthenticationStateProvider authenticationStateProvider) + { + _navigationManager = navigationManager; + _authenticationStateProvider = authenticationStateProvider; + } + + public HubConnection Connection => _hubConnection + ?? throw new InvalidOperationException("Connection not initialized"); + + public async ValueTask DisposeAsync() + { + if (_hubConnection != null) + await _hubConnection.DisposeAsync(); + } + + public async Task ConnectToHub(IDispatcher dispatcher) + { + var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); + var user = authState.User; + var isAuthenticated = user.Identity?.IsAuthenticated ?? false; + _userName = isAuthenticated ? user.Identity?.Name : "Gast"; + + + _hubConnection = new HubConnectionBuilder() + .WithUrl(_navigationManager.ToAbsoluteUri("/sharedmodelhub")) + .WithAutomaticReconnect() + .Build(); + + + + _hubConnection.On("ReceiveThrowPanelState", state => + { + dispatcher.Dispatch(new ReceiveStateFromServerAction(state)); + }); + + // _hubConnection.On("ReceiveTextUpdate", (newText) => + // { + // _sharedText = newText; + // InvokeAsync(StateHasChanged); + // }); + + await _hubConnection.StartAsync(); + } + + public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) + { + if (_hubConnection?.State != HubConnectionState.Connected) + { + //_logger.LogWarning("Cannot add activity: not connected"); + return; + } + + try + { + if (_hubConnection is not null) + { + await _hubConnection.SendAsync("BroadcastThrowPanelState", action.State); + } + + //await Clients.Others.SendAsync("ReceiveThrowPanelState", new ThrowPanelState()); + + //await _hubConnection.InvokeAsync("StartStopAction", stopAction); + + // Der Server wird ActivityAdded an alle Clients senden, + // einschließlich diesem. Dann wird ActivityAddedAction dispatched. + } + catch (Exception ex) + { + //_logger.LogError(ex, "Failed to add activity"); + // Optional: Error-Handling Action dispatchen + } + } + } + +} diff --git a/KoogleApp/Store/Game/ThrowPanel/Effects.cs b/KoogleApp/Store/Game/ThrowPanel/Effects.cs index f9ac3fd..4210664 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Effects.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Effects.cs @@ -11,29 +11,32 @@ namespace KoogleApp.Store.Game.ThrowPanel public class ThrowPanelEffects : IDisposable { private readonly ILogger _logger; - private readonly HubConnectionService _sharedModelHub; + private readonly HubConnectionService _sharedHubService; private readonly IState _state; private readonly SessionStorage _sessionStorage; private readonly string _dataFilePath = "ThrowPanelState.json"; private readonly AuthenticationStateProvider _authenticationStateProvider; + private readonly IGameStatusDataService _dataService; public ThrowPanelEffects( ILogger logger, - HubConnectionService sharedModelHub, + HubConnectionService sharedHubService, IState state, AuthenticationStateProvider authenticationStateProvider, + IGameStatusDataService dataService, SessionStorage sessionStorage) { _logger = logger; - _sharedModelHub = sharedModelHub; + _sharedHubService = sharedHubService; _state = state; _sessionStorage = sessionStorage; + _dataService = dataService; _authenticationStateProvider = authenticationStateProvider; } public void Dispose() { - //_sharedModelHub?.Dispose(); + //_sharedHubService?.Dispose(); } //[EffectMethod(typeof(StartStopAction))] @@ -83,7 +86,7 @@ namespace KoogleApp.Store.Game.ThrowPanel { try { - await _sharedModelHub.ConnectToHub(dispatcher); + await _sharedHubService.ConnectToHub(dispatcher); } catch (Exception ex) { @@ -105,7 +108,8 @@ namespace KoogleApp.Store.Game.ThrowPanel { var username = await GetUsername(); Console.WriteLine(username); - ////_dataService.UpdateData(state, _userName); + + _dataService.UpdateData(new GameStatus{ ThrowPanelState = action.State}, username); } } @@ -158,7 +162,7 @@ namespace KoogleApp.Store.Game.ThrowPanel [EffectMethod] public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) { - await _sharedModelHub.HandelBroadcastThrowPanelStateAction(action, dispatcher); + await _sharedHubService.HandelBroadcastThrowPanelStateAction(action, dispatcher); } diff --git a/KoogleApp/ThrowPanelState.json b/KoogleApp/ThrowPanelState.json index 636092f..1204d2e 100644 --- a/KoogleApp/ThrowPanelState.json +++ b/KoogleApp/ThrowPanelState.json @@ -1,25 +1,25 @@ { "IsStated": true, "BellValue": false, - "Pin1Value": true, - "Pin2Value": true, + "Pin1Value": false, + "Pin2Value": false, "Pin3Value": false, - "Pin4Value": true, + "Pin4Value": false, "Pin5Value": false, "Pin6Value": false, "Pin7Value": false, "Pin8Value": false, "Pin9Value": false, - "Pin1Disabled": true, - "Pin2Disabled": true, + "Pin1Disabled": false, + "Pin2Disabled": false, "Pin3Disabled": false, - "Pin4Disabled": true, + "Pin4Disabled": false, "Pin5Disabled": false, "Pin6Disabled": false, "Pin7Disabled": false, "Pin8Disabled": false, "Pin9Disabled": false, "ThrowsPerRound": 3, - "ThrowCounter": 3, + "ThrowCounter": 1, "ThrowMode": 1 } \ No newline at end of file diff --git a/KoogleApp/appdata.json b/KoogleApp/appdata.json index e2b9068..37d236b 100644 --- a/KoogleApp/appdata.json +++ b/KoogleApp/appdata.json @@ -1,16 +1,72 @@ { "CurrentData": { "Status": { - "Content": "Willkommen! Starte die Zusammenarbeit." + "ThrowPanelState": { + "IsStated": true, + "BellValue": false, + "Pin1Value": false, + "Pin2Value": false, + "Pin3Value": false, + "Pin4Value": false, + "Pin5Value": false, + "Pin6Value": false, + "Pin7Value": false, + "Pin8Value": false, + "Pin9Value": false, + "Pin1Disabled": false, + "Pin2Disabled": false, + "Pin3Disabled": false, + "Pin4Disabled": false, + "Pin5Disabled": false, + "Pin6Disabled": false, + "Pin7Disabled": false, + "Pin8Disabled": false, + "Pin9Disabled": false, + "ThrowsPerRound": 3, + "ThrowCounter": 1, + "ThrowMode": 1 + } }, - "Version": 3, - "LastModified": "2025-11-05T20:53:27.735651+01:00", + "Version": 4, + "LastModified": "2025-11-10T21:34:15.3376837+01:00", "LastModifiedBy": "test1@test.de" }, "UndoHistory": [ { "Status": { - "Content": "Willkommen! Starte die Zusammenarbeit." + "ThrowPanelState": { + "IsStated": true, + "BellValue": false, + "Pin1Value": false, + "Pin2Value": false, + "Pin3Value": false, + "Pin4Value": false, + "Pin5Value": false, + "Pin6Value": false, + "Pin7Value": false, + "Pin8Value": false, + "Pin9Value": false, + "Pin1Disabled": false, + "Pin2Disabled": false, + "Pin3Disabled": false, + "Pin4Disabled": false, + "Pin5Disabled": false, + "Pin6Disabled": false, + "Pin7Disabled": false, + "Pin8Disabled": false, + "Pin9Disabled": false, + "ThrowsPerRound": 3, + "ThrowCounter": 1, + "ThrowMode": 1 + } + }, + "Version": 3, + "LastModified": "2025-11-05T20:53:27.735651+01:00", + "LastModifiedBy": "test1@test.de" + }, + { + "Status": { + "ThrowPanelState": null }, "Version": 2, "LastModified": "2025-11-03T19:09:57.4404986+01:00", @@ -18,7 +74,7 @@ }, { "Status": { - "Content": "Willkommen! Starte die Zusammenarbeit." + "ThrowPanelState": null }, "Version": 1, "LastModified": "2025-11-03T19:08:40.9909294+01:00",