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); }