Files
scrabble-game/backend/internal/social/robotfriends.go
T
Ilia Denisov ed53e25e57
CI / changes (pull_request) Successful in 5s
CI / unit (pull_request) Successful in 11s
CI / integration (pull_request) Successful in 20s
CI / ui (pull_request) Has been skipped
CI / conformance (pull_request) Successful in 11s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 2m41s
feat(backend): per-tier, per-kind active-game limits with a guest funnel
Cap a player's simultaneous unfinished games per kind (vs_ai, random,
friends) with independent guest and durable-account tiers, held in a new
single-row backend.config table (-1 = unlimited) behind an in-memory cache
and editable live in the admin console (/_gm/limits). Each game is tagged
with games.game_kind on creation.

This replaces the earlier flat MaxActiveQuickGames=10 combined cap: the
per-tier/kind config is the single mechanism, enforced at the same handler
gate (ensureUnderGameLimit by kind on lobby/enqueue) plus the durable
friends cap in CreateInvitation. game.Service.AtGameLimit only resolves the
tier and counts; the limit policy stays at the request edge.

Guests are now refused friend requests, friend-code redemption,
befriend-in-game and invitation creation outright (403 guest_forbidden) --
previously only the UI hid these.

Admin: a kind column in both game lists and the config editor.

Defaults: guest 1 vs_ai / 1 random / 0 friends; durable 10 / 10 / 10.
2026-07-10 09:03:57 +02:00

182 lines
7.1 KiB
Go

package social
import (
"context"
"fmt"
"slices"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
)
// robotFriendRequestTTL is how long a per-game disguised-robot friend request is kept
// after its game has finished before the reaper deletes it. It is a sibling of
// friendRequestTTL; the request is housekeeping for the in-game "request sent" state,
// so it is purged once the finished game is well past its lobby lifetime.
const robotFriendRequestTTL = 7 * 24 * time.Hour
// robotFriendRequestReapInterval is how often the reaper sweeps expired rows.
const robotFriendRequestReapInterval = time.Hour
// RobotFriendRequest is one per-game friend request to a disguised-robot opponent: the
// row id, the game name the player saw, and the game + seat it was sent in (so the
// in-game scoreboard can re-mark that seat as already requested).
type RobotFriendRequest struct {
ID uuid.UUID
DisplayName string
GameID uuid.UUID
Seat int
}
// RequestInGame sends a friend request to addresseeID for requesterID from within gameID.
// A human is requested normally (the friendships table, via SendFriendRequest). A
// disguised-robot opponent is instead recorded per-game in robot_friend_requests — never
// the shared robot account — so the matchmaker keeps robots free, the same robot stays
// un-requested in the requester's other games (under its other names), and the in-game
// "request sent" state is pinned to this seat rather than the shared account. The robot
// ignores the request (it never becomes a friendship); the reaper deletes the row once
// the game is long finished. It is the entry point for the in-game add-friend control;
// the friend-code path goes elsewhere.
func (svc *Service) RequestInGame(ctx context.Context, requesterID, addresseeID, gameID uuid.UUID) error {
if requesterID == addresseeID {
return ErrSelfRelation
}
// A guest cannot use friends: a durable-account feature (the UI hides it).
if acc, err := svc.accounts.GetByID(ctx, requesterID); err != nil {
return err
} else if acc.IsGuest {
return ErrGuestForbidden
}
isRobot, err := svc.accounts.IsRobot(ctx, addresseeID)
if err != nil {
return err
}
if !isRobot {
return svc.SendFriendRequest(ctx, requesterID, addresseeID)
}
if gameID == uuid.Nil {
return ErrNotParticipant
}
seats, _, _, err := svc.games.Participants(ctx, gameID)
if err != nil {
return err
}
seat := slices.Index(seats, addresseeID)
if seat < 0 {
return ErrNotParticipant
}
name, _ := svc.games.SeatName(ctx, gameID, addresseeID)
return svc.store.insertRobotFriendRequest(ctx, requesterID, gameID, seat, addresseeID, name)
}
// ListRobotFriendRequests returns requesterID's per-game disguised-robot friend
// requests, newest first.
func (svc *Service) ListRobotFriendRequests(ctx context.Context, requesterID uuid.UUID) ([]RobotFriendRequest, error) {
return svc.store.listRobotFriendRequests(ctx, requesterID)
}
// insertRobotFriendRequest records a per-game robot friend request; a duplicate (same
// requester, game, seat) is ignored.
func (s *Store) insertRobotFriendRequest(ctx context.Context, requester, gameID uuid.UUID, seat int, robotID uuid.UUID, name string) error {
id, err := uuid.NewV7()
if err != nil {
return fmt.Errorf("social: new robot friend request id: %w", err)
}
const q = `INSERT INTO backend.robot_friend_requests (id, requester_id, game_id, seat, robot_id, display_name)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (requester_id, game_id, seat) DO NOTHING`
if _, err := s.db.ExecContext(ctx, q, id, requester, gameID, int16(seat), robotID, name); err != nil {
return fmt.Errorf("social: insert robot friend request: %w", err)
}
return nil
}
// listRobotFriendRequests returns requester's robot friend requests, newest first.
func (s *Store) listRobotFriendRequests(ctx context.Context, requester uuid.UUID) ([]RobotFriendRequest, error) {
const q = `SELECT id, display_name, game_id, seat FROM backend.robot_friend_requests
WHERE requester_id = $1 ORDER BY created_at DESC`
rows, err := s.db.QueryContext(ctx, q, requester)
if err != nil {
return nil, fmt.Errorf("social: list robot friend requests: %w", err)
}
defer rows.Close()
var out []RobotFriendRequest
for rows.Next() {
var r RobotFriendRequest
var seat int16
if err := rows.Scan(&r.ID, &r.DisplayName, &r.GameID, &seat); err != nil {
return nil, fmt.Errorf("social: scan robot friend request: %w", err)
}
r.Seat = int(seat)
out = append(out, r)
}
return out, rows.Err()
}
// ReapExpiredRobotFriendRequests deletes every per-game robot friend request whose game has
// been finished for longer than robotFriendRequestTTL, reporting how many rows were removed.
// Rows for active (not-yet-finished) games are kept so the in-game "request sent" state
// survives. It backs the RobotFriendRequestReaper and is directly callable in tests.
func (svc *Service) ReapExpiredRobotFriendRequests(ctx context.Context) (int64, error) {
return svc.store.deleteExpiredRobotFriendRequests(ctx, svc.now().Add(-robotFriendRequestTTL))
}
// deleteExpiredRobotFriendRequests removes robot friend requests whose game has been
// finished since before cutoff, reporting how many rows were deleted.
func (s *Store) deleteExpiredRobotFriendRequests(ctx context.Context, cutoff time.Time) (int64, error) {
const q = `DELETE FROM backend.robot_friend_requests r
USING backend.games g
WHERE r.game_id = g.game_id AND g.status = 'finished' AND g.finished_at < $1`
res, err := s.db.ExecContext(ctx, q, cutoff)
if err != nil {
return 0, fmt.Errorf("social: delete expired robot friend requests: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("social: delete expired robot friend requests rows: %w", err)
}
return n, nil
}
// RobotFriendRequestReaper periodically reaps expired per-game disguised-robot friend
// requests via Service.ReapExpiredRobotFriendRequests. It mirrors the account.GuestReaper:
// one background goroutine, started once from main.
type RobotFriendRequestReaper struct {
svc *Service
log *zap.Logger
}
// NewRobotFriendRequestReaper constructs a reaper over svc. log may be nil.
func NewRobotFriendRequestReaper(svc *Service, log *zap.Logger) *RobotFriendRequestReaper {
if log == nil {
log = zap.NewNop()
}
return &RobotFriendRequestReaper{svc: svc, log: log}
}
// Interval is the reaper's sweep cadence, for the startup log line.
func (r *RobotFriendRequestReaper) Interval() time.Duration { return robotFriendRequestReapInterval }
// Retention is the reaper's post-finish retention window, for the startup log line.
func (r *RobotFriendRequestReaper) Retention() time.Duration { return robotFriendRequestTTL }
// Run reaps expired robot friend requests on each tick until ctx is cancelled.
func (r *RobotFriendRequestReaper) Run(ctx context.Context) {
ticker := time.NewTicker(robotFriendRequestReapInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
n, err := r.svc.ReapExpiredRobotFriendRequests(ctx)
if err != nil {
r.log.Warn("robot friend request reap failed", zap.Error(err))
} else if n > 0 {
r.log.Info("reaped expired robot friend requests", zap.Int64("count", n))
}
}
}
}