AutoSaveService
TheAutoSaveService automatically saves the game state to the Convex database at regular intervals. It monitors for changes and only persists data when the state has been modified, reducing unnecessary database writes.
Location
Properties
Time between auto-save checks in milliseconds (5 minutes = 300000ms)
Internal flag tracking whether auto-save has been initialized
Cached copy of the last saved state used for change detection
Methods
startAutoSave()
Begins the automatic save process. Sets up an interval that checks for state changes and saves when needed.The service automatically calls
startAutoSave() once the character data has been loaded from the database. You typically don’t need to call this manually.checkAndSave()
Compares the current game state with the last saved state and triggers a database update if changes are detected.Change detection uses JSON string comparison. This is simple but effective for the game’s data structure.
stopAutoSave()
Stops the automatic save interval. Called when the application component is destroyed.Automatic initialization
The service uses an Angular effect to automatically start auto-saving once the character data is loaded from the database:- Auto-save only starts after data is loaded from the database
- It only initializes once per session
- No saves occur before the initial data load completes
Save frequency
The default save interval is 5 minutes (300,000 milliseconds). This balances data persistence with server load:- Too frequent: Increases database operations and API costs
- Too infrequent: Increases risk of data loss on unexpected exits
GameState interface
The service works with theGameState interface defined in the file:
Related services
- GameStateService - Aggregates state and coordinates database updates
- CharacterService - Manages character data and provides the loaded state signal