From 3e42cc1bb926eb3b40bf9fced87ff8e5bab19cc1 Mon Sep 17 00:00:00 2001 From: beo3000 Date: Fri, 21 Nov 2025 17:53:36 +0100 Subject: [PATCH] dev state handling --- .../Controls/ChangePlayerSelect.razor | 2 +- .../Components/Dialogs/StartGameDialog.razor | 2 +- KoogleApp/DayState.json | 8 +- KoogleApp/Model/GameStatus.cs | 3 + KoogleApp/Model/StartParams.cs | 4 +- KoogleApp/Services/GameStatusDataService.cs | 31 ++++++- KoogleApp/Store/Game/Participants/Actions.cs | 11 ++- KoogleApp/Store/Game/Participants/Effects.cs | 17 ++++ KoogleApp/Store/Game/Participants/Reducers.cs | 22 ++--- KoogleApp/Store/Game/Setup/Actions.cs | 6 +- KoogleApp/Store/Game/Setup/Effects.cs | 38 +++++++++ KoogleApp/Store/Game/Setup/Reducers.cs | 8 ++ KoogleApp/Store/Game/ThrowPanel/Actions.cs | 1 + KoogleApp/Store/Game/ThrowPanel/Effects.cs | 11 ++- KoogleApp/appdata.json | 84 +++++-------------- KoogleApp/participantsState.json | 10 +++ 16 files changed, 168 insertions(+), 90 deletions(-) create mode 100644 KoogleApp/Store/Game/Setup/Effects.cs create mode 100644 KoogleApp/participantsState.json diff --git a/KoogleApp/Components/Controls/ChangePlayerSelect.razor b/KoogleApp/Components/Controls/ChangePlayerSelect.razor index 234ffe9..bf79d95 100644 --- a/KoogleApp/Components/Controls/ChangePlayerSelect.razor +++ b/KoogleApp/Components/Controls/ChangePlayerSelect.razor @@ -39,7 +39,7 @@ set { if (value != null) - Dispatcher.Dispatch(new SelectedPlayerChangedAction(value.Id)); + Dispatcher.Dispatch(new SelectedPlayerChangedAction(ParticipantsState.Value, value.Id)); } } diff --git a/KoogleApp/Components/Dialogs/StartGameDialog.razor b/KoogleApp/Components/Dialogs/StartGameDialog.razor index 91c65b5..9a592fa 100644 --- a/KoogleApp/Components/Dialogs/StartGameDialog.razor +++ b/KoogleApp/Components/Dialogs/StartGameDialog.razor @@ -87,6 +87,6 @@ private void Start() { - MudDialog.Close(DialogResult.Ok(new StartParams(DayState.Value.Id, SetupState.Value.ThrowMode, SetupState.Value.ThrowsPerRound, PlayerIds))); + MudDialog.Close(DialogResult.Ok(new StartParams(DayState.Value.Id, SetupState.Value.ThrowMode, SetupState.Value.ThrowsPerRound, PlayerIds, SetupState.Value.ParticipantsMode))); } } diff --git a/KoogleApp/DayState.json b/KoogleApp/DayState.json index 995d6fe..e3ad331 100644 --- a/KoogleApp/DayState.json +++ b/KoogleApp/DayState.json @@ -1,13 +1,15 @@ { - "Id": 34, - "Date": "2025-11-19T00:00:00", + "Id": 35, + "Date": "2025-11-21T00:00:00", "Status": 1, "PlayerIds": [ 5, 3, + 10, + 12, 9 ], - "CreatedAt": "2025-11-19T16:42:56.6330335", + "CreatedAt": "2025-11-21T16:30:35.9889525", "ModifiedAt": null, "ModifiedBy": "" } \ No newline at end of file diff --git a/KoogleApp/Model/GameStatus.cs b/KoogleApp/Model/GameStatus.cs index b101b42..4e99570 100644 --- a/KoogleApp/Model/GameStatus.cs +++ b/KoogleApp/Model/GameStatus.cs @@ -1,4 +1,5 @@ using KoogleApp.Store.Game.Participants; +using KoogleApp.Store.Game.Setup; using KoogleApp.Store.Game.ThrowPanel; namespace KoogleApp.Model @@ -14,6 +15,8 @@ namespace KoogleApp.Model public class GameStatus { + public SetupState SetupState { get; set; } + public ThrowPanelState? ThrowPanelState { get; set; } public ParticipantsState? ParticipantsState { get; set; } diff --git a/KoogleApp/Model/StartParams.cs b/KoogleApp/Model/StartParams.cs index 6d5698e..d89afd2 100644 --- a/KoogleApp/Model/StartParams.cs +++ b/KoogleApp/Model/StartParams.cs @@ -2,10 +2,10 @@ namespace KoogleApp.Model { - public record StartParams(int DayId, ThrowMode ThrowMode, int ThrowsPerRound, int[] Participants) + public record StartParams(int DayId, ThrowMode ThrowMode, int ThrowsPerRound, int[] Participants, ParticipantsMode ParticipantsMode) { - public StartParams() : this(DayId: 0, ThrowMode: ThrowMode.Reposition, ThrowsPerRound: 3, []) + public StartParams() : this(DayId: 0, ThrowMode: ThrowMode.Reposition, ThrowsPerRound: 3, [], ParticipantsMode: Store.Game.ThrowPanel.ParticipantsMode.FreeToChoose) { } } diff --git a/KoogleApp/Services/GameStatusDataService.cs b/KoogleApp/Services/GameStatusDataService.cs index 82f985a..c8d604f 100644 --- a/KoogleApp/Services/GameStatusDataService.cs +++ b/KoogleApp/Services/GameStatusDataService.cs @@ -19,6 +19,7 @@ namespace KoogleApp.Services Task SaveToDatabaseAndReset(GameStatus lastContent, string username); void Initialize(GameStatus content, string username); + GameStatusSnapshot GetInitialData(); } public class GameStatusDataService : IGameStatusDataService @@ -35,7 +36,7 @@ namespace KoogleApp.Services } - public GameStatusSnapshot GetCurrentData() + public GameStatusSnapshot GetCurrentData() { lock (_lock) { @@ -57,6 +58,33 @@ namespace KoogleApp.Services } } + public GameStatusSnapshot GetInitialData() + { + lock (_lock) + { + var stateCurrent = GetCurrentData(); + if (stateCurrent == null) + { + LoadFromFile(); + } + + if (_undoStack != null && _undoStack.Count > 0) + { + var items = _undoStack.ToList(); + var state = items.First(); + return new GameStatusSnapshot + { + Status = state.Status, + LastModified = state.LastModified, + LastModifiedBy = state.LastModifiedBy, + Version = state.Version + }; + } + + return GetCurrentData(); + } + } + public Task SaveToDatabaseAndReset(GameStatus lastContent, string username) { UpdateData(lastContent, username); @@ -92,7 +120,6 @@ namespace KoogleApp.Services _redoStack.Clear(); } } - public void UpdateData(GameStatus content, string username) { if (_currentData == null) diff --git a/KoogleApp/Store/Game/Participants/Actions.cs b/KoogleApp/Store/Game/Participants/Actions.cs index bedab79..18f0ffb 100644 --- a/KoogleApp/Store/Game/Participants/Actions.cs +++ b/KoogleApp/Store/Game/Participants/Actions.cs @@ -1,4 +1,5 @@ -using KoogleApp.Store.Player; +using KoogleApp.Store.Game.ThrowPanel; +using KoogleApp.Store.Player; namespace KoogleApp.Store.Game.Participants { @@ -12,7 +13,13 @@ namespace KoogleApp.Store.Game.Participants public record ParticipantsStateLoadedAction(ParticipantsState ParticipantsState); + public record SetParticipatingPlayersAction(int[] PlayerIds); + public record DeleteParticipantsSessionAction(); - public record SelectedPlayerChangedAction(int Id); + public record SelectedPlayerChangedAction(ParticipantsState ParticipantsState, int NewId); + + + + } diff --git a/KoogleApp/Store/Game/Participants/Effects.cs b/KoogleApp/Store/Game/Participants/Effects.cs index be19e19..2e5a4af 100644 --- a/KoogleApp/Store/Game/Participants/Effects.cs +++ b/KoogleApp/Store/Game/Participants/Effects.cs @@ -23,6 +23,23 @@ namespace KoogleApp.Store.Game.Participants dispatcher.Dispatch(new BroadcastParticipantsStateAction(action.ParticipantsState)); } + [EffectMethod] + public async Task HandelParticipantsStateChangedAction(SelectedPlayerChangedAction action, + IDispatcher dispatcher) + { + var lst = action.ParticipantsState.PlayerIds.ToList(); + lst.Remove(action.NewId); + lst.Insert(0, action.NewId); + + var newState = action.ParticipantsState with + { + PlayerIds = lst.ToArray() + }; + + dispatcher.Dispatch(new ParticipantsStateChangedAction(newState)); + dispatcher.Dispatch(new ParticipantsStateLoadedAction(newState)); + } + [EffectMethod] public async Task HandelSaveParticipantsStateAction(SaveParticipantsStateAction action, IDispatcher dispatcher) { diff --git a/KoogleApp/Store/Game/Participants/Reducers.cs b/KoogleApp/Store/Game/Participants/Reducers.cs index 777dc88..32ea977 100644 --- a/KoogleApp/Store/Game/Participants/Reducers.cs +++ b/KoogleApp/Store/Game/Participants/Reducers.cs @@ -23,18 +23,18 @@ namespace KoogleApp.Store.Game.Participants } - [ReducerMethod] - public static ParticipantsState OnSelectedPlayerChangedAction(ParticipantsState state, SelectedPlayerChangedAction action) - { - var lst = state.PlayerIds.ToList(); - lst.Remove(action.Id); - lst.Insert(0,action.Id); + //[ReducerMethod] + //public static ParticipantsState OnSelectedPlayerChangedAction(ParticipantsState state, SelectedPlayerChangedAction action) + //{ + // var lst = state.PlayerIds.ToList(); + // lst.Remove(action.NewId); + // lst.Insert(0,action.NewId); - return state with - { - PlayerIds = lst.ToArray() - }; - } + // return state with + // { + // PlayerIds = lst.ToArray() + // }; + //} [ReducerMethod] public static ParticipantsState OnUpdateStateAfterUndoRedo(ParticipantsState state, UpdateStateAfterUndoRedo action) diff --git a/KoogleApp/Store/Game/Setup/Actions.cs b/KoogleApp/Store/Game/Setup/Actions.cs index 04d2cfe..dd3a252 100644 --- a/KoogleApp/Store/Game/Setup/Actions.cs +++ b/KoogleApp/Store/Game/Setup/Actions.cs @@ -6,7 +6,9 @@ namespace KoogleApp.Store.Game.Setup public record SetThrowsPerRoundAction(int ThrowsPerRound); - public record SetParticipatingPlayersAction(int[] PlayerIds); - public record SetParticipantsModeAction(ParticipantsMode ParticipantsMode); + + public record LoadSetupStatesFromSessionAction(); + + public record SetupStateLoadedAction(SetupState SetupState); } diff --git a/KoogleApp/Store/Game/Setup/Effects.cs b/KoogleApp/Store/Game/Setup/Effects.cs new file mode 100644 index 0000000..7e74889 --- /dev/null +++ b/KoogleApp/Store/Game/Setup/Effects.cs @@ -0,0 +1,38 @@ +using Fluxor; +using KoogleApp.Services; +using KoogleApp.Store.Game.Participants; + +namespace KoogleApp.Store.Game.Setup +{ + + public class SetupStateEffects( + SessionStorage sessionStorage, + HubConnectionService sharedHubService, + IGameStatusDataService dataService) + { + private readonly SessionStorage _sessionStorage = sessionStorage; + //private const string _dataFilePath = "setupState.json"; + //private const string _dataFilePath = "participantsState.json"; + private readonly HubConnectionService _sharedHubService = sharedHubService; + private readonly IGameStatusDataService _dataService = dataService; + + + + [EffectMethod] + public async Task HandelLoadSetupStatesFromSessionAction(LoadSetupStatesFromSessionAction action, + IDispatcher dispatcher) + { + var data = _dataService.GetInitialData(); + if (data.Status != null) + { + var state = data.Status.SetupState; + if (state != null) + { + dispatcher.Dispatch(new SetupStateLoadedAction(state)); + } + } + } + + + } +} diff --git a/KoogleApp/Store/Game/Setup/Reducers.cs b/KoogleApp/Store/Game/Setup/Reducers.cs index 1d05207..e31f261 100644 --- a/KoogleApp/Store/Game/Setup/Reducers.cs +++ b/KoogleApp/Store/Game/Setup/Reducers.cs @@ -1,4 +1,6 @@ using Fluxor; +using KoogleApp.Store.Game.Participants; +using KoogleApp.Store.Game.ThrowPanel; namespace KoogleApp.Store.Game.Setup { @@ -39,5 +41,11 @@ namespace KoogleApp.Store.Game.Setup ParticipantsMode = action.ParticipantsMode }; } + + [ReducerMethod] + public static SetupState OnSetupStateLoadedAction(SetupState state, SetupStateLoadedAction action) + { + return action.SetupState; + } } } diff --git a/KoogleApp/Store/Game/ThrowPanel/Actions.cs b/KoogleApp/Store/Game/ThrowPanel/Actions.cs index 9b926ad..b63380f 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Actions.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Actions.cs @@ -1,5 +1,6 @@ using KoogleApp.Model; using KoogleApp.Store.Game.Participants; +using KoogleApp.Store.Game.Setup; namespace KoogleApp.Store.Game.ThrowPanel { diff --git a/KoogleApp/Store/Game/ThrowPanel/Effects.cs b/KoogleApp/Store/Game/ThrowPanel/Effects.cs index bb5438c..048f2ca 100644 --- a/KoogleApp/Store/Game/ThrowPanel/Effects.cs +++ b/KoogleApp/Store/Game/ThrowPanel/Effects.cs @@ -55,17 +55,23 @@ namespace KoogleApp.Store.Game.ThrowPanel public async Task HandleStartAction(StartStopAction startStopAction, IDispatcher dispatcher) { ParticipantsState? participantsState = null; + SetupState? setupState = null; if (startStopAction.StartParams != null) // start action { dispatcher.Dispatch(new SetParticipatingPlayersAction(startStopAction.StartParams.Participants)); // here we just want to pass the participants with ThrowPanelStateChangedAction - therefore we create a copy of ParticipantsState - I think this is valid just for the startup of a new game participantsState = new ParticipantsState(startStopAction.StartParams.Participants, []); + setupState = new SetupState(startStopAction.StartParams.ThrowMode, + startStopAction.StartParams.ThrowsPerRound, startStopAction.StartParams.Participants, + ParticipantsMode: startStopAction.StartParams.ParticipantsMode); var username = await GetUsername(); _dataService.Initialize(new GameStatus() { ThrowPanelState = _throwPanelState.Value, + ParticipantsState = participantsState, + SetupState = setupState }, username); } else // stop action @@ -220,14 +226,15 @@ namespace KoogleApp.Store.Game.ThrowPanel } [EffectMethod] - public async Task HandelLoadStateAction(LoadAllStatesFromSessionAction allStatesFromSessionAction, IDispatcher dispatcher) + public async Task HandelLoadAllStatesFromSessionAction(LoadAllStatesFromSessionAction allStatesFromSessionAction, IDispatcher dispatcher) { dispatcher.Dispatch(new LoadThrowPanelStatesFromSessionAction()); dispatcher.Dispatch(new LoadParticipantsStatesFromSessionAction()); + dispatcher.Dispatch(new LoadSetupStatesFromSessionAction()); } [EffectMethod] - public async Task HandelLoadStateAction(LoadThrowPanelStatesFromSessionAction allStatesFromSessionAction, IDispatcher dispatcher) + public async Task HandelLoadThrowPanelStatesFromSessionAction(LoadThrowPanelStatesFromSessionAction allStatesFromSessionAction, IDispatcher dispatcher) { var state = await _sessionStorage.GetThrowPanelStateAsync(); diff --git a/KoogleApp/appdata.json b/KoogleApp/appdata.json index 083c353..06d712c 100644 --- a/KoogleApp/appdata.json +++ b/KoogleApp/appdata.json @@ -1,6 +1,7 @@ { "CurrentData": { "Status": { + "SetupState": null, "ThrowPanelState": { "IsStated": true, "BellValue": false, @@ -14,62 +15,42 @@ "Pin8State": 0, "Pin9State": 0, "ThrowsPerRound": 3, - "ThrowCounterPerRound": 2, + "ThrowCounterPerRound": 1, "ThrowMode": 0, - "ThrowPanelStateStatus": 3, - "ThrowCounter": 1, - "DayId": 34 + "ThrowPanelStateStatus": 1, + "ThrowCounter": 0, + "DayId": 35 }, "ParticipantsState": { "PlayerIds": [ 5, 3, + 10, + 12, 9 ], "Eliminated": [] } }, - "Version": 4, - "LastModified": "2025-11-21T14:49:53.2943989+01:00", + "Version": 2, + "LastModified": "2025-11-21T17:52:34.7010655+01:00", "LastModifiedBy": "test1@test.de" }, "UndoHistory": [ { "Status": { - "ThrowPanelState": { - "IsStated": true, - "BellValue": false, - "Pin1State": 0, - "Pin2State": 0, - "Pin3State": 0, - "Pin4State": 1, - "Pin5State": 1, - "Pin6State": 1, - "Pin7State": 1, - "Pin8State": 1, - "Pin9State": 1, - "ThrowsPerRound": 3, - "ThrowCounterPerRound": 1, + "SetupState": { "ThrowMode": 0, - "ThrowPanelStateStatus": 2, - "ThrowCounter": 0, - "DayId": 34 - }, - "ParticipantsState": { - "PlayerIds": [ + "ThrowsPerRound": 3, + "Players": [ 5, 3, + 10, + 12, 9 ], - "Eliminated": [] - } - }, - "Version": 3, - "LastModified": "2025-11-21T14:49:53.289455+01:00", - "LastModifiedBy": "test1@test.de" - }, - { - "Status": { + "ParticipantsMode": 1 + }, "ThrowPanelState": { "IsStated": true, "BellValue": false, @@ -87,46 +68,21 @@ "ThrowMode": 0, "ThrowPanelStateStatus": 1, "ThrowCounter": 0, - "DayId": 34 + "DayId": 35 }, "ParticipantsState": { "PlayerIds": [ 5, 3, + 10, + 12, 9 ], "Eliminated": [] } }, - "Version": 2, - "LastModified": "2025-11-21T14:49:31.5450392+01:00", - "LastModifiedBy": "test1@test.de" - }, - { - "Status": { - "ThrowPanelState": { - "IsStated": true, - "BellValue": false, - "Pin1State": 0, - "Pin2State": 0, - "Pin3State": 0, - "Pin4State": 0, - "Pin5State": 0, - "Pin6State": 0, - "Pin7State": 0, - "Pin8State": 0, - "Pin9State": 0, - "ThrowsPerRound": 3, - "ThrowCounterPerRound": 1, - "ThrowMode": 0, - "ThrowPanelStateStatus": 1, - "ThrowCounter": 0, - "DayId": 34 - }, - "ParticipantsState": null - }, "Version": 1, - "LastModified": "2025-11-21T14:49:31.5423511+01:00", + "LastModified": "2025-11-21T17:52:34.6986698+01:00", "LastModifiedBy": "test1@test.de" } ], diff --git a/KoogleApp/participantsState.json b/KoogleApp/participantsState.json new file mode 100644 index 0000000..19db104 --- /dev/null +++ b/KoogleApp/participantsState.json @@ -0,0 +1,10 @@ +{ + "PlayerIds": [ + 12, + 5, + 3, + 10, + 9 + ], + "Eliminated": [] +} \ No newline at end of file