This commit is contained in:
root
2026-09-19 20:21:47 +02:00
commit 0798933b05
62 changed files with 7658 additions and 0 deletions
+284
View File
@@ -0,0 +1,284 @@
package store
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"wh/sim"
)
// Ship statuses.
const (
StatusInventory = "inventory" // built, can be programmed
StatusLaunching = "launching" // enters the belt on the next run
StatusActive = "active" // in the belt
StatusDestroyed = "destroyed"
)
var (
ErrNotFound = errors.New("not found")
ErrState = errors.New("ship is not in the right state for that")
ErrTaken = errors.New("name already taken")
)
type Player struct {
ID int64
Name string
Credits int64
}
type Ship struct {
ID int64
OwnerID int64
Status string
ProgramSize int
DownlinkDay int64 // -1 if none yet
}
func HashKey(key string) string {
h := sha256.Sum256([]byte(key))
return hex.EncodeToString(h[:])
}
// Register creates a player with one command ship in inventory and returns
// the player's API key (shown once; only its hash is stored).
func (s *Store) Register(ctx context.Context, name string) (Player, string, int64, error) {
var (
p Player
shipID int64
key string
)
b := make([]byte, 24)
if _, err := rand.Read(b); err != nil {
return p, "", 0, err
}
key = hex.EncodeToString(b)
err := s.WithTx(ctx, func(q *Q) error {
var n int
if err := q.row(ctx, `SELECT COUNT(*) FROM players WHERE name = ?`, name).Scan(&n); err != nil {
return err
}
if n > 0 {
return ErrTaken
}
if err := q.row(ctx, `INSERT INTO players (name, key_hash) VALUES (?, ?) RETURNING id`,
name, HashKey(key)).Scan(&p.ID); err != nil {
return err
}
p.Name = name
return q.row(ctx, `INSERT INTO ships (owner_id, status) VALUES (?, ?) RETURNING id`,
p.ID, StatusInventory).Scan(&shipID)
})
return p, key, shipID, err
}
func (q *Q) PlayerByKey(ctx context.Context, key string) (Player, error) {
var p Player
err := q.row(ctx, `SELECT id, name, credits FROM players WHERE key_hash = ?`, HashKey(key)).
Scan(&p.ID, &p.Name, &p.Credits)
if err == sql.ErrNoRows {
return p, ErrNotFound
}
return p, err
}
func (q *Q) PlayerShips(ctx context.Context, owner int64) ([]Ship, error) {
rows, err := q.query(ctx, `SELECT id, owner_id, status, COALESCE(LENGTH(program), 0), downlink_day
FROM ships WHERE owner_id = ? ORDER BY id`, owner)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Ship
for rows.Next() {
var sh Ship
if err := rows.Scan(&sh.ID, &sh.OwnerID, &sh.Status, &sh.ProgramSize, &sh.DownlinkDay); err != nil {
return nil, err
}
out = append(out, sh)
}
return out, rows.Err()
}
// ownedStatus returns the ship's status if owner owns it.
func (q *Q) ownedStatus(ctx context.Context, owner, ship int64) (string, error) {
var st string
err := q.row(ctx, `SELECT status FROM ships WHERE id = ? AND owner_id = ?`, ship, owner).Scan(&st)
if err == sql.ErrNoRows {
return "", ErrNotFound
}
return st, err
}
func (q *Q) requireOneRow(res sql.Result, err error) error {
if err != nil {
return err
}
if n, _ := res.RowsAffected(); n == 0 {
return ErrState
}
return nil
}
// SetProgram replaces the program of a ship that is still in inventory.
func (q *Q) SetProgram(ctx context.Context, owner, ship int64, prog []byte) error {
if _, err := q.ownedStatus(ctx, owner, ship); err != nil {
return err
}
res, err := q.exec(ctx, `UPDATE ships SET program = ? WHERE id = ? AND owner_id = ? AND status = ?`,
prog, ship, owner, StatusInventory)
return q.requireOneRow(res, err)
}
// Launch queues an inventory ship (which must have a program) to enter the
// belt on the next daily run.
func (q *Q) Launch(ctx context.Context, owner, ship int64) error {
if _, err := q.ownedStatus(ctx, owner, ship); err != nil {
return err
}
res, err := q.exec(ctx, `UPDATE ships SET status = ? WHERE id = ? AND owner_id = ? AND status = ?
AND program IS NOT NULL`, StatusLaunching, ship, owner, StatusInventory)
return q.requireOneRow(res, err)
}
// SetUplink stores the message delivered on the next run, replacing any
// message already queued.
func (q *Q) SetUplink(ctx context.Context, owner, ship int64, data []byte) error {
if _, err := q.ownedStatus(ctx, owner, ship); err != nil {
return err
}
res, err := q.exec(ctx, `UPDATE ships SET uplink = ? WHERE id = ? AND owner_id = ? AND status IN (?, ?)`,
data, ship, owner, StatusActive, StatusLaunching)
return q.requireOneRow(res, err)
}
// Downlink returns the latest downlink and the day it was produced.
func (q *Q) Downlink(ctx context.Context, owner, ship int64) ([]byte, int64, error) {
var (
data []byte
day int64
)
err := q.row(ctx, `SELECT downlink, downlink_day FROM ships WHERE id = ? AND owner_id = ?`, ship, owner).
Scan(&data, &day)
if err == sql.ErrNoRows {
return nil, 0, ErrNotFound
}
return data, day, err
}
// PendingLaunches returns ships due to launch, ordered by id.
func (q *Q) PendingLaunches(ctx context.Context) ([]sim.Launch, error) {
rows, err := q.query(ctx, `SELECT id, owner_id, program FROM ships WHERE status = ? ORDER BY id`, StatusLaunching)
if err != nil {
return nil, err
}
defer rows.Close()
var out []sim.Launch
for rows.Next() {
var l sim.Launch
if err := rows.Scan(&l.ShipID, &l.Owner, &l.Program); err != nil {
return nil, err
}
out = append(out, l)
}
return out, rows.Err()
}
// PendingUplinks returns queued uplinks, ordered by ship id.
func (q *Q) PendingUplinks(ctx context.Context) ([]sim.Uplink, error) {
rows, err := q.query(ctx, `SELECT id, uplink FROM ships WHERE uplink IS NOT NULL ORDER BY id`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []sim.Uplink
for rows.Next() {
var u sim.Uplink
if err := rows.Scan(&u.ShipID, &u.Data); err != nil {
return nil, err
}
out = append(out, u)
}
return out, rows.Err()
}
// LoadState returns the most recent world snapshot, or nil if none exists.
func (q *Q) LoadState(ctx context.Context) (*sim.State, error) {
var data []byte
err := q.row(ctx, `SELECT data FROM snapshots ORDER BY day DESC LIMIT 1`).Scan(&data)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
var st sim.State
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&st); err != nil {
return nil, fmt.Errorf("decode snapshot: %w", err)
}
return &st, nil
}
func (q *Q) SaveState(ctx context.Context, st *sim.State) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(st); err != nil {
return err
}
h := st.Hash()
_, err := q.exec(ctx, `INSERT INTO snapshots (day, hash, data) VALUES (?, ?, ?)`,
st.Day, hex.EncodeToString(h[:]), buf.Bytes())
return err
}
// RecordRun writes the outputs of a completed day.
func (q *Q) RecordRun(ctx context.Context, res sim.DayResult, ranAt string) error {
h := res.Hash
if _, err := q.exec(ctx, `INSERT INTO day_runs (day, hash, ran_at) VALUES (?, ?, ?)`,
res.Day, hex.EncodeToString(h[:]), ranAt); err != nil {
return err
}
for _, e := range res.Events {
if _, err := q.exec(ctx, `INSERT INTO events (day, tick, ship_id, kind, detail) VALUES (?, ?, ?, ?, ?)`,
res.Day, e.Tick, e.ShipID, e.Kind, e.Detail); err != nil {
return err
}
}
for _, d := range res.Downlinks {
if _, err := q.exec(ctx, `UPDATE ships SET downlink = ?, downlink_day = ? WHERE id = ?`,
d.Data, res.Day, d.ShipID); err != nil {
return err
}
}
return nil
}
// AfterRun clears delivered uplinks, activates launched ships, marks
// destroyed ones and updates player balances.
func (q *Q) AfterRun(ctx context.Context, st *sim.State) error {
if _, err := q.exec(ctx, `UPDATE ships SET uplink = NULL WHERE uplink IS NOT NULL`); err != nil {
return err
}
if _, err := q.exec(ctx, `UPDATE ships SET status = ? WHERE status = ?`, StatusActive, StatusLaunching); err != nil {
return err
}
for _, sh := range st.Ships {
if !sh.Alive {
if _, err := q.exec(ctx, `UPDATE ships SET status = ? WHERE id = ?`, StatusDestroyed, sh.ID); err != nil {
return err
}
}
}
for owner, credits := range st.Credits {
if _, err := q.exec(ctx, `UPDATE players SET credits = ? WHERE id = ?`, credits, owner); err != nil {
return err
}
}
return nil
}
+177
View File
@@ -0,0 +1,177 @@
// Package store persists players, ships and world snapshots in SQLite or
// PostgreSQL through database/sql.
package store
import (
"context"
"database/sql"
"fmt"
"strings"
_ "github.com/jackc/pgx/v5/stdlib"
_ "modernc.org/sqlite"
)
type Dialect int
const (
SQLite Dialect = iota
Postgres
)
// Store is a database handle. Methods on the embedded Q run directly on the
// pool; use WithTx for atomic multi-step work.
type Store struct {
*Q
db *sql.DB
}
type execer interface {
ExecContext(ctx context.Context, q string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, q string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, q string, args ...any) *sql.Row
}
// Q holds the query methods; it wraps either a pool or a transaction.
type Q struct {
x execer
d Dialect
tx bool
}
// Open connects using dsn. A "postgres://" or "postgresql://" URL selects
// PostgreSQL; anything else is a SQLite file path (optionally prefixed with
// "sqlite:").
func Open(dsn string) (*Store, error) {
var (
driver string
d Dialect
)
switch {
case strings.HasPrefix(dsn, "postgres://"), strings.HasPrefix(dsn, "postgresql://"):
driver, d = "pgx", Postgres
default:
path := strings.TrimPrefix(dsn, "sqlite:")
sep := "?"
if strings.Contains(path, "?") {
sep = "&"
}
dsn = "file:" + path + sep + "_txlock=immediate&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)"
driver, d = "sqlite", SQLite
}
db, err := sql.Open(driver, dsn)
if err != nil {
return nil, err
}
if d == SQLite {
db.SetMaxOpenConns(1) // SQLite allows a single writer; keep it simple
}
return &Store{Q: &Q{x: db, d: d}, db: db}, nil
}
func (s *Store) Close() error { return s.db.Close() }
// WithTx runs fn in a transaction, committing if it returns nil.
func (s *Store) WithTx(ctx context.Context, fn func(q *Q) error) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
if err := fn(&Q{x: tx, d: s.d, tx: true}); err != nil {
tx.Rollback()
return err
}
return tx.Commit()
}
// rebind converts ? placeholders to $n for PostgreSQL.
func (q *Q) rebind(query string) string {
if q.d != Postgres {
return query
}
var b strings.Builder
n := 0
for _, r := range query {
if r == '?' {
n++
fmt.Fprintf(&b, "$%d", n)
} else {
b.WriteRune(r)
}
}
return b.String()
}
func (q *Q) exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
return q.x.ExecContext(ctx, q.rebind(query), args...)
}
func (q *Q) query(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return q.x.QueryContext(ctx, q.rebind(query), args...)
}
func (q *Q) row(ctx context.Context, query string, args ...any) *sql.Row {
return q.x.QueryRowContext(ctx, q.rebind(query), args...)
}
var schema = []string{
`CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)`,
`CREATE TABLE IF NOT EXISTS players (
id {ID}, name TEXT NOT NULL UNIQUE, key_hash TEXT NOT NULL UNIQUE,
credits BIGINT NOT NULL DEFAULT 0)`,
`CREATE TABLE IF NOT EXISTS ships (
id {ID}, owner_id BIGINT NOT NULL REFERENCES players(id),
status TEXT NOT NULL, program {BLOB}, uplink {BLOB}, downlink {BLOB},
downlink_day BIGINT NOT NULL DEFAULT -1)`,
`CREATE INDEX IF NOT EXISTS ships_owner ON ships(owner_id)`,
`CREATE INDEX IF NOT EXISTS ships_status ON ships(status)`,
`CREATE TABLE IF NOT EXISTS snapshots (
day BIGINT PRIMARY KEY, hash TEXT NOT NULL, data {BLOB} NOT NULL)`,
`CREATE TABLE IF NOT EXISTS events (
id {ID}, day BIGINT NOT NULL, tick INTEGER NOT NULL,
ship_id BIGINT NOT NULL, kind TEXT NOT NULL, detail TEXT NOT NULL)`,
`CREATE INDEX IF NOT EXISTS events_day ON events(day)`,
`CREATE TABLE IF NOT EXISTS day_runs (
day BIGINT PRIMARY KEY, hash TEXT NOT NULL, ran_at TEXT NOT NULL)`,
`INSERT INTO meta (key, value) VALUES ('lock', '') ON CONFLICT (key) DO NOTHING`,
}
// Migrate creates the schema if it does not exist.
func (s *Store) Migrate(ctx context.Context) error {
id, blob := "INTEGER PRIMARY KEY AUTOINCREMENT", "BLOB"
if s.d == Postgres {
id, blob = "BIGSERIAL PRIMARY KEY", "BYTEA"
}
r := strings.NewReplacer("{ID}", id, "{BLOB}", blob)
for _, stmt := range schema {
if _, err := s.exec(ctx, r.Replace(stmt)); err != nil {
return fmt.Errorf("migrate: %w", err)
}
}
return nil
}
// LockWorld serialises daily runs. SQLite transactions are already exclusive
// (immediate mode); PostgreSQL takes a row lock held until commit.
func (q *Q) LockWorld(ctx context.Context) error {
if q.d != Postgres {
return nil
}
var v string
return q.row(ctx, `SELECT value FROM meta WHERE key = 'lock' FOR UPDATE`).Scan(&v)
}
func (q *Q) GetMeta(ctx context.Context, key string) (string, bool, error) {
var v string
err := q.row(ctx, `SELECT value FROM meta WHERE key = ?`, key).Scan(&v)
if err == sql.ErrNoRows {
return "", false, nil
}
return v, err == nil, err
}
func (q *Q) SetMeta(ctx context.Context, key, value string) error {
_, err := q.exec(ctx, `INSERT INTO meta (key, value) VALUES (?, ?)
ON CONFLICT (key) DO UPDATE SET value = excluded.value`, key, value)
return err
}