217 lines
6.5 KiB
Go
217 lines
6.5 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRevivalDurability(t *testing.T) {
|
|
testRevivalDurability(t, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "revive.db")))
|
|
}
|
|
func TestPostgresRevivalDurability(t *testing.T) {
|
|
dsn := os.Getenv("TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("TEST_DATABASE_URL not configured")
|
|
}
|
|
testRevivalDurability(t, dsn)
|
|
}
|
|
func testRevivalDurability(t *testing.T, dsn string) {
|
|
ctx := context.Background()
|
|
db, e := Open(ctx, dsn)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer db.Close()
|
|
b, _ := content.Load()
|
|
now := time.Now().UTC()
|
|
w := game.New("revive-"+game.ID(), 1, now, b)
|
|
l, _, e := db.Acquire(ctx, w)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c, _ := w.Join("victim-"+game.ID(), "Victim")
|
|
rescuer, _ := w.Join("rescuer-"+game.ID(), "Rescuer")
|
|
_ = w.Connect(rescuer.View.Entity.ID)
|
|
rescuer.View.Entity.Position = c.View.Entity.Position
|
|
rescuer.View.Inventory["revival_stone"] = 3
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Create an actual new-system death without ending the world.
|
|
c.View.Entity.HP = 0
|
|
w.Advance(now.Add(time.Second), 1)
|
|
corpse := c.RevivalCorpse
|
|
if corpse == "" {
|
|
t.Fatal("death missing corpse identity")
|
|
}
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.RevivalConsent(ctx, w, c.Account, c.View.Entity.ID, true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.RevivalConsent(ctx, w, rescuer.Account, c.View.Entity.ID, true); !errors.Is(e, ErrRevival) {
|
|
t.Fatal("rescuer set another account's consent", e)
|
|
}
|
|
before, _ := json.Marshal(w)
|
|
stale := *l
|
|
stale.Revision--
|
|
if e = stale.Revive(ctx, w, rescuer.Account, corpse, "item"); !errors.Is(e, ErrOwnership) {
|
|
t.Fatal("stale revival not fenced", e)
|
|
}
|
|
after, _ := json.Marshal(w)
|
|
if string(before) != string(after) {
|
|
t.Fatal("failed revival mutated live world")
|
|
}
|
|
if e = l.Revive(ctx, w, rescuer.Account, corpse, "item"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if c.View.Entity.State != "alive" || rescuer.View.Inventory["revival_stone"] != 2 {
|
|
t.Fatal("revival not applied")
|
|
}
|
|
if e = l.Revive(ctx, w, rescuer.Account, corpse, "item"); !errors.Is(e, ErrRevival) {
|
|
t.Fatal("duplicate revival accepted", e)
|
|
}
|
|
// Death events survive revival and restart; repeats create distinct corpses.
|
|
var count int
|
|
if e = db.sql.QueryRowContext(ctx, db.q(`SELECT COUNT(*) FROM character_deaths WHERE character_id=? AND revived=1`), c.View.Entity.ID).Scan(&count); e != nil || count != 1 {
|
|
t.Fatal("death history missing", e)
|
|
}
|
|
c.View.Entity.HP = 0
|
|
w.Advance(now.Add(2*time.Second), 1)
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.RevivalConsent(ctx, w, c.Account, c.View.Entity.ID, true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
corpse = c.RevivalCorpse
|
|
other := game.New("replacement-"+game.ID(), 2, now, b)
|
|
ol, _, e := db.Acquire(ctx, other)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
replacement, e := other.Join(c.Account, "Replacement")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Simulate an old world binary inserting a replacement during rolling upgrade.
|
|
replacementJSON, _ := json.Marshal(replacement)
|
|
if _, e = db.sql.ExecContext(ctx, db.q(`INSERT INTO character_records(character_id,account_id,world_id,name,state,character) VALUES(?,?,?,?,?,?)`), replacement.View.Entity.ID, replacement.Account, other.ID, "Replacement", "alive", string(replacementJSON)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Even killing the replacement cannot restore eligibility for the old life.
|
|
replacement.View.Entity.HP = 0
|
|
other.Advance(now.Add(time.Second), 1)
|
|
if e = ol.Save(ctx, other, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.Revive(ctx, w, rescuer.Account, corpse, "item"); !errors.Is(e, ErrRevival) {
|
|
t.Fatal("replacement death re-enabled old revival", e)
|
|
}
|
|
if c.View.Entity.State != "dead" || rescuer.View.Inventory["revival_stone"] != 2 || w.Corpses[corpse] == nil {
|
|
t.Fatal("rejected old revival spent or moved items")
|
|
}
|
|
if e = l.Release(ctx); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
restored := game.New(w.ID, 1, now, b)
|
|
var raw []byte
|
|
l, raw, e = db.Acquire(ctx, restored)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = json.Unmarshal(raw, restored); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = restored.Restore(now.Add(3 * time.Second)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_ = restored.Connect(rescuer.View.Entity.ID)
|
|
if e = l.Revive(ctx, restored, rescuer.Account, corpse, "item"); !errors.Is(e, ErrRevival) {
|
|
t.Fatal("restart lost finality", e)
|
|
}
|
|
testRevivalRace(t, db)
|
|
}
|
|
|
|
func testRevivalRace(t *testing.T, db *DB) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
b, _ := content.Load()
|
|
now := time.Now().UTC()
|
|
w := game.New("race-source-"+game.ID(), 1, now, b)
|
|
l, _, e := db.Acquire(ctx, w)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
victim, _ := w.Join("race-victim-"+game.ID(), "Victim")
|
|
rescuer, _ := w.Join("race-rescuer-"+game.ID(), "Rescuer")
|
|
_ = w.Connect(rescuer.View.Entity.ID)
|
|
rescuer.View.Entity.Position = victim.View.Entity.Position
|
|
rescuer.View.Inventory["revival_stone"] = 1
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
victim.View.Entity.HP = 0
|
|
w.Advance(now.Add(time.Second), 1)
|
|
if e = l.Save(ctx, w, ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = l.RevivalConsent(ctx, w, victim.Account, victim.View.Entity.ID, true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
next := game.New("race-next-"+game.ID(), 2, now, b)
|
|
nl, _, e := db.Acquire(ctx, next)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
replacement, e := next.Join(victim.Account, "Replacement")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
type outcome struct {
|
|
revive bool
|
|
err error
|
|
}
|
|
done := make(chan outcome, 2)
|
|
start := make(chan struct{})
|
|
go func() {
|
|
<-start
|
|
done <- outcome{true, l.Revive(ctx, w, rescuer.Account, victim.RevivalCorpse, "item")}
|
|
}()
|
|
go func() { <-start; done <- outcome{false, nl.Save(ctx, next, "")} }()
|
|
close(start)
|
|
wins := 0
|
|
revived := false
|
|
for i := 0; i < 2; i++ {
|
|
r := <-done
|
|
if r.err == nil {
|
|
wins++
|
|
revived = r.revive
|
|
} else if !errors.Is(r.err, ErrRevival) && !errors.Is(r.err, ErrLivingCharacter) {
|
|
t.Fatal("unexpected race failure", r.err)
|
|
}
|
|
}
|
|
if wins != 1 {
|
|
t.Fatal("replacement and revival did not produce exactly one winner")
|
|
}
|
|
active, e := db.ActiveCharacter(ctx, victim.Account)
|
|
if e != nil || active == nil {
|
|
t.Fatal("race lost active life", e)
|
|
}
|
|
if revived {
|
|
if active.ID != victim.View.Entity.ID || rescuer.View.Inventory["revival_stone"] != 0 {
|
|
t.Fatal("revival winner lost identity or cost")
|
|
}
|
|
} else if active.ID != replacement.View.Entity.ID || rescuer.View.Inventory["revival_stone"] != 1 {
|
|
t.Fatal("replacement winner spent revival cost")
|
|
}
|
|
}
|