fix(offline): keep the local game's composition across a state reload

A local (offline) game persisted no draft: draftGet returned an empty
string and draftSave was a no-op, on the assumption that the arrangement
is only ever rebuilt from the rack when the game is reopened. The game
screen, however, refetches the state on several triggers, and every one
of them applied that empty draft over the live composition — dropping the
player's pending tiles back into the rack and resetting the rack order.

Offline the most visible trigger fires on its own: with no network the
live-stream watchdog declares the stream dead after 25 s, the reconnect
4 s later flips app.streamAlive back to true, and the game screen's
reconnect effect refetches. Composing a move offline was therefore reset
roughly every half minute.

Store the draft in the game's own record instead, so a reload restores it
and it survives leaving and reopening the app, and clear it wherever the
turn advances (a committed move, a resignation, a host skip) so a hotseat
seat never inherits the previous player's arrangement. Gate the two
stream-driven refetches on the game being a network one: a local game
takes its events from the source, so reacting to the stream only cost it
a pointless reload.
This commit is contained in:
Ilia Denisov
2026-07-27 18:12:09 +02:00
parent 6ec557d33f
commit ebd1f05da8
5 changed files with 52 additions and 13 deletions
+15 -6
View File
@@ -195,6 +195,7 @@ export class LocalSource implements GameLoopSource {
const seat = entry.game.currentPlayer;
const move = entry.game.resign();
entry.unlockedSeat = null;
entry.record.draft = undefined; // the turn is over; the abandoned composition goes with it
await this.persist(entry);
return this.moveResult(entry, move, seat);
}
@@ -251,6 +252,7 @@ export class LocalSource implements GameLoopSource {
if (action === 'skip') entry.game.pass();
else entry.game.resignSeat(targetSeat ?? entry.game.currentPlayer);
entry.unlockedSeat = null;
entry.record.draft = undefined; // the host advanced the turn; the seat's composition is stale
await this.persist(entry);
return this.stateView(entry);
}
@@ -282,13 +284,18 @@ export class LocalSource implements GameLoopSource {
return { word, legal: game.dictionaryHas(idx) };
}
// Drafts (the in-progress composition) are not persisted for local games — the arrangement is
// rebuilt from the rack on reopen. The screen tolerates an empty draft.
async draftGet(): Promise<string> {
return '';
// Drafts (the in-progress composition: rack order + pending board tiles) live in the game's own
// record — a local game has no server to hold them. Persisting them keeps every reload of the
// state (the game screen refetches on several triggers) from dropping the arrangement back into
// the rack, and carries it across leaving and reopening the app.
async draftGet(gameId: string): Promise<string> {
const entry = await this.load(gameId);
return entry.record.draft ?? '';
}
async draftSave(): Promise<void> {
/* no-op offline */
async draftSave(gameId: string, json: string): Promise<void> {
const entry = await this.load(gameId);
entry.record.draft = json || undefined;
await this.persist(entry);
}
/** events subscribes to a local game's push events (the robot's reply, game over). Returns an
@@ -325,6 +332,7 @@ export class LocalSource implements GameLoopSource {
const seat = entry.game.currentPlayer;
const move = act(entry.game);
entry.unlockedSeat = null; // the turn has advanced; a hotseat seat re-locks (no effect for vs_ai)
entry.record.draft = undefined; // the composition was committed (or swapped away) with the turn
const robotMoves = this.runRobots(entry);
await this.persist(entry);
const result = this.moveResult(entry, move, seat);
@@ -378,6 +386,7 @@ export class LocalSource implements GameLoopSource {
createdAtUnix: entry.record.createdAtUnix,
updatedAtUnix: nowUnix(),
hintUnlockAtMs: entry.record.hintUnlockAtMs,
draft: entry.record.draft,
});
await saveLocalGame(entry.record);
}