vibes
This commit is contained in:
+157
@@ -0,0 +1,157 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AuditEntry is one thing a player did, recorded for the operator.
|
||||
type AuditEntry struct {
|
||||
At int64 // unix seconds
|
||||
PlayerID int64 // the account that acted
|
||||
Action string // register, login, code_save, code_clear, program_upload, launch, scrap, uplink
|
||||
Ship int64 // the ship concerned, or 0
|
||||
Slot int // the code slot concerned, or 0
|
||||
Detail string // JSON with the specifics
|
||||
IP string
|
||||
}
|
||||
|
||||
// AuditRow is an entry as listed, with its id and the account's name.
|
||||
type AuditRow struct {
|
||||
AuditEntry
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
// AddAudit appends an entry to the audit log.
|
||||
func (q *Q) AddAudit(ctx context.Context, e AuditEntry) error {
|
||||
_, err := q.exec(ctx, `INSERT INTO audit_log (at, player_id, action, ship_id, slot, detail, ip) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
e.At, e.PlayerID, e.Action, e.Ship, e.Slot, e.Detail, e.IP)
|
||||
return err
|
||||
}
|
||||
|
||||
// AuditFilter narrows ListAudit. Zero values mean "any".
|
||||
type AuditFilter struct {
|
||||
PlayerID int64
|
||||
Action string
|
||||
Before int64 // only entries with an id below this, for paging back through the log
|
||||
Limit int
|
||||
}
|
||||
|
||||
// ListAudit returns entries newest first.
|
||||
func (q *Q) ListAudit(ctx context.Context, f AuditFilter) ([]AuditRow, error) {
|
||||
if f.Limit <= 0 || f.Limit > 500 {
|
||||
f.Limit = 100
|
||||
}
|
||||
rows, err := q.query(ctx, `SELECT a.id, a.at, a.player_id, COALESCE(p.name, ''), a.action, a.ship_id, a.slot, a.detail, a.ip
|
||||
FROM audit_log a LEFT JOIN players p ON p.id = a.player_id
|
||||
WHERE (? = 0 OR a.player_id = ?) AND (? = '' OR a.action = ?) AND (? = 0 OR a.id < ?)
|
||||
ORDER BY a.id DESC LIMIT ?`,
|
||||
f.PlayerID, f.PlayerID, f.Action, f.Action, f.Before, f.Before, f.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []AuditRow{}
|
||||
for rows.Next() {
|
||||
var r AuditRow
|
||||
if err := rows.Scan(&r.ID, &r.At, &r.PlayerID, &r.Name, &r.Action, &r.Ship, &r.Slot, &r.Detail, &r.IP); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AccountRow summarises one account for the admin list.
|
||||
type AccountRow struct {
|
||||
ID int64
|
||||
Name string
|
||||
Credits int64
|
||||
Ships int
|
||||
Active int // ships in the belt
|
||||
Slots int // code slots in use
|
||||
Actions int // audit entries
|
||||
LastLogin int64
|
||||
LastAction int64
|
||||
}
|
||||
|
||||
// ListAccounts returns every account, oldest first.
|
||||
func (q *Q) ListAccounts(ctx context.Context) ([]AccountRow, error) {
|
||||
rows, err := q.query(ctx, `SELECT p.id, p.name, p.credits,
|
||||
(SELECT COUNT(*) FROM ships s WHERE s.owner_id = p.id),
|
||||
(SELECT COUNT(*) FROM ships s WHERE s.owner_id = p.id AND s.status = ?),
|
||||
(SELECT COUNT(*) FROM code_slots c WHERE c.player_id = p.id),
|
||||
(SELECT COUNT(*) FROM audit_log a WHERE a.player_id = p.id),
|
||||
(SELECT MAX(a.at) FROM audit_log a WHERE a.player_id = p.id AND a.action = 'login'),
|
||||
(SELECT MAX(a.at) FROM audit_log a WHERE a.player_id = p.id)
|
||||
FROM players p ORDER BY p.id`, StatusActive)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []AccountRow{}
|
||||
for rows.Next() {
|
||||
var r AccountRow
|
||||
var login, last sql.NullInt64
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.Credits, &r.Ships, &r.Active, &r.Slots, &r.Actions, &login, &last); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.LastLogin, r.LastAction = login.Int64, last.Int64
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// PlayerByID looks an account up by number.
|
||||
func (q *Q) PlayerByID(ctx context.Context, id int64) (Player, error) {
|
||||
var p Player
|
||||
err := q.row(ctx, `SELECT id, name, credits FROM players WHERE id = ?`, id).Scan(&p.ID, &p.Name, &p.Credits)
|
||||
if err == sql.ErrNoRows {
|
||||
return p, ErrNotFound
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
|
||||
// SlotSummary describes a saved code slot without its source.
|
||||
type SlotSummary struct {
|
||||
Slot int
|
||||
Name string
|
||||
Size int
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
// ListSlotSummaries returns a player's saved slots for the admin view.
|
||||
func (q *Q) ListSlotSummaries(ctx context.Context, owner int64) ([]SlotSummary, error) {
|
||||
rows, err := q.query(ctx, `SELECT slot, name, LENGTH(source), updated_at FROM code_slots WHERE player_id = ? ORDER BY slot`, owner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []SlotSummary{}
|
||||
for rows.Next() {
|
||||
var s SlotSummary
|
||||
if err := rows.Scan(&s.Slot, &s.Name, &s.Size, &s.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AuditActions lists the action names the log uses.
|
||||
var AuditActions = []string{"register", "login", "code_save", "code_clear", "program_upload", "launch", "scrap", "uplink"}
|
||||
|
||||
// ValidAuditAction reports whether name is one of AuditActions (or empty, meaning any).
|
||||
func ValidAuditAction(name string) bool {
|
||||
if name == "" {
|
||||
return true
|
||||
}
|
||||
for _, a := range AuditActions {
|
||||
if strings.EqualFold(a, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
+93
-1
@@ -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 {
|
||||
|
||||
@@ -123,10 +123,23 @@ var schema = []string{
|
||||
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 TABLE IF NOT EXISTS downlinks (
|
||||
ship_id BIGINT NOT NULL, day BIGINT NOT NULL, data {BLOB} NOT NULL,
|
||||
PRIMARY KEY (ship_id, day))`,
|
||||
// Downlinks kept before the history table existed: only the latest of each ship.
|
||||
`INSERT INTO downlinks (ship_id, day, data)
|
||||
SELECT id, downlink_day, downlink FROM ships WHERE downlink_day >= 0 AND downlink IS NOT NULL
|
||||
ON CONFLICT (ship_id, day) DO NOTHING`,
|
||||
`CREATE TABLE IF NOT EXISTS code_slots (
|
||||
player_id BIGINT NOT NULL REFERENCES players(id), slot INTEGER NOT NULL,
|
||||
name TEXT NOT NULL, source TEXT NOT NULL, updated_at TEXT NOT NULL,
|
||||
PRIMARY KEY (player_id, slot))`,
|
||||
`CREATE TABLE IF NOT EXISTS audit_log (
|
||||
id {ID}, at BIGINT NOT NULL, player_id BIGINT NOT NULL, action TEXT NOT NULL,
|
||||
ship_id BIGINT NOT NULL DEFAULT 0, slot INTEGER NOT NULL DEFAULT 0,
|
||||
detail TEXT NOT NULL DEFAULT '', ip TEXT NOT NULL DEFAULT '')`,
|
||||
`CREATE INDEX IF NOT EXISTS audit_player ON audit_log(player_id, id)`,
|
||||
`CREATE INDEX IF NOT EXISTS audit_action ON audit_log(action, id)`,
|
||||
`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 (
|
||||
|
||||
Reference in New Issue
Block a user