Files
scrabble-game/backend/internal/lobby/lobby.go
T
Ilia Denisov 41a642ef97
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 37s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m8s
R4: push enrichment — events carry a state delta, kill the last poll
Enrich the in-app live stream into a delta channel so the UI renders a move from the event without a follow-up game.state, and make the matchmaking poll a stream-down fallback.

- pkg/fbs: trailing fields on opponent_moved (move+game+bag_len), your_turn (move_count), match_found (state), game_over (game), notify (account/invitation/state), MoveResult (rack+bag_len); regenerate Go + TS.
- backend: notify owns the FB encoding (encode.go + payload.go input structs); game/lobby/social map their domain types in. emitMove builds the move delta; game.Service.InitialState feeds match_found/game_started the recipient's initial StateView; friends/invitations notify carry their account/invitation. The move-commit response (submit_play/pass/exchange/resign) returns the actor's refilled rack + bag size.
- gateway: MoveResult transcode carries rack+bag_len.
- ui: pure lib/gamedelta.ts reducer advances the per-game cache keyed on move_count (idempotent + gap-safe); app.svelte seeds the cache on match_found/game_started; Game.svelte applies the delta (commit/pass/exchange/resign drop their load()); NewGame polls only while app.streamAlive is false.
- docs: ARCHITECTURE §10, FUNCTIONAL(+ru), backend/gateway/ui READMEs; PRERELEASE R4 marked done + Refinements.
2026-06-10 08:01:50 +02:00

75 lines
3.5 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/engine"
"scrabble/backend/internal/game"
"scrabble/backend/internal/notify"
)
// GameCreator is the slice of the game domain the lobby needs: starting a seated
// game and reading a player's initial view of it. 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 match_found / game_started events so the client renders the new game
// without a follow-up fetch (R4).
InitialState(ctx context.Context, gameID, accountID uuid.UUID) (notify.PlayerState, 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)
}
// 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")
)