R4: push enrichment — events carry a state delta, kill the last poll
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 37s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 37s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
Enrich the in-app live stream into a delta channel so the UI renders a move from the event without a follow-up game.state, and make the matchmaking poll a stream-down fallback. - pkg/fbs: trailing fields on opponent_moved (move+game+bag_len), your_turn (move_count), match_found (state), game_over (game), notify (account/invitation/state), MoveResult (rack+bag_len); regenerate Go + TS. - backend: notify owns the FB encoding (encode.go + payload.go input structs); game/lobby/social map their domain types in. emitMove builds the move delta; game.Service.InitialState feeds match_found/game_started the recipient's initial StateView; friends/invitations notify carry their account/invitation. The move-commit response (submit_play/pass/exchange/resign) returns the actor's refilled rack + bag size. - gateway: MoveResult transcode carries rack+bag_len. - ui: pure lib/gamedelta.ts reducer advances the per-game cache keyed on move_count (idempotent + gap-safe); app.svelte seeds the cache on match_found/game_started; Game.svelte applies the delta (commit/pass/exchange/resign drop their load()); NewGame polls only while app.streamAlive is false. - docs: ARCHITECTURE §10, FUNCTIONAL(+ru), backend/gateway/ui READMEs; PRERELEASE R4 marked done + Refinements.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Pure reducers that advance the per-game cache from live events (R4), so the UI renders a move
|
||||
// from the event without a follow-up game.state + game.history fetch. They never touch the network
|
||||
// or the cache store — the stream handler applies the returned cache and the game screen acts on
|
||||
// `refetch` — which keeps the gap / own-move / idempotency logic unit-testable in isolation.
|
||||
|
||||
import type { CachedGame } from './gamecache';
|
||||
import type { GameView, MoveRecord, StateView } from './model';
|
||||
|
||||
/** The fields an opponent_moved delta carries that advance a cached game. */
|
||||
export interface MoveDelta {
|
||||
move?: MoveRecord;
|
||||
game?: GameView;
|
||||
bagLen: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* DeltaResult is the outcome of applying an event: the advanced cache (set only when it changed)
|
||||
* and whether the caller must fall back to a full game.state + game.history fetch — a gap, a
|
||||
* missing payload, or the actor's own move on a device that has not drawn its new rack yet.
|
||||
*/
|
||||
export interface DeltaResult {
|
||||
cache?: CachedGame;
|
||||
refetch: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* seedInitialState builds a fresh cache entry from a started game's initial view (match_found /
|
||||
* game_started). A freshly started game has no moves, so the board replays from an empty journal.
|
||||
*/
|
||||
export function seedInitialState(state: StateView): CachedGame {
|
||||
return { view: state, moves: [] };
|
||||
}
|
||||
|
||||
/**
|
||||
* applyMoveDelta advances cached by one move from an opponent_moved delta, keyed on the post-move
|
||||
* count so it is idempotent (a re-delivered move, or the echo of one's own move, is a no-op) and
|
||||
* gap-safe (a missed move asks for a refetch). An opponent's move leaves the recipient's rack
|
||||
* unchanged, so it is preserved; the actor's own move drew a new rack the delta does not carry, so
|
||||
* a device still behind on its own move refetches to pick it up.
|
||||
*/
|
||||
export function applyMoveDelta(cached: CachedGame | undefined, d: MoveDelta): DeltaResult {
|
||||
// Nothing cached to advance (the game was never opened on this device): ignore it; the next open
|
||||
// cold-loads the game.
|
||||
if (!cached) return { refetch: false };
|
||||
// A pre-R4 peer, or a dropped payload, carried no delta: the open game must refetch.
|
||||
if (!d.move || !d.game) return { refetch: true };
|
||||
const have = cached.view.game.moveCount;
|
||||
const next = d.game.moveCount;
|
||||
if (next <= have) return { refetch: false }; // already applied (idempotent / own echo)
|
||||
if (next > have + 1) return { refetch: true }; // a gap — an intermediate move was missed
|
||||
// The actor's own move changed their rack (a draw), which opponent_moved does not carry.
|
||||
if (d.move.player === cached.view.seat) return { refetch: true };
|
||||
const view: StateView = { ...cached.view, game: d.game, bagLen: d.bagLen };
|
||||
return { cache: { view, moves: [...cached.moves, d.move] }, refetch: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* applyGameOver settles a finished game from a game_over event's final summary (the adjusted
|
||||
* scores after rack penalties and the winner). It refetches when the cached board is behind the
|
||||
* final move count — the closing move was missed — so history is repaired, and is a harmless
|
||||
* re-settle when the closing opponent_moved already finished the game.
|
||||
*/
|
||||
export function applyGameOver(cached: CachedGame | undefined, game: GameView | undefined): DeltaResult {
|
||||
if (!cached) return { refetch: false };
|
||||
if (!game) return { refetch: cached.view.game.status !== 'finished' };
|
||||
if (cached.view.game.moveCount < game.moveCount) return { refetch: true };
|
||||
const view: StateView = { ...cached.view, game };
|
||||
return { cache: { view, moves: cached.moves }, refetch: false };
|
||||
}
|
||||
Reference in New Issue
Block a user