diff --git a/KoogleApp/Components/Pages/Game.razor b/KoogleApp/Components/Pages/Game.razor index 9742dc3..c267064 100644 --- a/KoogleApp/Components/Pages/Game.razor +++ b/KoogleApp/Components/Pages/Game.razor @@ -154,11 +154,7 @@ { await base.OnInitializedAsync(); - var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var user = authState.User; - - isAuthenticated = user.Identity?.IsAuthenticated ?? false; - _userName = isAuthenticated ? user.Identity?.Name : "Gast"; + // Beim Laden (auch nach F5) werden die gespeicherten Daten geladen diff --git a/KoogleApp/Hub/SharedModelHub.cs b/KoogleApp/Hub/SharedModelHub.cs index 92afbbe..8c8cbc7 100644 --- a/KoogleApp/Hub/SharedModelHub.cs +++ b/KoogleApp/Hub/SharedModelHub.cs @@ -1,23 +1,28 @@ using Fluxor; using KoogleApp.Model; using KoogleApp.Services; +using KoogleApp.Store.Game.ThrowPanel; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.Diagnostics; -using KoogleApp.Store.Game.ThrowPanel; 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) + public HubConnectionService(NavigationManager navigationManager, + AuthenticationStateProvider authenticationStateProvider) { _navigationManager = navigationManager; + _authenticationStateProvider = authenticationStateProvider; } public HubConnection Connection => _hubConnection @@ -31,6 +36,12 @@ namespace KoogleApp.Hub 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() @@ -85,17 +96,19 @@ namespace KoogleApp.Hub public class SharedModelHub : Microsoft.AspNetCore.SignalR.Hub { private readonly IGameStatusDataService _dataService; + private readonly AuthenticationStateProvider _authenticationStateProvider; //private HubConnection _hubConnection; - + //public async Task UpdateText(string newText) //{ // await Clients.Others.SendAsync("ReceiveTextUpdate", newText); //} - public SharedModelHub(IGameStatusDataService dataService, NavigationManager navigationManager) + public SharedModelHub(IGameStatusDataService dataService, NavigationManager navigationManager, + AuthenticationStateProvider authenticationStateProvider) { _dataService = dataService; - + _authenticationStateProvider = authenticationStateProvider; } @@ -186,6 +199,7 @@ namespace KoogleApp.Hub await base.OnConnectedAsync(); } + public async Task BroadcastThrowPanelState(ThrowPanelState state, IDispatcher dispatcher) { //if (_hubConnection?.State != HubConnectionState.Connected) diff --git a/KoogleApp/Store/Game/ThrowPanel/Actions.cs b/KoogleApp/Store/Game/ThrowPanel/Actions.cs index 730d649..eed7b7b 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Actions.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Actions.cs @@ -20,6 +20,10 @@ namespace KoogleApp.Store.Game.ThrowPanel public record StateLoadedAction(ThrowPanelState? State); + public record SaveThrowPanelStateAction(ThrowPanelState State); + + public record ThrowPanelStateChangedAction(ThrowPanelState State, bool SaveToDb); + public record BroadcastThrowPanelStateAction(ThrowPanelState State); public record ThrowAction(bool LeftSink, bool RightSink); diff --git a/KoogleApp/Store/Game/ThrowPanel/Effects.cs b/KoogleApp/Store/Game/ThrowPanel/Effects.cs index 60185c8..f9ac3fd 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Effects.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Effects.cs @@ -2,7 +2,9 @@ using KoogleApp.Hub; using KoogleApp.Model; using KoogleApp.Services; +using Microsoft.AspNetCore.Components.Authorization; using System.Text.Json; +using Microsoft.Win32.SafeHandles; namespace KoogleApp.Store.Game.ThrowPanel { @@ -13,16 +15,20 @@ namespace KoogleApp.Store.Game.ThrowPanel private readonly IState _state; private readonly SessionStorage _sessionStorage; private readonly string _dataFilePath = "ThrowPanelState.json"; + private readonly AuthenticationStateProvider _authenticationStateProvider; public ThrowPanelEffects( ILogger logger, - HubConnectionService sharedModelHub, IState state, + HubConnectionService sharedModelHub, + IState state, + AuthenticationStateProvider authenticationStateProvider, SessionStorage sessionStorage) { _logger = logger; _sharedModelHub = sharedModelHub; _state = state; _sessionStorage = sessionStorage; + _authenticationStateProvider = authenticationStateProvider; } public void Dispose() @@ -32,41 +38,45 @@ namespace KoogleApp.Store.Game.ThrowPanel //[EffectMethod(typeof(StartStopAction))] [EffectMethod] - public async Task HandleStartAction(StartStopAction stopAction, IDispatcher dispatcher) + public Task HandleStartAction(StartStopAction stopAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value, false)); + return Task.CompletedTask; } [EffectMethod] - public async Task HandleToggleAllPinsAction(ToggleAllPinsAction stopAction, IDispatcher dispatcher) + public Task HandleToggleAllPinsAction(ToggleAllPinsAction stopAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value, false)); + return Task.CompletedTask; } [EffectMethod] - public async Task HandleTogglePinValueAction(TogglePinValueAction stopAction, IDispatcher dispatcher) + public Task HandleTogglePinValueAction(TogglePinValueAction stopAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value, false)); + return Task.CompletedTask; } [EffectMethod] - public async Task HandleUpdatePinStateByNumberAction(UpdatePinStateByNumberAction stopAction, IDispatcher dispatcher) + public Task HandleUpdatePinStateByNumberAction(UpdatePinStateByNumberAction stopAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value, false)); + return Task.CompletedTask; } [EffectMethod] - public async Task HandleToggleBellAction(ToggleBellAction stopAction, IDispatcher dispatcher) + public Task HandleToggleBellAction(ToggleBellAction stopAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value, false)); + return Task.CompletedTask; } [EffectMethod] - public async Task HandleThrowAction(ThrowAction stopAction, IDispatcher dispatcher) + public async Task HandleThrowAction(ThrowAction throwAction, IDispatcher dispatcher) { - dispatcher.Dispatch(new BroadcastThrowPanelStateAction(_state.Value)); + dispatcher.Dispatch(new ThrowPanelStateChangedAction(_state.Value,true)); } - [EffectMethod] public async Task HandleConnectToHubAction(ConnectToHubAction action, IDispatcher dispatcher) @@ -82,6 +92,23 @@ namespace KoogleApp.Store.Game.ThrowPanel } } + + + [EffectMethod] + public async Task HandelThrowPanelStateChangedAction(ThrowPanelStateChangedAction action, + IDispatcher dispatcher) + { + dispatcher.Dispatch(new SaveThrowPanelStateAction(action.State)); + dispatcher.Dispatch(new BroadcastThrowPanelStateAction(action.State)); + + if (action.SaveToDb) + { + var username = await GetUsername(); + Console.WriteLine(username); + ////_dataService.UpdateData(state, _userName); + } + } + [EffectMethod] public async Task HandelLoadStateAction(LoadStateFromSessionAction stateFromSessionAction, IDispatcher dispatcher) { @@ -107,11 +134,10 @@ namespace KoogleApp.Store.Game.ThrowPanel { dispatcher.Dispatch(new StateLoadedAction(state)); } - } [EffectMethod] - public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) + public async Task HandelSaveThrowPanelStateAction(SaveThrowPanelStateAction action, IDispatcher dispatcher) { await _sessionStorage.SetThrowPanelStateAsync(action.State); @@ -127,8 +153,26 @@ namespace KoogleApp.Store.Game.ThrowPanel { // TODO error handling } + } + [EffectMethod] + public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) + { await _sharedModelHub.HandelBroadcastThrowPanelStateAction(action, dispatcher); } + + + private async Task GetUsername() + { + var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); + var user = authState.User; + var isAuthenticated = user.Identity?.IsAuthenticated ?? false; + return isAuthenticated ? user.Identity?.Name : "Gast"; + } + + + + + } } diff --git a/KoogleApp/Store/Game/ThrowPanel/Reducers.cs b/KoogleApp/Store/Game/ThrowPanel/Reducers.cs index 86e041f..6a2627d 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Reducers.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Reducers.cs @@ -3,7 +3,6 @@ using KoogleApp.Model; namespace KoogleApp.Store.Game.ThrowPanel { - // Reducer public static class ThrowPanelStateReducer { [ReducerMethod] @@ -216,29 +215,29 @@ namespace KoogleApp.Store.Game.ThrowPanel } - return state with - { - Pin1Value = _pins[8].Value, - Pin1Disabled = _pins[8].Disabled, - Pin2Value = _pins[7].Value, - Pin2Disabled = _pins[7].Disabled, - Pin3Value = _pins[6].Value, - Pin3Disabled = _pins[6].Disabled, - Pin4Value = _pins[5].Value, - Pin4Disabled = _pins[5].Disabled, - Pin5Value = _pins[4].Value, - Pin5Disabled = _pins[4].Disabled, - Pin6Value = _pins[3].Value, - Pin6Disabled = _pins[3].Disabled, - Pin7Value = _pins[2].Value, - Pin7Disabled = _pins[2].Disabled, - Pin8Value = _pins[1].Value, - Pin8Disabled = _pins[1].Disabled, - Pin9Value = _pins[0].Value, - Pin9Disabled = _pins[0].Disabled, - ThrowCounter = nextCounter, - BellValue = false - }; + return state with + { + Pin1Value = _pins[8].Value, + Pin1Disabled = _pins[8].Disabled, + Pin2Value = _pins[7].Value, + Pin2Disabled = _pins[7].Disabled, + Pin3Value = _pins[6].Value, + Pin3Disabled = _pins[6].Disabled, + Pin4Value = _pins[5].Value, + Pin4Disabled = _pins[5].Disabled, + Pin5Value = _pins[4].Value, + Pin5Disabled = _pins[4].Disabled, + Pin6Value = _pins[3].Value, + Pin6Disabled = _pins[3].Disabled, + Pin7Value = _pins[2].Value, + Pin7Disabled = _pins[2].Disabled, + Pin8Value = _pins[1].Value, + Pin8Disabled = _pins[1].Disabled, + Pin9Value = _pins[0].Value, + Pin9Disabled = _pins[0].Disabled, + ThrowCounter = nextCounter, + BellValue = false + }; } } } diff --git a/KoogleApp/ThrowPanelState.json b/KoogleApp/ThrowPanelState.json index 39b21fb..636092f 100644 --- a/KoogleApp/ThrowPanelState.json +++ b/KoogleApp/ThrowPanelState.json @@ -1,24 +1,24 @@ { "IsStated": true, "BellValue": false, - "Pin1Value": false, + "Pin1Value": true, "Pin2Value": true, "Pin3Value": false, "Pin4Value": true, "Pin5Value": false, - "Pin6Value": true, + "Pin6Value": false, "Pin7Value": false, - "Pin8Value": true, - "Pin9Value": true, - "Pin1Disabled": false, + "Pin8Value": false, + "Pin9Value": false, + "Pin1Disabled": true, "Pin2Disabled": true, "Pin3Disabled": false, "Pin4Disabled": true, "Pin5Disabled": false, - "Pin6Disabled": true, + "Pin6Disabled": false, "Pin7Disabled": false, - "Pin8Disabled": true, - "Pin9Disabled": true, + "Pin8Disabled": false, + "Pin9Disabled": false, "ThrowsPerRound": 3, "ThrowCounter": 3, "ThrowMode": 1