Fix stale restore after game restart

This commit is contained in:
2026-05-20 22:27:36 +02:00
parent 8258ea2321
commit beac5a2be3
12 changed files with 167 additions and 44 deletions
+17 -1
View File
@@ -33,6 +33,8 @@ class SocketClientModule extends BaseModule {
this.pendingCommand = null;
this.gameApiTimeoutMs = GAME_API_TIMEOUT_MS;
this.playerCommandTimeoutMs = PLAYER_COMMAND_TIMEOUT_MS;
this.gameApiRequestId = 0;
this.latestNarrativeRequestId = 0;
// Bind methods using parent's bindMethods utility
this.bindMethods([
@@ -220,6 +222,15 @@ class SocketClientModule extends BaseModule {
// Special handling for narrative text
this.socket.on('narrativeResponse', (data) => {
const responseRequestId = Number(data?.clientRequestId || 0);
if (responseRequestId > 0 && responseRequestId !== this.latestNarrativeRequestId) {
console.warn('Socket Client: Ignoring stale narrative response', {
responseRequestId,
latestNarrativeRequestId: this.latestNarrativeRequestId,
turnId: data?.turnId
});
return;
}
this.clearPendingCommand('narrative-response');
this.processTurnResult(data);
});
@@ -834,6 +845,11 @@ class SocketClientModule extends BaseModule {
return;
}
const requestId = ++this.gameApiRequestId;
const normalizedMethod = String(method || '').replace(/\(\)$/, '');
if (['newGame', 'loadGame', 'chooseChoice'].includes(normalizedMethod)) {
this.latestNarrativeRequestId = requestId;
}
let settled = false;
const finish = (response) => {
if (settled) return;
@@ -852,7 +868,7 @@ class SocketClientModule extends BaseModule {
finish({ success: false, error: 'timeout', method });
}, this.gameApiTimeoutMs);
this.socket.emit('gameApi', { method, args }, (response) => {
this.socket.emit('gameApi', { method, args, requestId }, (response) => {
finish(response);
});
});