viewer: ignore tentative-state echoes that don't match the turn buffer

A move response's tentative state replaced the local preview
unconditionally. If the user had undone down to an empty buffer (which
re-emits nothing) or already made another move, a late echo transiently
showed a phantom or regressed move (and a wrong sidebar log) until the
next response landed.

Only accept a tentative state whose visible log extends the committed log
by exactly the buffered moves, in order — validated against the engine:
every tentative echo satisfies this (all tentative moves are visible-log
moves; bids and bid-phase loans go to the hidden log but commit
immediately and never take this path). Stale echoes are dropped; the echo
for the current buffer, if one is in flight, follows anyway.
This commit is contained in:
coyotte508 2026-08-16 21:32:04 +00:00
commit 3f9d7d51b8

View file

@ -553,7 +553,7 @@ import { Vue, Component, Prop, Watch, Provide, ProvideReactive } from 'vue-prope
import { MoveName, ended, move as engineMove } from 'container-engine';
import type { GameState, Move } from 'container-engine';
import { EventEmitter } from 'events';
import { groupBy } from 'lodash';
import { groupBy, isEqual } from 'lodash';
import { ContainerState, DropZoneType, Piece, PieceType, ShipType, UIData, Preferences } from '../types/ui-data';
import { ContainerColor, ShipPosition } from 'container-engine/src/gamestate';
import { Container, Factory, Warehouse, Ship, LoanCard, Piece as PieceComponent } from './pieces';
@ -651,11 +651,40 @@ export default class Game extends Vue {
// progress, echoed back by the server — keep the buffer for those.
this.committedState = JSON.parse(JSON.stringify(state));
this.turnMoves = [];
} else if (state && !this.matchesTurnBuffer(state)) {
// Stale tentative echo: server responses can arrive after the buffer has
// changed (a move was undone — possibly down to an empty buffer, which
// re-emits nothing — or another move was made before the echo landed).
// Applying it would transiently show a phantom or regressed move; the echo
// for the current buffer (if any) will follow, so just drop this one.
return;
}
this.replaceState(state, false);
}
/**
* A tentative state is a valid preview only if it is the server's replay of
* exactly the current turn buffer: the visible log must extend the committed log
* by precisely the buffered moves, in order. (All tentative moves are visible-log
* moves — bids and bid-phase loans go to the hidden log but commit immediately.)
*/
matchesTurnBuffer(state: GameState): boolean {
if (!this.committedState) {
return false;
}
const committedLength = this.committedState.log.length;
if (state.log.length !== committedLength + this.turnMoves.length) {
return false;
}
return state.log
.slice(committedLength)
.every((item, i) => item.type === 'move' && isEqual(item.move, this.turnMoves[i]));
}
replaceState(state: GameState, fake: boolean) {
this.G = JSON.parse(JSON.stringify(state));