751e74b14f
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.
90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package game
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"scrabble/backend/internal/engine"
|
|
)
|
|
|
|
// movePayload is the JSON stored in game_moves.payload. It holds the decoded,
|
|
// dictionary-independent values needed both to replay the game through the engine
|
|
// and to render history / emit GCG without a dictionary (docs/ARCHITECTURE.md
|
|
// §9.1): the acting player's rack before the move, and per action the play's
|
|
// direction, main-word anchor, placed tiles and formed words, or an exchange's
|
|
// swapped tiles.
|
|
type movePayload struct {
|
|
Rack []string `json:"rack"`
|
|
Dir string `json:"dir,omitempty"`
|
|
MainRow int `json:"main_row,omitempty"`
|
|
MainCol int `json:"main_col,omitempty"`
|
|
Tiles []tilePayload `json:"tiles,omitempty"`
|
|
Words []string `json:"words,omitempty"`
|
|
Exchanged []string `json:"exchanged,omitempty"`
|
|
}
|
|
|
|
// tilePayload is one placed tile in a play payload.
|
|
type tilePayload struct {
|
|
Row int `json:"row"`
|
|
Col int `json:"col"`
|
|
Letter string `json:"letter"`
|
|
Blank bool `json:"blank,omitempty"`
|
|
}
|
|
|
|
// buildPayload assembles the journal payload from the engine's decoded record,
|
|
// the acting rack captured before the move, and (for an exchange) the swapped
|
|
// tiles.
|
|
func buildPayload(rec engine.MoveRecord, rackBefore, exchanged []string) movePayload {
|
|
p := movePayload{Rack: rackBefore}
|
|
switch rec.Action {
|
|
case engine.ActionPlay:
|
|
p.Dir = rec.Dir.String()
|
|
p.MainRow = rec.MainRow
|
|
p.MainCol = rec.MainCol
|
|
p.Words = rec.Words
|
|
p.Tiles = make([]tilePayload, len(rec.Tiles))
|
|
for i, t := range rec.Tiles {
|
|
p.Tiles[i] = tilePayload{Row: t.Row, Col: t.Col, Letter: t.Letter, Blank: t.Blank}
|
|
}
|
|
case engine.ActionExchange:
|
|
p.Exchanged = exchanged
|
|
}
|
|
return p
|
|
}
|
|
|
|
// marshal renders the payload as the JSON text stored in the column.
|
|
func (p movePayload) marshal() (string, error) {
|
|
b, err := json.Marshal(p)
|
|
if err != nil {
|
|
return "", fmt.Errorf("game: marshal move payload: %w", err)
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// parsePayload parses a stored payload back into its decoded fields.
|
|
func parsePayload(s string) (movePayload, error) {
|
|
var p movePayload
|
|
if err := json.Unmarshal([]byte(s), &p); err != nil {
|
|
return movePayload{}, fmt.Errorf("game: parse move payload: %w", err)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// tileRecords converts payload tiles back into engine TileRecords for replay and
|
|
// history.
|
|
func (p movePayload) tileRecords() []engine.TileRecord {
|
|
out := make([]engine.TileRecord, len(p.Tiles))
|
|
for i, t := range p.Tiles {
|
|
out[i] = engine.TileRecord{Row: t.Row, Col: t.Col, Letter: t.Letter, Blank: t.Blank}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// direction parses the stored "H"/"V" into an engine.Direction.
|
|
func (p movePayload) direction() engine.Direction {
|
|
if p.Dir == engine.Vertical.String() {
|
|
return engine.Vertical
|
|
}
|
|
return engine.Horizontal
|
|
}
|