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:
@@ -408,6 +408,12 @@ func (svc *Service) LastMoveAt(ctx context.Context, gameID, accountID uuid.UUID)
|
||||
return svc.store.LastMoveAt(ctx, gameID, accountID)
|
||||
}
|
||||
|
||||
// TurnStartedAt returns the start time of a game's current turn. The social service uses
|
||||
// it as the per-turn boundary for the one-chat-message-per-turn limit.
|
||||
func (svc *Service) TurnStartedAt(ctx context.Context, gameID uuid.UUID) (time.Time, error) {
|
||||
return svc.store.TurnStartedAt(ctx, gameID)
|
||||
}
|
||||
|
||||
// transition validates the actor and turn, applies op under the per-game lock and
|
||||
// commits the result.
|
||||
func (svc *Service) transition(ctx context.Context, gameID, accountID uuid.UUID, op engineOp) (MoveResult, error) {
|
||||
|
||||
@@ -968,6 +968,19 @@ func (s *Store) LastMoveAt(ctx context.Context, gameID, accountID uuid.UUID) (ti
|
||||
return at.Time, true, nil
|
||||
}
|
||||
|
||||
// TurnStartedAt returns the start time of a game's current turn (games.turn_started_at).
|
||||
// The social service uses it as the per-turn boundary for the one-chat-message-per-turn
|
||||
// limit; it advances only on a move, never on chat or a nudge.
|
||||
func (s *Store) TurnStartedAt(ctx context.Context, gameID uuid.UUID) (time.Time, error) {
|
||||
var at time.Time
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT turn_started_at FROM backend.games WHERE game_id = $1`, gameID).Scan(&at)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("game: turn started at %s: %w", gameID, err)
|
||||
}
|
||||
return at, nil
|
||||
}
|
||||
|
||||
// RobotSchedule returns a game's bag seed and current turn-start time. The admin console
|
||||
// combines them with the robot strategy to show a robot seat's play-to-win intent and its
|
||||
// next-move ETA. Both are server-only state, never part of the public game view.
|
||||
|
||||
@@ -333,6 +333,29 @@ func TestChatOnlyOnYourTurn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatOnePerTurn checks the one-message-per-turn limit: a second message on the same
|
||||
// turn is refused, and a fresh turn (turn_started_at advancing past the sent message)
|
||||
// reopens chat for the player.
|
||||
func TestChatOnePerTurn(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
gameID, seats := newGameWithSeats(t, 2) // seat 0 is to move at the opening
|
||||
if _, err := svc.PostMessage(ctx, gameID, seats[0], "first", ""); err != nil {
|
||||
t.Fatalf("first message: %v", err)
|
||||
}
|
||||
if _, err := svc.PostMessage(ctx, gameID, seats[0], "second", ""); !errors.Is(err, social.ErrChatAlreadySentThisTurn) {
|
||||
t.Fatalf("second message = %v, want ErrChatAlreadySentThisTurn", err)
|
||||
}
|
||||
// Advancing the turn start past the sent message (as a real move does) reopens chat.
|
||||
if _, err := testDB.ExecContext(ctx,
|
||||
`UPDATE backend.games SET turn_started_at = now() WHERE game_id = $1`, gameID); err != nil {
|
||||
t.Fatalf("advance turn: %v", err)
|
||||
}
|
||||
if _, err := svc.PostMessage(ctx, gameID, seats[0], "next turn", ""); err != nil {
|
||||
t.Fatalf("message on the new turn: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNudgeRulesAndRateLimit(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
svc := newSocialService()
|
||||
|
||||
@@ -25,6 +25,7 @@ func TestStatusForError(t *testing.T) {
|
||||
"nudge own turn": {social.ErrNudgeOnOwnTurn, http.StatusConflict, "nudge_own_turn"},
|
||||
"nudge too soon": {social.ErrNudgeTooSoon, http.StatusConflict, "nudge_too_soon"},
|
||||
"chat off turn": {social.ErrChatNotYourTurn, http.StatusConflict, "chat_not_your_turn"},
|
||||
"chat one/turn": {social.ErrChatAlreadySentThisTurn, http.StatusConflict, "chat_already_sent"},
|
||||
"illegal play": {engine.ErrIllegalPlay, http.StatusUnprocessableEntity, "illegal_play"},
|
||||
"email taken": {account.ErrEmailTaken, http.StatusConflict, "email_taken"},
|
||||
"code mismatch": {account.ErrCodeMismatch, http.StatusUnauthorized, "code_invalid"},
|
||||
|
||||
@@ -213,6 +213,8 @@ func statusForError(err error) (int, string) {
|
||||
return http.StatusConflict, "nudge_too_soon"
|
||||
case errors.Is(err, social.ErrChatNotYourTurn):
|
||||
return http.StatusConflict, "chat_not_your_turn"
|
||||
case errors.Is(err, social.ErrChatAlreadySentThisTurn):
|
||||
return http.StatusConflict, "chat_already_sent"
|
||||
case errors.Is(err, social.ErrSelfRelation):
|
||||
return http.StatusBadRequest, "self_relation"
|
||||
case errors.Is(err, social.ErrRequestExists):
|
||||
|
||||
@@ -65,6 +65,18 @@ func (svc *Service) PostMessage(ctx context.Context, gameID, senderID uuid.UUID,
|
||||
if idx != toMove {
|
||||
return Message{}, ErrChatNotYourTurn
|
||||
}
|
||||
// At most one chat message per turn. The turn's start (move-driven, so stable for the
|
||||
// whole turn) bounds the window: a prior message posted at or after it closes chat until
|
||||
// the next turn.
|
||||
turnStart, err := svc.games.TurnStartedAt(ctx, gameID)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if last, ok, err := svc.store.lastMessageAt(ctx, gameID, senderID); err != nil {
|
||||
return Message{}, err
|
||||
} else if ok && !last.Before(turnStart) {
|
||||
return Message{}, ErrChatAlreadySentThisTurn
|
||||
}
|
||||
sender, err := svc.accounts.GetByID(ctx, senderID)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
|
||||
@@ -31,6 +31,10 @@ type GameReader interface {
|
||||
// LastMoveAt is the time of an account's most recent move in a game (and whether it
|
||||
// has moved); the nudge cooldown resets once the player has taken a turn.
|
||||
LastMoveAt(ctx context.Context, gameID, accountID uuid.UUID) (time.Time, bool, error)
|
||||
// TurnStartedAt is the start time of the game's current turn; it bounds the
|
||||
// one-chat-message-per-turn limit (a message counts toward the current turn when it
|
||||
// was posted at or after this time).
|
||||
TurnStartedAt(ctx context.Context, gameID uuid.UUID) (time.Time, error)
|
||||
// GameLanguage is the game's language tag ("en"/"ru"), so a nudge's out-of-app push routes
|
||||
// to the game's bot rather than the recipient's last-login bot.
|
||||
GameLanguage(ctx context.Context, gameID uuid.UUID) (string, error)
|
||||
@@ -77,6 +81,9 @@ var (
|
||||
// sender's turn — chat is allowed only on your own turn (the opponent's-turn control
|
||||
// is the nudge).
|
||||
ErrChatNotYourTurn = errors.New("social: cannot chat while it is not your turn")
|
||||
// ErrChatAlreadySentThisTurn is returned when the sender has already posted a chat
|
||||
// message during the current turn — at most one message is allowed per turn.
|
||||
ErrChatAlreadySentThisTurn = errors.New("social: already sent a chat message this turn")
|
||||
)
|
||||
|
||||
// Service is the social domain. It is the only writer of the friendships, blocks
|
||||
|
||||
Reference in New Issue
Block a user