Files
scrabble-game/ui/src/lib/gamelimits.ts
T
Ilia Denisov 3306a016a0
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 12s
CI / integration (pull_request) Successful in 28s
CI / ui (pull_request) Successful in 1m11s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m46s
feat(ui): per-kind active-game limit lock on the New Game screen
Carry the caller's per-tier active-game caps and each game's kind on the
wire (Profile.game_limits + GameView.kind, additive FBS + gateway transcode
+ client codec, committed regen). The New Game screen counts the player's
active games per kind from the lobby and locks a capped start: an outline
button with a lock that opens a funnel modal instead of a game -- a sign-in
prompt for a guest, a "finish a current game first" notice for a signed-in
account (native Telegram popup, in-app modal elsewhere). The lock lifts via
the existing profile refetch after a guest->durable upgrade.

Remove the lobby's old at_game_limit New-Game tab disable + notice: the flag
(now the random-kind cap) conflicted with the per-kind lock -- it hid the
screen where the lock lives and wrongly blocked an unfulfilled kind. The New
Game tab is always enabled; the per-kind start lock is the only gate. The
at_game_limit wire field stays (unused by the client) for a later cleanup.

Tests: client lock logic + codec kind/game_limits roundtrip + gateway
transcode encode + native popup builders (unit); a mock e2e for the lock
badge and the modal.
2026-07-10 10:09:45 +02:00

51 lines
2.0 KiB
TypeScript

// Client-side active-game limit logic: count the caller's active games per kind and test a kind
// against the caller's per-tier cap so the New-Game screen can lock a capped start. The server is
// the source of truth (it refuses a capped create); this only drives the pre-emptive UI lock. Kept
// pure (no DOM / stores) so it unit-tests in the node environment.
import { GameKind, type GameLimits, type GameView } from './model';
/**
* activeByKind counts the caller's active (status open or in-progress) games per kind. Finished
* games, local pass-and-play (hotseat) games and untagged games (kind 0, pre-existing) do not
* count — matching the server's per-kind cap, which is keyed on games.game_kind for open/active
* games only.
*/
export function activeByKind(games: GameView[]): Record<number, number> {
const out: Record<number, number> = {};
for (const g of games) {
if (g.hotseat) continue;
if (g.kind === 0) continue;
if (g.status !== 'active' && g.status !== 'open') continue;
out[g.kind] = (out[g.kind] ?? 0) + 1;
}
return out;
}
/** capFor returns the caller's tier cap for a kind (-1 = unlimited), or -1 when the limits are
* absent (an old backend) — the lock then never applies. */
export function capFor(limits: GameLimits | undefined, kind: number): number {
if (!limits) return -1;
switch (kind) {
case GameKind.vsAi:
return limits.vsAi;
case GameKind.random:
return limits.random;
case GameKind.friends:
return limits.friends;
default:
return -1;
}
}
/**
* isKindLocked reports whether the caller has reached its cap for kind, so the New-Game start for
* that kind is locked. An unlimited cap (-1) or absent limits never lock; a cap of 0 always locks
* (the kind is disabled for the tier).
*/
export function isKindLocked(games: GameView[], limits: GameLimits | undefined, kind: number): boolean {
const cap = capFor(limits, kind);
if (cap < 0) return false;
return (activeByKind(games)[kind] ?? 0) >= cap;
}