]> git.cworth.org Git - empires-server/commitdiff
empathy: 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:19:30 +0000 (19:19 -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.

empathy.js

index 7dc850d6628ccaffcb5f8e57c1c2b13395ac6a22..cfc80bdd9439a2684dc334e1e5ca5bd66f30ad0b 100644 (file)
@@ -642,6 +642,26 @@ class Empathy extends Game {
     this.broadcast_event_object('scores', this.state.scores);
   }
 
+  serialize() {
+    const data = super.serialize();
+    data.next_prompt_id = this.next_prompt_id;
+    data.equivalencies = this.equivalencies;
+    data.answering_start_time_ms = this.answering_start_time_ms;
+    data.judging_start_time_ms = this.judging_start_time_ms;
+    data.answers = this.answers.map(a => ({
+      session_id: a.player.session_id,
+      answers: a.answers
+    }));
+    data.kudos = {};
+    for (const [name, k] of Object.entries(this.kudos)) {
+      data.kudos[name] = {
+        session_id: k.player.session_id,
+        words: k.words
+      };
+    }
+    return data;
+  }
+
   restore(data) {
     super.restore(data);
     this.state.players_answering = new Set(this.state.players_answering);
@@ -649,6 +669,29 @@ class Empathy extends Game {
     this.state.players_judging = new Set(this.state.players_judging);
     this.state.end_judging = new Set(this.state.end_judging);
     this.state.new_game_votes = new Set(this.state.new_game_votes);
+
+    this.next_prompt_id = data.next_prompt_id || 1;
+    this.equivalencies = data.equivalencies || {};
+    this.answering_start_time_ms = data.answering_start_time_ms || 0;
+    this.judging_start_time_ms = data.judging_start_time_ms || 0;
+
+    if (data.answers) {
+      this.answers = data.answers
+        .filter(a => this.players_by_session[a.session_id])
+        .map(a => ({
+          player: this.players_by_session[a.session_id],
+          answers: a.answers
+        }));
+    }
+
+    if (data.kudos) {
+      for (const [name, k] of Object.entries(data.kudos)) {
+        const player = this.players_by_session[k.session_id];
+        if (player) {
+          this.kudos[name] = { player, words: k.words };
+        }
+      }
+    }
   }
 }