]> git.cworth.org Git - lmno-server/commitdiff
empires: Implement sub-class serialize/restore for extra data
authorCarl Worth <cworth@cworth.org>
Sun, 8 Mar 2026 23:19:30 +0000 (19:19 -0400)
committerCarl Worth <cworth@cworth.org>
Sun, 8 Mar 2026 23:20:57 +0000 (19:20 -0400)
This game contains a fair amount of data not in this.state, so
we need this functions to correctly save and restore an in-flight
game in the case of a server shutdown/restart.

empires.js

index 839f5f8be8dc56460b4c6165d4378d99de56f095..e0efdb82a76008f8c923968829a6b3420bea5e6d 100644 (file)
@@ -216,6 +216,34 @@ class Empires extends Game {
     this.broadcast_phase_change();
   }
 
+  serialize() {
+    const data = super.serialize();
+    data._spectators = this._spectators;
+    data.next_spectator_id = this.next_spectator_id;
+    data._players = this._players;
+    data.characters_to_reveal = this.characters_to_reveal;
+    data.phase = this.phase;
+    data.old_phase = this.old_phase;
+    data.reveal_index = this.reveal_index;
+    return data;
+  }
+
+  restore(data) {
+    super.restore(data);
+    this._spectators = data._spectators || [];
+    this.next_spectator_id = data.next_spectator_id || 1;
+    this._players = data._players || [];
+    this.characters_to_reveal = data.characters_to_reveal || null;
+    this.phase = data.phase || GamePhase.JOIN;
+    this.old_phase = data.old_phase;
+    this.reveal_index = data.reveal_index || 0;
+
+    /* Restart the reveal interval if we were mid-reveal. */
+    if (this.phase === GamePhase.REVEAL && this.characters_to_reveal) {
+      this.reveal_interval = setInterval(this.reveal_next.bind(this), 3000);
+    }
+  }
+
   handle_events(request, response) {
 
     super.handle_events(request, response);