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

55 lines
1.8 KiB
Go

package game
import (
"errors"
"fmt"
"time"
)
// Config configures the game subsystem: where the engine loads its dictionaries,
// which version new games pin, and the two background knobs — the turn-timeout
// sweep cadence and the idle window after which the live-game cache evicts a game
// (it is then rebuilt from the journal on next access, so this never affects
// correctness). It composes into the backend configuration.
type Config struct {
// DictDir is the directory holding the committed DAWG files. Sourced from
// BACKEND_DICT_DIR; it has no default and must be set.
DictDir string
// DictVersion labels the dictionary version new games pin. Sourced from
// BACKEND_DICT_VERSION.
DictVersion string
// TimeoutSweepInterval is how often the sweeper scans for overdue turns.
// Sourced from BACKEND_GAME_TIMEOUT_SWEEP_INTERVAL.
TimeoutSweepInterval time.Duration
// CacheTTL is how long an idle game stays resident before eviction. Sourced
// from BACKEND_GAME_CACHE_TTL.
CacheTTL time.Duration
}
// DefaultConfig returns the game configuration defaults. DictDir is deliberately
// empty: it must be supplied through the environment.
func DefaultConfig() Config {
return Config{
DictVersion: "v1",
TimeoutSweepInterval: time.Minute,
CacheTTL: 24 * time.Hour,
}
}
// Validate reports whether the configuration is usable.
func (c Config) Validate() error {
if c.DictDir == "" {
return errors.New("game: BACKEND_DICT_DIR must be set")
}
if c.DictVersion == "" {
return errors.New("game: BACKEND_DICT_VERSION must not be empty")
}
if c.TimeoutSweepInterval <= 0 {
return fmt.Errorf("game: timeout sweep interval must be positive, got %s", c.TimeoutSweepInterval)
}
if c.CacheTTL <= 0 {
return fmt.Errorf("game: cache TTL must be positive, got %s", c.CacheTTL)
}
return nil
}