This commit is contained in:
beo3000 2025-11-10 21:35:19 +01:00
parent 26f08d9dc1
commit 4b921835e4
7 changed files with 174 additions and 105 deletions

View File

@ -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<ThrowPanelState>("ReceiveThrowPanelState", state =>
{
dispatcher.Dispatch(new ReceiveStateFromServerAction(state));
});
// _hubConnection.On<string>("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);

View File

@ -11,7 +11,7 @@
public class GameStatus
{
public string Content { get; set; }
public ThrowPanelState? ThrowPanelState { get; set; }
}
//public class DataSnapshot

View File

@ -32,7 +32,7 @@ namespace KoogleApp.Services
{
Status = new GameStatus
{
Content = "Willkommen! Starte die Zusammenarbeit.",
},
Version = 1,
LastModified = DateTime.Now,

View File

@ -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<ThrowPanelState>("ReceiveThrowPanelState", state =>
{
dispatcher.Dispatch(new ReceiveStateFromServerAction(state));
});
// _hubConnection.On<string>("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
}
}
}
}

View File

@ -11,29 +11,32 @@ namespace KoogleApp.Store.Game.ThrowPanel
public class ThrowPanelEffects : IDisposable
{
private readonly ILogger<ThrowPanelEffects> _logger;
private readonly HubConnectionService _sharedModelHub;
private readonly HubConnectionService _sharedHubService;
private readonly IState<ThrowPanelState> _state;
private readonly SessionStorage _sessionStorage;
private readonly string _dataFilePath = "ThrowPanelState.json";
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly IGameStatusDataService _dataService;
public ThrowPanelEffects(
ILogger<ThrowPanelEffects> logger,
HubConnectionService sharedModelHub,
HubConnectionService sharedHubService,
IState<ThrowPanelState> 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);
}

View File

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

View File

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