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.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
// Command backend is the Scrabble platform's internal domain service. It boots
|
||||
// the OpenTelemetry runtime, opens the Postgres pool and applies migrations,
|
||||
// warms the session cache, and serves the HTTP listener with the infrastructure
|
||||
// probes and the /api/v1 route-group skeleton. Domain endpoints are added by
|
||||
// later stages described in PLAN.md.
|
||||
// loads the dictionaries into the engine registry, warms the session cache,
|
||||
// constructs the game domain and starts its turn-timeout sweeper, then serves the
|
||||
// HTTP listener with the infrastructure probes and the /api/v1 route-group
|
||||
// skeleton. Domain HTTP endpoints are added with the gateway in a later stage
|
||||
// described in PLAN.md.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -15,7 +17,10 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"scrabble/backend/internal/account"
|
||||
"scrabble/backend/internal/config"
|
||||
"scrabble/backend/internal/engine"
|
||||
"scrabble/backend/internal/game"
|
||||
"scrabble/backend/internal/postgres"
|
||||
"scrabble/backend/internal/server"
|
||||
"scrabble/backend/internal/session"
|
||||
@@ -46,7 +51,8 @@ func main() {
|
||||
}
|
||||
|
||||
// run wires the process dependencies in order — telemetry, database (with
|
||||
// migrations), session cache, HTTP server — and blocks until ctx is cancelled.
|
||||
// migrations), engine dictionaries, session cache, game domain (with its
|
||||
// turn-timeout sweeper), HTTP server — and blocks until ctx is cancelled.
|
||||
func run(ctx context.Context, cfg config.Config, logger *zap.Logger) error {
|
||||
tel, err := telemetry.New(ctx, cfg.Telemetry)
|
||||
if err != nil {
|
||||
@@ -74,12 +80,26 @@ func run(ctx context.Context, cfg config.Config, logger *zap.Logger) error {
|
||||
}
|
||||
logger.Info("database migrations applied")
|
||||
|
||||
registry, err := engine.Open(cfg.Game.DictDir, cfg.Game.DictVersion)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load dictionaries: %w", err)
|
||||
}
|
||||
defer func() { _ = registry.Close() }()
|
||||
logger.Info("dictionaries loaded",
|
||||
zap.String("dir", cfg.Game.DictDir),
|
||||
zap.String("version", cfg.Game.DictVersion))
|
||||
|
||||
sessions := session.NewService(session.NewStore(db), session.NewCache())
|
||||
if err := sessions.Warm(ctx); err != nil {
|
||||
return fmt.Errorf("warm session cache: %w", err)
|
||||
}
|
||||
logger.Info("session cache warmed")
|
||||
|
||||
games := game.NewService(game.NewStore(db), account.NewStore(db), registry, cfg.Game, logger)
|
||||
go games.RunSweeper(ctx, cfg.Game.TimeoutSweepInterval)
|
||||
logger.Info("game turn-timeout sweeper started",
|
||||
zap.Duration("interval", cfg.Game.TimeoutSweepInterval))
|
||||
|
||||
srv := server.New(cfg.HTTPAddr, server.Deps{
|
||||
Logger: logger,
|
||||
DB: db,
|
||||
|
||||
Reference in New Issue
Block a user