This commit is contained in:
root
2026-10-01 22:16:48 +02:00
parent b6d1909d7f
commit 9dfdf79fa7
39 changed files with 4560 additions and 181 deletions
+93 -1
View File
@@ -19,6 +19,8 @@ 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"
)
@@ -91,7 +93,7 @@ func (q *Q) PlayerByKey(ctx context.Context, key string) (Player, error) {
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)
FROM ships WHERE owner_id = ? AND status <> ? ORDER BY id`, owner, StatusScrapped)
if err != nil {
return nil, err
}
@@ -148,6 +150,35 @@ func (q *Q) Launch(ctx context.Context, owner, ship int64) error {
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 {
@@ -173,6 +204,36 @@ func (q *Q) Downlink(ctx context.Context, owner, ship int64) ([]byte, int64, err
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)
@@ -255,6 +316,11 @@ func (q *Q) RecordRun(ctx context.Context, res sim.DayResult, ranAt string) erro
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
}
@@ -268,6 +334,32 @@ func (q *Q) AfterRun(ctx context.Context, st *sim.State) error {
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 {