feat(chat): limit in-game chat to one message per turn
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

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.
This commit is contained in:
Ilia Denisov
2026-06-14 20:18:58 +02:00
parent fc28e43e43
commit 02681ae9e0
18 changed files with 207 additions and 15 deletions
+16 -6
View File
@@ -8,6 +8,7 @@
myId,
busy,
myTurn = false,
canSend = false,
waiting = false,
nudgeOnCooldown = false,
onsend,
@@ -16,11 +17,16 @@
messages: ChatMessage[];
myId: string;
busy: boolean;
// Chat and nudge are mutually exclusive by turn: on the player's own turn the
// message field + send are shown (and nudging makes no sense — there is no one to
// hurry); on the opponent's turn only the nudge button shows. While the hourly nudge
// cooldown is active the nudge is disabled with an "awaiting reply" caption.
// The input area has three turn-driven states: on your own turn the message field +
// send show while you may still post (canSend); once the one allowed message is sent a
// short caption replaces them until the next turn; on the opponent's turn only the
// nudge button shows (nudging your own turn makes no sense — there is no one to hurry).
// While the hourly nudge cooldown is active the nudge is disabled with an "awaiting
// reply" caption.
myTurn?: boolean;
// canSend is true while the player may still post this turn (own turn and the per-turn
// limit not yet reached); it gates the message field.
canSend?: boolean;
// waiting is true while an auto-match game still has no opponent: both send and nudge
// are disabled (there is no one to message or hurry yet).
waiting?: boolean;
@@ -53,14 +59,18 @@
{/each}
</div>
<div class="input">
{#if myTurn}
{#if canSend}
<input
maxlength="60"
placeholder={t('chat.placeholder')}
bind:value={text}
onkeydown={(e) => e.key === 'Enter' && send()}
onkeydown={(e) => e.key === 'Enter' && !busy && send()}
/>
<button class="iconbtn" onclick={send} disabled={busy || waiting || !connection.online} aria-label={t('chat.send')}>⬆️</button>
{:else if myTurn}
<!-- Own turn, the one allowed message already sent: a caption holds the row until the
next turn (no nudge — there is no one to hurry on your own turn). -->
<span class="cooldown">{t('chat.sentThisTurn')}</span>
{:else}
<!-- A flex:1 caption keeps the nudge pinned right whether or not the cooldown text shows. -->
<span class="cooldown">{nudgeOnCooldown ? t('chat.awaitingReply') : ''}</span>
+23 -4
View File
@@ -3,6 +3,7 @@
import Chat from './Chat.svelte';
import { gateway } from '../lib/gateway';
import { app, handleError, clearChatUnread } from '../lib/app.svelte';
import { canSendChat, sentThisTurn } from '../lib/chatlimit';
import type { ChatMessage, StateView } from '../lib/model';
// The Chat tab body, hosted by CommsHub (which supplies the nav bar + tab bar). The
@@ -20,6 +21,11 @@
const isMyTurn = $derived(
!!view && (view.game.status === 'active' || view.game.status === 'open') && view.game.toMove === view.seat,
);
// At most one chat message per turn: "already wrote this turn" is derived from the
// message list against the move-driven turn start (lastActivityUnix), so it resets when
// the turn advances. canSend hides the field once the limit is reached (and always on
// the opponent's turn).
const canSend = $derived(canSendChat(isMyTurn, view ? sentThisTurn(messages, myId, view.game.lastActivityUnix) : 0));
// While the auto-match game still has no opponent, chat and nudge are both disabled.
const waiting = $derived(!!view && view.game.status === 'open');
const nudgeCooldownSecs = 3600;
@@ -46,22 +52,35 @@
/* best-effort */
}
}
onMount(async () => {
// loadState (re)reads the game state that drives the chat/nudge toggle and the per-turn
// limit. The initial load surfaces an error; live reloads are best-effort.
async function loadState(initial = false) {
try {
view = await gateway.gameState(id, false);
} catch (e) {
handleError(e);
if (initial) handleError(e);
}
}
onMount(async () => {
await loadState(true);
await refresh();
});
// Live: refresh (and keep the unread cleared) on a chat / nudge for this game.
// Live: refresh the message list on a chat / nudge for this game, and reload the state on
// a turn / game-state change so the chat/nudge toggle and the per-turn limit (keyed on the
// turn start) follow the live game.
$effect(() => {
const e = app.lastEvent;
if (!e) return;
if ((e.kind === 'chat_message' && e.message.gameId === id) || (e.kind === 'nudge' && e.gameId === id)) {
void refresh();
}
if (
(e.kind === 'your_turn' || e.kind === 'opponent_moved' || e.kind === 'game_over' || e.kind === 'opponent_joined') &&
e.gameId === id
) {
void loadState();
}
});
// Re-evaluate the nudge cooldown on a timer so the control re-enables on time.
$effect(() => {
@@ -91,4 +110,4 @@
}
</script>
<Chat {messages} {myId} {busy} myTurn={isMyTurn} {waiting} {nudgeOnCooldown} onsend={sendChat} onnudge={nudge} />
<Chat {messages} {myId} {busy} myTurn={isMyTurn} {canSend} {waiting} {nudgeOnCooldown} onsend={sendChat} onnudge={nudge} />
+41
View File
@@ -0,0 +1,41 @@
import { describe, expect, it } from 'vitest';
import { canSendChat, sentThisTurn, turnMessageLimit } from './chatlimit';
import type { ChatMessage } from './model';
function msg(senderId: string, kind: string, createdAtUnix: number): ChatMessage {
return { id: `${senderId}-${kind}-${createdAtUnix}`, gameId: 'g1', senderId, kind, body: kind === 'nudge' ? '' : 'hi', createdAtUnix };
}
const ME = 'me';
const TURN_START = 1000;
describe('turnMessageLimit', () => {
it('allows one message on your turn and none on the opponent\'s', () => {
expect(turnMessageLimit(true)).toBe(1);
expect(turnMessageLimit(false)).toBe(0);
});
});
describe('sentThisTurn', () => {
it('counts my messages posted at or after the turn start', () => {
const messages = [msg(ME, 'message', TURN_START), msg(ME, 'message', TURN_START + 5)];
expect(sentThisTurn(messages, ME, TURN_START)).toBe(2);
});
it('ignores my messages from a previous turn', () => {
expect(sentThisTurn([msg(ME, 'message', TURN_START - 1)], ME, TURN_START)).toBe(0);
});
it("ignores other players' messages and my own nudges", () => {
const messages = [msg('ann', 'message', TURN_START + 1), msg(ME, 'nudge', TURN_START + 2)];
expect(sentThisTurn(messages, ME, TURN_START)).toBe(0);
});
});
describe('canSendChat', () => {
it("is false on the opponent's turn", () => {
expect(canSendChat(false, 0)).toBe(false);
});
it('is true on your turn before sending and false after one message', () => {
expect(canSendChat(true, 0)).toBe(true);
expect(canSendChat(true, 1)).toBe(false);
});
});
+37
View File
@@ -0,0 +1,37 @@
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);
}
+2
View File
@@ -113,6 +113,7 @@ export const en = {
'chat.awaitingReply': "Waiting for the opponent's reply",
'chat.empty': 'No messages yet.',
'chat.nudged': '{name} nudged you',
'chat.sentThisTurn': 'You can write again next turn.',
'profile.title': 'Profile',
'profile.language': 'Language',
@@ -177,6 +178,7 @@ export const en = {
'error.chat_rejected': 'Message rejected (too long or contains contact info).',
'error.nudge_too_soon': "Please don't rush your opponent so often.",
'error.chat_not_your_turn': 'You can chat only on your turn.',
'error.chat_already_sent': 'You can send only one message per turn.',
'error.game_finished': 'This game is finished.',
'error.not_a_player': 'You are not a player in this game.',
'error.already_queued': 'You are already in the queue.',
+2
View File
@@ -114,6 +114,7 @@ export const ru: Record<MessageKey, string> = {
'chat.awaitingReply': 'Ждём реакцию соперника',
'chat.empty': 'Сообщений пока нет.',
'chat.nudged': '{name} торопит вас',
'chat.sentThisTurn': 'Можно написать снова в следующем ходу.',
'profile.title': 'Профиль',
'profile.language': 'Язык',
@@ -178,6 +179,7 @@ export const ru: Record<MessageKey, string> = {
'error.chat_rejected': 'Сообщение отклонено (слишком длинное или содержит контакты).',
'error.nudge_too_soon': 'Не стоит торопить соперника так часто.',
'error.chat_not_your_turn': 'Писать в чат можно только в свой ход.',
'error.chat_already_sent': 'За один ход можно отправить только одно сообщение.',
'error.game_finished': 'Эта игра уже завершена.',
'error.not_a_player': 'Вы не участник этой игры.',
'error.already_queued': 'Вы уже в очереди.',