Files
Halcyon/store/entities.go
T
2026-10-01 22:16:48 +02:00

377 lines
11 KiB
Go

package store
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"halcyon/sim"
)
// Ship statuses.
const (
StatusInventory = "inventory" // built, can be programmed
StatusLaunching = "launching" // enters the belt on the next run
StatusActive = "active" // in the belt
StatusScrapping = "scrapping" // scrapped: leaves the belt on the next run, a fresh ship replaces it
StatusScrapped = "scrapped" // gone; hidden from the fleet list
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 = ? AND status <> ? ORDER BY id`, owner, StatusScrapped)
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)
}
// Scrap queues a ship that is in the belt to be scrapped on the next daily
// run: it is removed from the world and its owner gets a new ship in inventory.
func (q *Q) Scrap(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 = ?, uplink = NULL WHERE id = ? AND owner_id = ? AND status = ?`,
StatusScrapping, ship, owner, StatusActive)
return q.requireOneRow(res, err)
}
// PendingScraps returns the ships to remove on the next run, ordered by id.
func (q *Q) PendingScraps(ctx context.Context) ([]int64, error) {
rows, err := q.query(ctx, `SELECT id FROM ships WHERE status = ? ORDER BY id`, StatusScrapping)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, rows.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
}
// DownlinkAt returns the downlink a ship produced in the run of the given day.
func (q *Q) DownlinkAt(ctx context.Context, owner, ship, day int64) ([]byte, error) {
var data []byte
err := q.row(ctx, `SELECT d.data FROM downlinks d JOIN ships s ON s.id = d.ship_id
WHERE d.ship_id = ? AND s.owner_id = ? AND d.day = ?`, ship, owner, day).Scan(&data)
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
return data, err
}
// DownlinkDays lists the days a ship has a stored downlink for, oldest first.
func (q *Q) DownlinkDays(ctx context.Context, owner, ship int64) ([]int64, error) {
rows, err := q.query(ctx, `SELECT d.day FROM downlinks d JOIN ships s ON s.id = d.ship_id
WHERE d.ship_id = ? AND s.owner_id = ? ORDER BY d.day`, ship, owner)
if err != nil {
return nil, err
}
defer rows.Close()
days := []int64{}
for rows.Next() {
var d int64
if err := rows.Scan(&d); err != nil {
return nil, err
}
days = append(days, d)
}
return days, rows.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
}
if _, err := q.exec(ctx, `INSERT INTO downlinks (ship_id, day, data) VALUES (?, ?, ?)
ON CONFLICT (ship_id, day) DO UPDATE SET data = excluded.data`,
d.ShipID, res.Day, d.Data); 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
}
// Scrapped ships are gone; each owner gets a new ship on the dock in exchange.
owners, err := q.query(ctx, `SELECT owner_id FROM ships WHERE status = ? ORDER BY id`, StatusScrapping)
if err != nil {
return err
}
var scrapped []int64
for owners.Next() {
var o int64
if err := owners.Scan(&o); err != nil {
owners.Close()
return err
}
scrapped = append(scrapped, o)
}
owners.Close()
if err := owners.Err(); err != nil {
return err
}
if _, err := q.exec(ctx, `UPDATE ships SET status = ? WHERE status = ?`, StatusScrapped, StatusScrapping); err != nil {
return err
}
for _, o := range scrapped {
if _, err := q.exec(ctx, `INSERT INTO ships (owner_id, status) VALUES (?, ?)`, o, StatusInventory); 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
}