Skip to content

Commit 6ce1f42

Browse files
authored
Merge pull request #190 from davidwengier/SaveTweaks
2 parents bfce9e0 + 5207d04 commit 6ce1f42

File tree

19 files changed

+80
-109
lines changed

19 files changed

+80
-109
lines changed

src/BlazingTrains/BlazingTrains.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="BlazorBeforeUnload" Version="1.0.4" />
2120
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-rc.2.21480.10" />
2221
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-rc.2.21480.10" PrivateAssets="all" />
2322

src/BlazingTrains/BlazorGameStorage.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ namespace BlazingTrains;
55

66
public class BlazorGameStorage : IGameStorage
77
{
8+
private ISyncLocalStorageService? _syncLocalStorageService;
9+
private readonly Dictionary<string, string> _lastSavedValue = new Dictionary<string, string>();
10+
811
public IServiceProvider? AspNetCoreServices { get; set; }
912

10-
private ISyncLocalStorageService? SyncLocalStorageService => this.AspNetCoreServices?.GetService<ISyncLocalStorageService>();
13+
private ISyncLocalStorageService? SyncLocalStorageService
14+
{
15+
get
16+
{
17+
return (_syncLocalStorageService ??= this.AspNetCoreServices?.GetService<ISyncLocalStorageService>());
18+
}
19+
}
1120

1221
public string? Read(string key)
1322
{
1423
var data = this.SyncLocalStorageService?.GetItemAsString(key);
1524
return data;
1625
}
1726

18-
private readonly Dictionary<string, string> _lastSavedValue = new Dictionary<string, string>();
1927
public void Write(string key, string value)
2028
{
2129
var valueExists = _lastSavedValue.TryGetValue(key, out var previousValue);

src/BlazingTrains/BlazorTimer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class BlazorTimer : ITimer
1717

1818
public void Dispose()
1919
{
20-
throw new NotImplementedException();
20+
Stop();
2121
}
2222

2323
public void Start()

src/BlazingTrains/Commands/ChangeSaveModeCommand.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/BlazingTrains/Pages/Index.razor

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
@using SkiaSharp.Views.Blazor
44
@using Trains.NET.Rendering
55
@using Trains.NET.Rendering.Skia
6-
@using blazejewicz.Blazor.BeforeUnload
7-
@inject BeforeUnload BeforeUnload
8-
@implements IDisposable
96

107
<PageTitle>Trains - @@davidwengier - @ThisAssembly.AssemblyInformationalVersion</PageTitle>
118

src/BlazingTrains/Pages/Index.razor.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.AspNetCore.Components.Web;
22
using SkiaSharp;
33
using SkiaSharp.Views.Blazor;
4-
using Trains.NET.Engine;
54
using Trains.NET.Instrumentation;
65
using Trains.NET.Rendering;
76
using Trains.NET.Rendering.Skia;
@@ -22,8 +21,6 @@ protected override async Task OnInitializedAsync()
2221
_interactionManager = DI.ServiceLocator.GetService<IInteractionManager>();
2322

2423
await _game.InitializeAsync(200, 200);
25-
26-
this.BeforeUnload.BeforeUnloadHandler += BeforeUnload_BeforeUnloadHandler;
2724
}
2825

2926
private void OnPaintSurface(SKPaintGLSurfaceEventArgs e)
@@ -114,15 +111,4 @@ private void OnMouseWheel(WheelEventArgs e)
114111
_interactionManager.PointerZoomOut((int)e.ClientX, (int)e.ClientY);
115112
}
116113
}
117-
118-
private void BeforeUnload_BeforeUnloadHandler(object? sender, blazejewicz.Blazor.BeforeUnload.BeforeUnloadArgs e)
119-
{
120-
DI.ServiceLocator.GetService<IGameStateManager>().Save();
121-
_game.Dispose();
122-
}
123-
124-
public void Dispose()
125-
{
126-
this.BeforeUnload.BeforeUnloadHandler -= BeforeUnload_BeforeUnloadHandler;
127-
}
128114
}

src/BlazingTrains/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
builder.RootComponents.Add<App>("#app");
99
builder.RootComponents.Add<HeadOutlet>("head::after");
1010

11-
builder.Services.AddBeforeUnload();
1211
builder.Services.AddBlazoredLocalStorage();
1312
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1413

src/Trains.NET.Engine/GamePlay/GameManager.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5-
using Trains.NET.Engine.Utilities;
65
using Trains.NET.Instrumentation;
76

87
namespace Trains.NET.Engine;
@@ -15,7 +14,6 @@ public class GameManager : IGameManager, IInitializeAsync
1514
private ITool _currentTool;
1615
private readonly ITool _defaultTool;
1716
private readonly ITimer _gameLoopTimer;
18-
private readonly IGameStateManager _gameStateManager;
1917
private readonly IEnumerable<IGameStep> _gameSteps;
2018
private readonly ElapsedMillisecondsTimedStat _gameUpdateTime = InstrumentationBag.Add<ElapsedMillisecondsTimedStat>("Game-LoopStepTime");
2119

@@ -44,7 +42,7 @@ public ITool CurrentTool
4442
}
4543
}
4644

47-
public GameManager(IEnumerable<ITool> tools, IEnumerable<IGameStep> gameSteps, ITimer timer, IGameStateManager gameStateManager)
45+
public GameManager(IEnumerable<ITool> tools, IEnumerable<IGameStep> gameSteps, ITimer timer)
4846
{
4947
_defaultTool = tools.First();
5048
_currentTool = _defaultTool;
@@ -54,7 +52,6 @@ public GameManager(IEnumerable<ITool> tools, IEnumerable<IGameStep> gameSteps, I
5452

5553
_gameLoopTimer.Interval = GameLoopInterval;
5654
_gameLoopTimer.Elapsed += GameLoopTimerElapsed;
57-
_gameStateManager = gameStateManager;
5855
}
5956

6057
public Task InitializeAsync(int columns, int rows)
@@ -74,8 +71,6 @@ public void GameLoopStep()
7471
foreach (var gameStep in _gameSteps)
7572
{
7673
gameStep.Update(timeSinceLastTick);
77-
if (_gameStateManager.SaveMode == SaveModes.GameStep)
78-
_gameStateManager.Save();
7974
}
8075
}
8176
}
Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
11
using System.Collections.Generic;
22
using System.Threading.Tasks;
3-
using Trains.NET.Engine.Utilities;
43
using Trains.NET.Instrumentation;
54
using Trains.NET.Instrumentation.Stats;
65

76
namespace Trains.NET.Engine;
87

98
[Order(999999)]
10-
public class GameStateManager : IGameStateManager, IInitializeAsync
9+
public class GameStateManager : IGameStateManager, IInitializeAsync, IGameStep
1110
{
11+
// Auto-save every 2 seconds
12+
private const int AutosaveInterval = 2 * 60;
13+
14+
private int _autosaveCounter;
15+
private bool _autosaveEnabled;
16+
1217
private readonly IEnumerable<IGameState> _gameStates;
1318
private readonly IGameStorage _storage;
14-
private readonly ITimer _timer;
15-
private readonly InformationStat _saveModeStat = InstrumentationBag.Add<InformationStat>("Save-Mode");
19+
private readonly InformationStat _autosaveStat = InstrumentationBag.Add<InformationStat>("Autosave");
1620
private readonly ElapsedMillisecondsTimedStat _saveTimeStat = InstrumentationBag.Add<ElapsedMillisecondsTimedStat>("Save-Time");
1721

18-
public SaveModes SaveMode { get; private set; }
22+
public bool AutosaveEnabled
23+
{
24+
get { return _autosaveEnabled; }
25+
set
26+
{
27+
_autosaveEnabled = value;
28+
if (_autosaveEnabled)
29+
{
30+
_autosaveCounter = 0;
31+
Save();
32+
}
33+
else
34+
{
35+
// if they're turning off autosave, we want to at least save that
36+
// otherwise if they leave before the next save, it could be back on
37+
// on load
38+
_storage.Write("Autosave", this.AutosaveEnabled.ToString());
39+
}
40+
}
41+
}
1942

20-
public GameStateManager(IEnumerable<IGameState> gameStates, IGameStorage storage, ITimer timer)
43+
public GameStateManager(IEnumerable<IGameState> gameStates, IGameStorage storage)
2144
{
2245
_gameStates = gameStates;
2346
_storage = storage;
24-
_timer = timer;
25-
_saveModeStat.Information = $"{this.SaveMode}";
2647
}
2748

2849
public Task InitializeAsync(int columns, int rows)
2950
{
3051
Load();
3152

32-
_timer.Interval = 1000;
33-
_timer.Elapsed += _timer_Elapsed;
34-
3553
return Task.CompletedTask;
3654
}
3755

38-
private void _timer_Elapsed(object? sender, System.EventArgs e)
39-
{
40-
Save();
41-
}
42-
4356
public void Load()
4457
{
4558
foreach (var gameState in _gameStates)
@@ -51,6 +64,7 @@ public void Load()
5164
break;
5265
}
5366
}
67+
this.AutosaveEnabled = _storage.Read("Autosave")?.Equals("True") ?? true;
5468
}
5569

5670
public void Reset()
@@ -69,22 +83,19 @@ public void Save()
6983
{
7084
gameState.Save(_storage);
7185
}
86+
_storage.Write("Autosave", this.AutosaveEnabled.ToString());
7287
}
7388
}
7489

75-
public void ChangeSaveMode()
90+
public void Update(long timeSinceLastTick)
7691
{
77-
this.SaveMode = this.SaveMode switch
92+
_autosaveStat.Information = this.AutosaveEnabled ? "On" : "Off";
93+
94+
if (this.AutosaveEnabled &&
95+
++_autosaveCounter > AutosaveInterval)
7896
{
79-
SaveModes.Manual => SaveModes.GameStep,
80-
SaveModes.GameStep => SaveModes.Timer,
81-
SaveModes.Timer => SaveModes.Manual,
82-
_ => SaveModes.Manual
83-
};
84-
_saveModeStat.Information = $"{this.SaveMode}";
85-
if (this.SaveMode == SaveModes.Timer)
86-
_timer.Start();
87-
else
88-
_timer.Stop();
97+
Save();
98+
_autosaveCounter = 0;
99+
}
89100
}
90101
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using Trains.NET.Engine.Utilities;
2-
3-
namespace Trains.NET.Engine;
1+
namespace Trains.NET.Engine;
42

53
public interface IGameStateManager
64
{
7-
SaveModes SaveMode { get; }
5+
bool AutosaveEnabled { get; set; }
86

97
void Load();
108
void Save();
119
void Reset();
12-
void ChangeSaveMode();
1310
}

0 commit comments

Comments
 (0)