220 lines
4.9 KiB
Go
220 lines
4.9 KiB
Go
// Package scores persists pilots, every completed run, and their personal bests.
|
|
package scores
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"thermal-snake/internal/game"
|
|
)
|
|
|
|
var (
|
|
pilotsKey = []byte("pilots")
|
|
runsKey = []byte("runs")
|
|
recordedKey = []byte("recorded")
|
|
)
|
|
|
|
type Store struct{ db *bbolt.DB }
|
|
|
|
type Pilot struct {
|
|
Callsign string
|
|
Best *Run
|
|
Total int
|
|
}
|
|
|
|
type Run struct {
|
|
ID string
|
|
Score int
|
|
Heat int
|
|
Outcome game.Outcome
|
|
Finished time.Time
|
|
Order uint64
|
|
}
|
|
|
|
type Standing struct {
|
|
Rank int
|
|
Pilot
|
|
}
|
|
|
|
func Open(path string) (*Store, error) {
|
|
db, err := bbolt.Open(path, 0600, &bbolt.Options{Timeout: time.Second})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = db.Update(func(tx *bbolt.Tx) error {
|
|
for _, name := range [][]byte{pilotsKey, runsKey, recordedKey} {
|
|
if _, err := tx.CreateBucketIfNotExists(name); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
_ = db.Close()
|
|
return nil, err
|
|
}
|
|
return &Store{db: db}, nil
|
|
}
|
|
|
|
func (s *Store) Close() error { return s.db.Close() }
|
|
|
|
// Register assigns a unique display name to a verified public key fingerprint.
|
|
func (s *Store) Register(fingerprint string) (Pilot, error) {
|
|
var pilot Pilot
|
|
err := s.db.Update(func(tx *bbolt.Tx) error {
|
|
pilots := tx.Bucket(pilotsKey)
|
|
key := []byte(fingerprint)
|
|
if raw := pilots.Get(key); raw != nil {
|
|
return json.Unmarshal(raw, &pilot)
|
|
}
|
|
number, err := pilots.NextSequence()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pilot.Callsign = fmt.Sprintf("PILOT-%04d", number)
|
|
return put(pilots, key, pilot)
|
|
})
|
|
return pilot, err
|
|
}
|
|
|
|
func (s *Store) Profile(fingerprint string) (Pilot, error) {
|
|
var pilot Pilot
|
|
err := s.db.View(func(tx *bbolt.Tx) error {
|
|
var err error
|
|
pilot, err = readPilot(tx.Bucket(pilotsKey), []byte(fingerprint))
|
|
return err
|
|
})
|
|
return pilot, err
|
|
}
|
|
|
|
// Record writes the run, its history entry, and any new best in one transaction.
|
|
// Retrying the same ID is safe even when several sessions share a pilot.
|
|
func (s *Store) Record(fingerprint string, run Run) (Pilot, bool, error) {
|
|
var pilot Pilot
|
|
var personalBest bool
|
|
err := s.db.Update(func(tx *bbolt.Tx) error {
|
|
pilots := tx.Bucket(pilotsKey)
|
|
key := []byte(fingerprint)
|
|
var err error
|
|
pilot, err = readPilot(pilots, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
allRuns := tx.Bucket(runsKey)
|
|
recorded := tx.Bucket(recordedKey)
|
|
id := []byte(fingerprint + "/" + run.ID)
|
|
if recorded.Get(id) != nil {
|
|
personalBest = pilot.Best != nil && pilot.Best.ID == run.ID
|
|
return nil
|
|
}
|
|
|
|
run.Order, err = allRuns.NextSequence()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
history, err := allRuns.CreateBucketIfNotExists(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
order := binary.BigEndian.AppendUint64(nil, run.Order)
|
|
if err := put(history, order, run); err != nil {
|
|
return err
|
|
}
|
|
if err := recorded.Put(id, order); err != nil {
|
|
return err
|
|
}
|
|
pilot.Total++
|
|
personalBest = pilot.Best == nil || run.Score > pilot.Best.Score
|
|
if personalBest {
|
|
pilot.Best = &run
|
|
}
|
|
return put(pilots, key, pilot)
|
|
})
|
|
return pilot, personalBest, err
|
|
}
|
|
|
|
func (s *Store) Leaderboard() ([]Standing, error) {
|
|
var standings []Standing
|
|
err := s.db.View(func(tx *bbolt.Tx) error {
|
|
return tx.Bucket(pilotsKey).ForEach(func(_, raw []byte) error {
|
|
var pilot Pilot
|
|
if err := json.Unmarshal(raw, &pilot); err != nil {
|
|
return err
|
|
}
|
|
if pilot.Best != nil {
|
|
standings = append(standings, Standing{Pilot: pilot})
|
|
}
|
|
return nil
|
|
})
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Slice(standings, func(i, j int) bool {
|
|
left, right := standings[i].Best, standings[j].Best
|
|
if left.Score != right.Score {
|
|
return left.Score > right.Score
|
|
}
|
|
return left.Order < right.Order
|
|
})
|
|
for i := range standings {
|
|
standings[i].Rank = i + 1
|
|
}
|
|
return standings, nil
|
|
}
|
|
|
|
func (s *Store) History(fingerprint string, offset, limit int) ([]Run, int, error) {
|
|
var runs []Run
|
|
var total int
|
|
err := s.db.View(func(tx *bbolt.Tx) error {
|
|
key := []byte(fingerprint)
|
|
pilot, err := readPilot(tx.Bucket(pilotsKey), key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
total = pilot.Total
|
|
if offset >= total || limit <= 0 {
|
|
return nil
|
|
}
|
|
history := tx.Bucket(runsKey).Bucket(key)
|
|
cursor := history.Cursor()
|
|
k, v := cursor.Last()
|
|
for range offset {
|
|
k, v = cursor.Prev()
|
|
}
|
|
for k != nil && len(runs) < limit {
|
|
var run Run
|
|
if err := json.Unmarshal(v, &run); err != nil {
|
|
return err
|
|
}
|
|
runs = append(runs, run)
|
|
k, v = cursor.Prev()
|
|
}
|
|
return nil
|
|
})
|
|
return runs, total, err
|
|
}
|
|
|
|
func readPilot(bucket *bbolt.Bucket, key []byte) (Pilot, error) {
|
|
var pilot Pilot
|
|
raw := bucket.Get(key)
|
|
if raw == nil {
|
|
return pilot, fmt.Errorf("pilot is not registered")
|
|
}
|
|
err := json.Unmarshal(raw, &pilot)
|
|
return pilot, err
|
|
}
|
|
|
|
func put(bucket *bbolt.Bucket, key []byte, value any) error {
|
|
raw, err := json.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return bucket.Put(key, raw)
|
|
}
|