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
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:
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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.',
|
||||
|
||||
@@ -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': 'Вы уже в очереди.',
|
||||
|
||||
Reference in New Issue
Block a user