Skip to main content

AutoSaveService

The AutoSaveService 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

src/app/services/autosave-service.ts

Properties

AUTO_SAVE_INTERVAL
number
default:"300000"
Time between auto-save checks in milliseconds (5 minutes = 300000ms)
autoSaveHasStarted
boolean
default:"false"
Internal flag tracking whether auto-save has been initialized
lastSavedGameState
GameState | undefined
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.
startAutoSave(): void
Example:
constructor(private autoSaveService: AutoSaveService) {
  // Auto-save starts automatically when character data loads
}
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.
checkAndSave(): void
Implementation:
checkAndSave() {
  const currentGameState = this.GameStateService.getState();

  if (JSON.stringify(currentGameState) !== JSON.stringify(this.lastSavedGameState)) {
    this.GameStateService.pushUpdatesToDatabase();
    this.lastSavedGameState = currentGameState;
  }
}
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.
stopAutoSave(): void
Example:
ngOnDestroy() {
  this.autoSaveService.stopAutoSave();
}

Automatic initialization

The service uses an Angular effect to automatically start auto-saving once the character data is loaded from the database:
constructor(private GameStateService: GameStateService) {
  effect(() => {
    if (this.autoSaveHasStarted) return;
    if (this.GameStateService.characterService.hasLoadedFromDb()) {
      this.startAutoSave();
      this.autoSaveHasStarted = true;
    }
  });
}
This ensures that:
  • 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
The interval can be adjusted by modifying the AUTO_SAVE_INTERVAL property if you need more or less frequent saves during development.

GameState interface

The service works with the GameState interface defined in the file:
export interface GameState {
  goldUpgrades: Upgrade[];
  prestigeUpgrades: Upgrade[];
  character: Character;
}
This represents all the data that needs to be persisted across sessions.
Make sure to call stopAutoSave() in your root component’s ngOnDestroy hook to prevent memory leaks from the interval timer.

Build docs developers (and LLMs) love