2d1fadb50c
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 20s
CI / ui (pull_request) Successful in 1m12s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m54s
Land the offline-model redesign (ANDROID_PLAN.md O1-O7): replace the explicit offline toggle with a single detected net-state machine, unify the lobby, and add a two-tier client-version gate. Contour-safe: both version vars empty => dormant, the wire change is additive, so web/pwa/vk/tg behaviour is unchanged unless the gate is deliberately configured. O1 net-state reducer (test-first). O2 store + wiring (connection/offline shims; +@capacitor/network). O3 remove the offline toggle + migrate the pref. O4 two-tier gate: hard update_required degrades to an offline Update/Play-offline notice (not terminal); soft GATEWAY_RECOMMENDED_CLIENT_VERSION -> X-Update-Recommended nudge. O5 unified lobby (device-local + greyed-from-cache server games; self-set identity; closes G-step-0). O6 create flows (with-friends online/offline segment + offline dict guard). O7 docs. Telegram/VK stay online-only (contour review): the offline model is channel-gated via offlineCapable() (native + plain web; false in the mini-apps). offlineMode.active is hard-gated on it, so the whole model (blue chrome, unified/greyed lobby, transport kill switch, device-local vs_ai/hotseat create) stays inert in Telegram/VK regardless of the detected net state (closing a version-lock path that could have flipped them offline); and New Game's with-friends hides the online/offline segment there, leaving the remote invite alone. ARCHITECTURE already declared "Telegram/VK are exempt" - this makes the code match. Deploy/CI: wire the version gate through the deploy (GATEWAY_MIN_CLIENT_VERSION + GATEWAY_RECOMMENDED_CLIENT_VERSION as plain unprefixed vars via compose + ci.yaml + prod-deploy.yaml + write-prod-env.sh + .env.example) so the gate is live when set (test-contour commit-hash version fails open; empty => dormant). Disable the manual android-build CI workflow for now (rename .disabled). Bump the app bundle budget 127->130 KB for the added always-loaded wiring. Fix the UI Docker stage (gateway/Dockerfile): install --ignore-scripts so the Alpine/musl SPA build no longer tries to native-build sharp (a local android:assets tool, unused in the image); esbuild's binary is an optional dep so Vite still builds. Tests: gateway go green (two-tier gate over a real HTTP handler); docker build of the landing + gateway targets green locally; compose --no-interpolate confirms the gateway env; two new telegram.spec.ts e2e (offline signal keeps the chrome online + quick-match opponent choice visible => server enqueue; with-friends shows no pass-and-play), RED-verified; svelte-check 0/0, vitest 617, e2e 248 (chromium + webkit), build + bundle-size 127.8/130.
105 lines
5.2 KiB
TypeScript
105 lines
5.2 KiB
TypeScript
// Pure grouping + ordering of the lobby's game list. The lobby shows three
|
|
// sections — games awaiting the caller's move, games awaiting the opponent, and finished
|
|
// games — each ordered by last activity: your-turn oldest-first (the longest-neglected on
|
|
// top), the other two newest-first.
|
|
|
|
import type { GameView, Seat } from './model';
|
|
|
|
/** isMyTurn reports whether an active game's seat-to-move belongs to the caller. myIds carries every id
|
|
* that is "you" — the server user id and the device-local guest id — since after reconciliation a
|
|
* device-local game seats you under localGuestId while a server game seats you under the user id (the
|
|
* ids never collide). */
|
|
export function isMyTurn(game: GameView, myIds: readonly string[]): boolean {
|
|
const me = game.seats.find((s) => myIds.includes(s.accountId));
|
|
// 'open' (an auto-match game still awaiting an opponent) is in progress like 'active'.
|
|
return (game.status === 'active' || game.status === 'open') && !!me && game.toMove === me.seat;
|
|
}
|
|
|
|
/**
|
|
* scoreStanding colours a single seat's score on an in-progress game, viewed by myId. The
|
|
* viewer's own seat is 'win' (green) when it leads or ties for the lead and 'lose' (red) when it
|
|
* trails; any other seat is 'win' only when it ties the viewer for the lead, so an equal score
|
|
* paints both numbers green — otherwise an opponent's number keeps the muted default. It returns
|
|
* null (every number muted) for a game that is not active (open or finished — finished games show
|
|
* the result emoji instead), for a fresh 0:0 board where nobody has scored yet, and when myId is
|
|
* not seated against an opponent.
|
|
*/
|
|
export function scoreStanding(game: GameView, myIds: readonly string[], seat: Seat): 'win' | 'lose' | null {
|
|
if (game.status !== 'active') return null;
|
|
const me = game.seats.find((s) => myIds.includes(s.accountId));
|
|
if (!me) return null;
|
|
const others = game.seats.filter((s) => s.accountId && !myIds.includes(s.accountId)).map((s) => s.score);
|
|
if (!others.length) return null;
|
|
const top = Math.max(me.score, ...others);
|
|
if (top === 0) return null; // nobody has scored yet: leave the 0:0 board muted, like a finished game
|
|
const meLeads = me.score === top;
|
|
if (myIds.includes(seat.accountId)) return meLeads ? 'win' : 'lose';
|
|
// Another seat greens only when it ties the leading viewer at the top — the equal-score case.
|
|
return meLeads && seat.score === top ? 'win' : null;
|
|
}
|
|
|
|
/**
|
|
* orderedSeats returns a game's seats in seat-number order (matching the over-the-board
|
|
* scoreboard), without mutating the source array.
|
|
*/
|
|
export function orderedSeats(game: GameView): GameView['seats'] {
|
|
return [...game.seats].sort((a, b) => a.seat - b.seat);
|
|
}
|
|
|
|
/** LobbyPhase is a game's lobby bucket — which of the three card sections it currently sits in. */
|
|
export type LobbyPhase = 'mine' | 'theirs' | 'finished';
|
|
|
|
/**
|
|
* gamePhase reports a game's lobby bucket for myIds: 'finished' for any non-active/open status,
|
|
* else 'mine' when it is the caller's turn, else 'theirs'. It mirrors groupGames' partition and
|
|
* drives the per-card blink, which fires when a card transitions into 'mine' or 'finished'.
|
|
*/
|
|
export function gamePhase(game: GameView, myIds: readonly string[]): LobbyPhase {
|
|
if (game.status !== 'active' && game.status !== 'open') return 'finished';
|
|
return isMyTurn(game, myIds) ? 'mine' : 'theirs';
|
|
}
|
|
|
|
/**
|
|
* shouldBlink reports whether a card's bucket change should fire the attention blink: only a
|
|
* transition into 'mine' (it became your turn) or 'finished' (the game ended) does — an
|
|
* opponent's-turn change is in-place. A first observation (prev === undefined) never blinks, so a
|
|
* cold render and games arriving from another screen stay quiet.
|
|
*/
|
|
export function shouldBlink(prev: LobbyPhase | undefined, next: LobbyPhase): boolean {
|
|
return prev !== undefined && prev !== next && (next === 'mine' || next === 'finished');
|
|
}
|
|
|
|
/** LobbyGroups holds the three ordered lobby sections. */
|
|
export interface LobbyGroups {
|
|
yourTurn: GameView[];
|
|
theirTurn: GameView[];
|
|
finished: GameView[];
|
|
}
|
|
|
|
/**
|
|
* groupGames partitions games for myId into the three lobby sections and orders each. In the two
|
|
* active sections a game with an unread notification (unread[g.id], any chat entry or nudge) sorts
|
|
* first, then by last activity: your-turn ascending (the longest-waiting first), opponent-turn
|
|
* descending (the most recent first). Finished games ignore unread and stay descending by activity.
|
|
*/
|
|
export function groupGames(
|
|
games: GameView[],
|
|
myIds: readonly string[],
|
|
unread: Record<string, boolean>,
|
|
): LobbyGroups {
|
|
const yourTurn: GameView[] = [];
|
|
const theirTurn: GameView[] = [];
|
|
const finished: GameView[] = [];
|
|
for (const g of games) {
|
|
if (g.status !== 'active' && g.status !== 'open') finished.push(g);
|
|
else if (isMyTurn(g, myIds)) yourTurn.push(g);
|
|
else theirTurn.push(g);
|
|
}
|
|
// Unread is the primary key in the active sections (rank 1 before rank 0), last activity the tie-break.
|
|
const rank = (g: GameView) => (unread[g.id] ? 1 : 0);
|
|
yourTurn.sort((a, b) => rank(b) - rank(a) || a.lastActivityUnix - b.lastActivityUnix);
|
|
theirTurn.sort((a, b) => rank(b) - rank(a) || b.lastActivityUnix - a.lastActivityUnix);
|
|
finished.sort((a, b) => b.lastActivityUnix - a.lastActivityUnix);
|
|
return { yourTurn, theirTurn, finished };
|
|
}
|