Files
scrabble-game/ui/src/lib/chatlimit.ts
T
Ilia Denisov 02681ae9e0
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 12s
CI / ui (pull_request) Successful in 45s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
feat(chat): limit in-game chat to one message per turn
Enforce one chat message per turn on both ends. The backend rejects a
second message in the same turn (ErrChatAlreadySentThisTurn -> 409
chat_already_sent), keyed on the move-driven turn start (turn_started_at).
The UI derives "already wrote this turn" from the message list against
GameView.lastActivityUnix (no counter, survives reopening, resets on turn
change), hides the field behind a short caption once the limit is reached,
and now reloads the game state on turn/game-state events so the toggle and
the limit follow the live game. Enter is gated on !busy to avoid a
double-send in the in-flight window.

Backend: new game.TurnStartedAt; social GameReader gains it; PostMessage
enforces the limit reusing lastMessageAt. UI: new lib/chatlimit.ts pure
logic + unit tests; Chat/ChatScreen wiring; chat.sentThisTurn and
error.chat_already_sent i18n (en/ru); extended chat e2e. Docs: FUNCTIONAL
(+ru), ARCHITECTURE, UI_DESIGN.
2026-06-14 20:18:58 +02:00

38 lines
1.6 KiB
TypeScript

import type { ChatMessage } from './model';
// The in-game chat is a per-turn reaction, not a running conversation: a player may post
// at most one message on their own turn and none on the opponent's. The turn boundary is
// the move-driven turn start (GameView.lastActivityUnix), so "already wrote this turn" is
// derived from the loaded message list rather than a counter — it survives reopening the
// chat and resets by itself when the turn advances. The backend enforces the same rule.
/**
* turnMessageLimit is how many chat messages a player may post in the given turn state:
* one on the player's own turn, none on the opponent's.
*/
export function turnMessageLimit(isMyTurn: boolean): number {
return isMyTurn ? 1 : 0;
}
/**
* sentThisTurn counts the chat messages (nudges excluded) that the viewer myId has posted
* since the current turn began. turnStartUnix is the move-driven turn start in Unix
* seconds; a message counts toward the current turn when it was created at or after it.
*/
export function sentThisTurn(messages: ChatMessage[], myId: string, turnStartUnix: number): number {
let n = 0;
for (const m of messages) {
if (m.senderId === myId && m.kind === 'message' && m.createdAtUnix >= turnStartUnix) n += 1;
}
return n;
}
/**
* canSendChat reports whether the viewer may still post a chat message this turn: the count
* already sent is below the turn's limit. It is false on the opponent's turn (limit zero)
* and once the single own-turn message has been sent.
*/
export function canSendChat(isMyTurn: boolean, sent: number): boolean {
return sent < turnMessageLimit(isMyTurn);
}