Files
scrabble-game/backend/internal/engine/registry.go
T
Ilia Denisov 751e74b14f
Tests · Go / test (push) Successful in 6s
Tests · Integration / integration (push) Successful in 8s
Tests · Go / test (pull_request) Successful in 5s
Tests · Integration / integration (pull_request) Successful in 8s
Stage 3: game domain (lifecycle, journal+cache, hint, word-check, GCG, stats)
internal/game drives the engine over a single match and owns everything the
engine does not: event-sourced persistence (a games row + an append-only decoded
move journal, with the live engine.Game kept warm in a cache and rebuilt by
replay on a miss), the play/pass/exchange/resign transitions with
validate-at-submit scoring, an unlimited score/legality preview, the hint
(per-game allowance + profile wallet), the word-check tool with complaint
capture, per-player game state, history and GCG export (Poslfit dialect), and a
background turn-timeout sweeper that auto-resigns overdue turns honouring each
player's daily away window. Like Stages 1-2 it is a service/store layer with no
HTTP; the gateway surface lands in Stage 6.

Engine: additive decoded domain API (Direction, SubmitPlay/SubmitExchange/
EvaluatePlay/HintView/Hand, MoveRecord.{Dir,MainRow,MainCol}, Registry.Lookup,
ParseVariant) so internal/game never imports scrabble-solver; and a Resign fix
so the resigner keeps their score yet never wins (the other player wins a
two-player game). Timeout reuses Resign.

Persistence: migration 00002 adds games, game_players, game_moves, complaints,
account_stats and extends accounts with away_start/away_end/hint_balance; go-jet
regenerated. account gained SpendHint. Config adds BACKEND_DICT_DIR (required),
BACKEND_DICT_VERSION, BACKEND_GAME_TIMEOUT_SWEEP_INTERVAL, BACKEND_GAME_CACHE_TTL;
main loads the registry at boot (hard dependency) and starts the sweeper.

Tests: engine resign + decoded-API tests; game unit tests (GCG, away-window
boundaries, hint budget, cache, keyed mutex, payload); inttest integration
(lifecycle, replay equivalence, timeout sweep with away grace, resign stats,
hint policy, word-check/complaint, per-game-lock). Docs (PLAN, ARCHITECTURE,
FUNCTIONAL +_ru, TESTING, README) updated.
2026-06-02 17:33:49 +02:00

179 lines
5.5 KiB
Go

package engine
import (
"fmt"
"path/filepath"
"sort"
"sync"
dawg "github.com/iliadenisov/dafsa"
"scrabble-solver/scrabble"
)
// dictFiles maps each variant to its committed DAWG filename, as built by
// scrabble-solver and delivered in the dictionary directory.
var dictFiles = map[Variant]string{
VariantEnglish: "en_sowpods.dawg",
VariantRussianScrabble: "ru_scrabble.dawg",
VariantErudit: "ru_erudit.dawg",
}
// entry is one resident dictionary: the loaded finder and the solver built over
// it. The finder is retained so Close can release it.
type entry struct {
finder dawg.Finder
solver *scrabble.Solver
}
// Registry holds the dictionaries resident in memory, addressed by variant and
// dictionary version, and the solvers built over them. Several versions of a
// variant may be resident at once; a game pins the version it started on. The
// admin reload flow (a later stage) registers a new version through Load.
// Registry is safe for concurrent use.
type Registry struct {
mu sync.RWMutex
entries map[Variant]map[string]entry
latest map[Variant]string
}
// NewRegistry constructs an empty Registry. Use Load or Open to populate it.
func NewRegistry() *Registry {
return &Registry{
entries: make(map[Variant]map[string]entry),
latest: make(map[Variant]string),
}
}
// Open builds a Registry by loading, at dictionary version, the committed DAWG of
// every requested variant (or all variants when none are named) from dir. It
// fails if a variant is unknown or its file is missing or unreadable; a
// partially loaded registry is closed before the error is returned.
func Open(dir, version string, variants ...Variant) (*Registry, error) {
if len(variants) == 0 {
variants = Variants()
}
r := NewRegistry()
for _, v := range variants {
if err := r.Load(v, version, dir); err != nil {
_ = r.Close()
return nil, err
}
}
return r, nil
}
// Load reads the committed DAWG of variant from dir, builds a solver over it and
// registers it under version. Reloading the same (variant, version) replaces the
// previous entry, closing its finder. The most recently loaded version of a
// variant becomes its latest.
func (r *Registry) Load(v Variant, version, dir string) error {
rs, ok := v.ruleset()
if !ok {
return fmt.Errorf("%w: %d", ErrUnknownVariant, v)
}
path := filepath.Join(dir, dictFiles[v])
finder, err := dawg.Load(path)
if err != nil {
return fmt.Errorf("engine: load %s dictionary %q from %s: %w", v, version, path, err)
}
r.mu.Lock()
defer r.mu.Unlock()
if r.entries[v] == nil {
r.entries[v] = make(map[string]entry)
}
if old, ok := r.entries[v][version]; ok {
_ = old.finder.Close()
}
r.entries[v][version] = entry{finder: finder, solver: scrabble.NewSolver(rs, finder)}
r.latest[v] = version
return nil
}
// Solver returns the solver for the (variant, version) pair, or ErrUnknownVariant
// when the variant is absent and ErrUnknownVersion when only the version is.
func (r *Registry) Solver(v Variant, version string) (*scrabble.Solver, error) {
r.mu.RLock()
defer r.mu.RUnlock()
versions, ok := r.entries[v]
if !ok {
return nil, fmt.Errorf("%w: %s", ErrUnknownVariant, v)
}
e, ok := versions[version]
if !ok {
return nil, fmt.Errorf("%w: %s/%s", ErrUnknownVersion, v, version)
}
return e.solver, nil
}
// Latest returns the most recently loaded version of variant and its solver, or
// ErrUnknownVariant when none is resident.
func (r *Registry) Latest(v Variant) (string, *scrabble.Solver, error) {
r.mu.RLock()
defer r.mu.RUnlock()
version, ok := r.latest[v]
if !ok {
return "", nil, fmt.Errorf("%w: %s", ErrUnknownVariant, v)
}
return version, r.entries[v][version].solver, nil
}
// Versions returns the dictionary versions resident for variant, sorted, or nil
// when none are.
func (r *Registry) Versions(v Variant) []string {
r.mu.RLock()
defer r.mu.RUnlock()
if len(r.entries[v]) == 0 {
return nil
}
versions := make([]string, 0, len(r.entries[v]))
for ver := range r.entries[v] {
versions = append(versions, ver)
}
sort.Strings(versions)
return versions
}
// Lookup reports whether word is present in the (variant, version) dictionary,
// backing the unlimited word-check tool. It returns ErrUnknownVariant or
// ErrUnknownVersion when that dictionary is not resident, and an error when word
// contains a character outside the variant's alphabet. The word is matched as
// given; callers normalise case to the variant's alphabet first.
func (r *Registry) Lookup(v Variant, version, word string) (bool, error) {
r.mu.RLock()
defer r.mu.RUnlock()
versions, ok := r.entries[v]
if !ok {
return false, fmt.Errorf("%w: %s", ErrUnknownVariant, v)
}
e, ok := versions[version]
if !ok {
return false, fmt.Errorf("%w: %s/%s", ErrUnknownVersion, v, version)
}
idx, err := e.finder.IndexOf(word)
if err != nil {
return false, fmt.Errorf("engine: lookup %q in %s/%s: %w", word, v, version, err)
}
return idx >= 0, nil
}
// Close releases every resident dictionary and empties the registry. It is safe
// to call more than once; the first close error is returned after all finders
// have been closed.
func (r *Registry) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
var firstErr error
for v, versions := range r.entries {
for ver, e := range versions {
if err := e.finder.Close(); err != nil && firstErr == nil {
firstErr = fmt.Errorf("engine: close %s/%s dictionary: %w", v, ver, err)
}
}
}
r.entries = make(map[Variant]map[string]entry)
r.latest = make(map[Variant]string)
return firstErr
}