11using System . Collections . Generic ;
22using System . Threading . Tasks ;
3- using Trains . NET . Engine . Utilities ;
43using Trains . NET . Instrumentation ;
54using Trains . NET . Instrumentation . Stats ;
65
76namespace 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}
0 commit comments