From 82c89c7fec8efa1afbe044bd946e6072655f41c7 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 8 Mar 2026 19:19:30 -0400 Subject: [PATCH] empathy: Implement sub-class serialize/restore for extra data 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 | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/empathy.js b/empathy.js index 7dc850d..cfc80bd 100644 --- a/empathy.js +++ b/empathy.js @@ -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 }; + } + } + } } } -- 2.45.2