Fix stale restore after game restart
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user