Files
scrabble-game/backend/internal/lobby/lobby.go
T
Ilia Denisov 85baabe4ba
Tests · Go / test (push) Successful in 6s
Tests · Integration / integration (push) Successful in 10s
Tests · Go / test (pull_request) Successful in 5s
Tests · Integration / integration (pull_request) Successful in 10s
Stage 5: robot opponent (pool, seed-derived strategy, move driver, matchmaker substitution)
- internal/robot: durable kind='robot' account pool (migration 00004); every
  per-game and per-turn choice derived deterministically from the game seed
  (restart-stable FNV mix); a background move driver; margin targeting (band
  1-30, closest-to-band); right-skewed [2,90]min delays (median ~10m);
  opponent-anchored sleep with +/-3h drift; daytime nudge reply + proactive
  12h nudge; friend/chat blocked via profile toggles.
- engine.Candidates (decoded ranked plays); game.Candidates + RobotTurns;
  social.LastNudgeAt.
- matchmaker: 10s wait then robot substitution (reaper) + Poll delivery seam.
- config (BACKEND_ROBOT_DRIVE_INTERVAL, BACKEND_LOBBY_ROBOT_WAIT,
  BACKEND_LOBBY_REAPER_INTERVAL); main wiring + boot-time pool provisioning.
- metrics: robot account_stats (authoritative balance) + robot_games_finished_total
  OTel counter + per-finish log.
- docs: PLAN, ARCHITECTURE, FUNCTIONAL(+ru), TESTING, README; account.go comment.
- tests: robot strategy units, matchmaker reaper/Poll, engine.Candidates; inttest
  robot full-game / substitution / proactive-nudge.
2026-06-02 21:02:20 +02:00

69 lines
3.1 KiB
Go

// Package lobby forms games: an in-memory matchmaking pool that pairs two humans
// for an auto-match, and friend-game invitations (invite -> accept) that start a
// 2-4 player game once every invitee has accepted. Both produce a game through the
// game domain (a GameCreator); neither imports the engine. The matchmaking pool
// is in-memory and lost on restart (players re-queue); the robot that substitutes
// for a missing human after a short wait is added in a later stage.
package lobby
import (
"context"
"errors"
"github.com/google/uuid"
"scrabble/backend/internal/game"
)
// GameCreator is the slice of the game domain the lobby needs: starting a seated
// game. game.Service satisfies it.
type GameCreator interface {
Create(ctx context.Context, params game.CreateParams) (game.Game, 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() (uuid.UUID, error)
}
// Blocker reports whether two accounts have a block between them (either
// direction). social.Service satisfies it; the lobby uses it to refuse
// invitations between blocked accounts.
type Blocker interface {
IsBlocked(ctx context.Context, a, b uuid.UUID) (bool, 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 (
// ErrAlreadyQueued is returned when an account already waits in a pool.
ErrAlreadyQueued = errors.New("lobby: account already in the matchmaking pool")
// ErrInvalidInvitation is returned for a malformed invitation (bad player
// count, duplicate or self invitee, or unacceptable settings).
ErrInvalidInvitation = errors.New("lobby: invalid invitation")
// 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")
)