Files
scrabble-game/backend/internal/lobby/lobby.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

89 lines
4.6 KiB
Go

// Package lobby forms games: an auto-match maker that drops a player straight into a
// game with an empty opponent seat (or joins them into another player's waiting one),
// and friend-game invitations (invite -> accept) that start a 2-4 player game once
// every invitee has accepted. Both produce games through the game domain; neither
// imports the engine. Auto-match state is the open games in the database, so it
// survives a restart; a background reaper substitutes a pooled robot for any open game
// that waits too long, guaranteeing every game gets an opponent.
package lobby
import (
"context"
"errors"
"github.com/google/uuid"
"scrabble/backend/internal/engine"
"scrabble/backend/internal/game"
"scrabble/backend/internal/gamelimits"
"scrabble/backend/internal/notify"
)
// GameCreator is the slice of the game domain the lobby needs: starting a seated
// game, reading a player's initial view of it, and testing a caller's active-game
// cap. game.Service satisfies it.
type GameCreator interface {
Create(ctx context.Context, params game.CreateParams) (game.Game, error)
// InitialState returns a seated player's full initial view of a started game, used
// to enrich the game_started event so the client renders the new game without a
// follow-up fetch.
InitialState(ctx context.Context, gameID, accountID uuid.UUID) (notify.PlayerState, error)
// AtGameLimit reports whether accountID has reached its tier's active-game cap for kind. The
// invitation path uses it to enforce the friends limit before opening one.
AtGameLimit(ctx context.Context, accountID uuid.UUID, kind gamelimits.Kind) (bool, error)
}
// RobotProvider supplies a robot account to substitute for a missing human in
// auto-match. robot.Service satisfies it; it returns an error when no robot is
// available so the matchmaker can defer substitution.
type RobotProvider interface {
Pick(variant engine.Variant) (uuid.UUID, error)
// PickNamed selects a robot and composes a fresh per-game display name for it, for
// the disguised auto-match path (the name is stamped on the robot's seat).
PickNamed(variant engine.Variant) (uuid.UUID, string, error)
}
// Blocker exposes the per-user block graph the lobby needs. social.Service satisfies it.
// Invitations consult the exact direction (Blocks) so the inviter's block refuses while a
// block the invitee placed on the inviter is silently suppressed; auto-match consults
// BlockedWith so a block either way keeps the pair out of the same anonymous game.
type Blocker interface {
// Blocks reports whether blocker has blocked blocked (exact direction).
Blocks(ctx context.Context, blocker, blocked uuid.UUID) (bool, error)
// BlockedWith returns every account in a block with accountID in either direction.
BlockedWith(ctx context.Context, accountID uuid.UUID) ([]uuid.UUID, error)
}
// Auto-match defaults: a casual two-player game on the longest move clock with one
// hint per player (docs/ARCHITECTURE.md §6). The drop-out tile disposition is moot
// for two players, so the engine default (remove) applies.
const (
autoMatchHintsAllowed = true
autoMatchHintsPerPlayer = 1
)
// Sentinel errors returned by the lobby.
var (
// ErrInvalidInvitation is returned for a malformed invitation (bad player
// count, duplicate or self invitee, or unacceptable settings).
ErrInvalidInvitation = errors.New("lobby: invalid invitation")
// ErrGuestForbidden is returned when a guest attempts a durable-only action (invite a
// friend); the friend flow is gated to durable accounts.
ErrGuestForbidden = errors.New("lobby: guests cannot invite")
// ErrInvitationBlocked is returned when a block stands between the inviter and
// an invitee.
ErrInvitationBlocked = errors.New("lobby: invitation blocked between accounts")
// ErrInvitationNotFound is returned when no invitation matches the lookup.
ErrInvitationNotFound = errors.New("lobby: invitation not found")
// ErrInvitationNotPending is returned when an invitation is no longer open.
ErrInvitationNotPending = errors.New("lobby: invitation is not pending")
// ErrInvitationExpired is returned when an invitation has passed its deadline.
ErrInvitationExpired = errors.New("lobby: invitation has expired")
// ErrNotInvited is returned when an account is not an invitee of the invitation.
ErrNotInvited = errors.New("lobby: account was not invited")
// ErrAlreadyResponded is returned when an invitee has already accepted or declined.
ErrAlreadyResponded = errors.New("lobby: invitee has already responded")
// ErrNotInviter is returned when a non-inviter tries to cancel an invitation.
ErrNotInviter = errors.New("lobby: only the inviter may cancel")
)