This commit is contained in:
beo3000 2025-11-10 21:15:40 +01:00
parent a0a4d5efba
commit 26f08d9dc1
6 changed files with 115 additions and 58 deletions

View File

@ -154,11 +154,7 @@
{ {
await base.OnInitializedAsync(); 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 // Beim Laden (auch nach F5) werden die gespeicherten Daten geladen

View File

@ -1,23 +1,28 @@
using Fluxor; using Fluxor;
using KoogleApp.Model; using KoogleApp.Model;
using KoogleApp.Services; using KoogleApp.Services;
using KoogleApp.Store.Game.ThrowPanel;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client; using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Diagnostics; using System.Diagnostics;
using KoogleApp.Store.Game.ThrowPanel;
namespace KoogleApp.Hub namespace KoogleApp.Hub
{ {
public class HubConnectionService : IAsyncDisposable public class HubConnectionService : IAsyncDisposable
{ {
private readonly NavigationManager _navigationManager; private readonly NavigationManager _navigationManager;
private readonly AuthenticationStateProvider _authenticationStateProvider;
private HubConnection? _hubConnection; private HubConnection? _hubConnection;
private string? _userName;
public HubConnectionService(NavigationManager navigationManager) public HubConnectionService(NavigationManager navigationManager,
AuthenticationStateProvider authenticationStateProvider)
{ {
_navigationManager = navigationManager; _navigationManager = navigationManager;
_authenticationStateProvider = authenticationStateProvider;
} }
public HubConnection Connection => _hubConnection public HubConnection Connection => _hubConnection
@ -31,6 +36,12 @@ namespace KoogleApp.Hub
public async Task ConnectToHub(IDispatcher dispatcher) 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() _hubConnection = new HubConnectionBuilder()
.WithUrl(_navigationManager.ToAbsoluteUri("/sharedmodelhub")) .WithUrl(_navigationManager.ToAbsoluteUri("/sharedmodelhub"))
.WithAutomaticReconnect() .WithAutomaticReconnect()
@ -85,6 +96,7 @@ namespace KoogleApp.Hub
public class SharedModelHub : Microsoft.AspNetCore.SignalR.Hub public class SharedModelHub : Microsoft.AspNetCore.SignalR.Hub
{ {
private readonly IGameStatusDataService _dataService; private readonly IGameStatusDataService _dataService;
private readonly AuthenticationStateProvider _authenticationStateProvider;
//private HubConnection _hubConnection; //private HubConnection _hubConnection;
//public async Task UpdateText(string newText) //public async Task UpdateText(string newText)
@ -92,10 +104,11 @@ namespace KoogleApp.Hub
// await Clients.Others.SendAsync("ReceiveTextUpdate", newText); // await Clients.Others.SendAsync("ReceiveTextUpdate", newText);
//} //}
public SharedModelHub(IGameStatusDataService dataService, NavigationManager navigationManager) public SharedModelHub(IGameStatusDataService dataService, NavigationManager navigationManager,
AuthenticationStateProvider authenticationStateProvider)
{ {
_dataService = dataService; _dataService = dataService;
_authenticationStateProvider = authenticationStateProvider;
} }
@ -186,6 +199,7 @@ namespace KoogleApp.Hub
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
public async Task BroadcastThrowPanelState(ThrowPanelState state, IDispatcher dispatcher) public async Task BroadcastThrowPanelState(ThrowPanelState state, IDispatcher dispatcher)
{ {
//if (_hubConnection?.State != HubConnectionState.Connected) //if (_hubConnection?.State != HubConnectionState.Connected)

View File

@ -20,6 +20,10 @@ namespace KoogleApp.Store.Game.ThrowPanel
public record StateLoadedAction(ThrowPanelState? State); public record StateLoadedAction(ThrowPanelState? State);
public record SaveThrowPanelStateAction(ThrowPanelState State);
public record ThrowPanelStateChangedAction(ThrowPanelState State, bool SaveToDb);
public record BroadcastThrowPanelStateAction(ThrowPanelState State); public record BroadcastThrowPanelStateAction(ThrowPanelState State);
public record ThrowAction(bool LeftSink, bool RightSink); public record ThrowAction(bool LeftSink, bool RightSink);

View File

@ -2,7 +2,9 @@
using KoogleApp.Hub; using KoogleApp.Hub;
using KoogleApp.Model; using KoogleApp.Model;
using KoogleApp.Services; using KoogleApp.Services;
using Microsoft.AspNetCore.Components.Authorization;
using System.Text.Json; using System.Text.Json;
using Microsoft.Win32.SafeHandles;
namespace KoogleApp.Store.Game.ThrowPanel namespace KoogleApp.Store.Game.ThrowPanel
{ {
@ -13,16 +15,20 @@ namespace KoogleApp.Store.Game.ThrowPanel
private readonly IState<ThrowPanelState> _state; private readonly IState<ThrowPanelState> _state;
private readonly SessionStorage _sessionStorage; private readonly SessionStorage _sessionStorage;
private readonly string _dataFilePath = "ThrowPanelState.json"; private readonly string _dataFilePath = "ThrowPanelState.json";
private readonly AuthenticationStateProvider _authenticationStateProvider;
public ThrowPanelEffects( public ThrowPanelEffects(
ILogger<ThrowPanelEffects> logger, ILogger<ThrowPanelEffects> logger,
HubConnectionService sharedModelHub, IState<ThrowPanelState> state, HubConnectionService sharedModelHub,
IState<ThrowPanelState> state,
AuthenticationStateProvider authenticationStateProvider,
SessionStorage sessionStorage) SessionStorage sessionStorage)
{ {
_logger = logger; _logger = logger;
_sharedModelHub = sharedModelHub; _sharedModelHub = sharedModelHub;
_state = state; _state = state;
_sessionStorage = sessionStorage; _sessionStorage = sessionStorage;
_authenticationStateProvider = authenticationStateProvider;
} }
public void Dispose() public void Dispose()
@ -32,42 +38,46 @@ namespace KoogleApp.Store.Game.ThrowPanel
//[EffectMethod(typeof(StartStopAction))] //[EffectMethod(typeof(StartStopAction))]
[EffectMethod] [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] [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] [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] [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] [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] [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] [EffectMethod]
public async Task HandleConnectToHubAction(ConnectToHubAction action, IDispatcher dispatcher) 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] [EffectMethod]
public async Task HandelLoadStateAction(LoadStateFromSessionAction stateFromSessionAction, IDispatcher dispatcher) public async Task HandelLoadStateAction(LoadStateFromSessionAction stateFromSessionAction, IDispatcher dispatcher)
{ {
@ -107,11 +134,10 @@ namespace KoogleApp.Store.Game.ThrowPanel
{ {
dispatcher.Dispatch(new StateLoadedAction(state)); dispatcher.Dispatch(new StateLoadedAction(state));
} }
} }
[EffectMethod] [EffectMethod]
public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher) public async Task HandelSaveThrowPanelStateAction(SaveThrowPanelStateAction action, IDispatcher dispatcher)
{ {
await _sessionStorage.SetThrowPanelStateAsync(action.State); await _sessionStorage.SetThrowPanelStateAsync(action.State);
@ -127,8 +153,26 @@ namespace KoogleApp.Store.Game.ThrowPanel
{ {
// TODO error handling // TODO error handling
} }
}
[EffectMethod]
public async Task HandelBroadcastThrowPanelStateAction(BroadcastThrowPanelStateAction action, IDispatcher dispatcher)
{
await _sharedModelHub.HandelBroadcastThrowPanelStateAction(action, dispatcher); await _sharedModelHub.HandelBroadcastThrowPanelStateAction(action, dispatcher);
} }
private async Task<string> GetUsername()
{
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
var isAuthenticated = user.Identity?.IsAuthenticated ?? false;
return isAuthenticated ? user.Identity?.Name : "Gast";
}
} }
} }

View File

@ -3,7 +3,6 @@ using KoogleApp.Model;
namespace KoogleApp.Store.Game.ThrowPanel namespace KoogleApp.Store.Game.ThrowPanel
{ {
// Reducer
public static class ThrowPanelStateReducer public static class ThrowPanelStateReducer
{ {
[ReducerMethod] [ReducerMethod]
@ -216,29 +215,29 @@ namespace KoogleApp.Store.Game.ThrowPanel
} }
return state with return state with
{ {
Pin1Value = _pins[8].Value, Pin1Value = _pins[8].Value,
Pin1Disabled = _pins[8].Disabled, Pin1Disabled = _pins[8].Disabled,
Pin2Value = _pins[7].Value, Pin2Value = _pins[7].Value,
Pin2Disabled = _pins[7].Disabled, Pin2Disabled = _pins[7].Disabled,
Pin3Value = _pins[6].Value, Pin3Value = _pins[6].Value,
Pin3Disabled = _pins[6].Disabled, Pin3Disabled = _pins[6].Disabled,
Pin4Value = _pins[5].Value, Pin4Value = _pins[5].Value,
Pin4Disabled = _pins[5].Disabled, Pin4Disabled = _pins[5].Disabled,
Pin5Value = _pins[4].Value, Pin5Value = _pins[4].Value,
Pin5Disabled = _pins[4].Disabled, Pin5Disabled = _pins[4].Disabled,
Pin6Value = _pins[3].Value, Pin6Value = _pins[3].Value,
Pin6Disabled = _pins[3].Disabled, Pin6Disabled = _pins[3].Disabled,
Pin7Value = _pins[2].Value, Pin7Value = _pins[2].Value,
Pin7Disabled = _pins[2].Disabled, Pin7Disabled = _pins[2].Disabled,
Pin8Value = _pins[1].Value, Pin8Value = _pins[1].Value,
Pin8Disabled = _pins[1].Disabled, Pin8Disabled = _pins[1].Disabled,
Pin9Value = _pins[0].Value, Pin9Value = _pins[0].Value,
Pin9Disabled = _pins[0].Disabled, Pin9Disabled = _pins[0].Disabled,
ThrowCounter = nextCounter, ThrowCounter = nextCounter,
BellValue = false BellValue = false
}; };
} }
} }
} }

View File

@ -1,24 +1,24 @@
{ {
"IsStated": true, "IsStated": true,
"BellValue": false, "BellValue": false,
"Pin1Value": false, "Pin1Value": true,
"Pin2Value": true, "Pin2Value": true,
"Pin3Value": false, "Pin3Value": false,
"Pin4Value": true, "Pin4Value": true,
"Pin5Value": false, "Pin5Value": false,
"Pin6Value": true, "Pin6Value": false,
"Pin7Value": false, "Pin7Value": false,
"Pin8Value": true, "Pin8Value": false,
"Pin9Value": true, "Pin9Value": false,
"Pin1Disabled": false, "Pin1Disabled": true,
"Pin2Disabled": true, "Pin2Disabled": true,
"Pin3Disabled": false, "Pin3Disabled": false,
"Pin4Disabled": true, "Pin4Disabled": true,
"Pin5Disabled": false, "Pin5Disabled": false,
"Pin6Disabled": true, "Pin6Disabled": false,
"Pin7Disabled": false, "Pin7Disabled": false,
"Pin8Disabled": true, "Pin8Disabled": false,
"Pin9Disabled": true, "Pin9Disabled": false,
"ThrowsPerRound": 3, "ThrowsPerRound": 3,
"ThrowCounter": 3, "ThrowCounter": 3,
"ThrowMode": 1 "ThrowMode": 1