Complete player stats inventory and Control content editors

This commit is contained in:
dingo
2026-09-08 06:02:13 -04:00
parent 2df1f6ab38
commit b6ce3bc292
27 changed files with 960 additions and 267 deletions
+74 -35
View File
@@ -1,42 +1,81 @@
package transport
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"time"
"aeternalis/server/internal/store"
"aeternalis/server/internal/store"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"time"
)
func(h *HTTP) catalogRoutes(mux *http.ServeMux){
mux.HandleFunc("GET /api/control/catalog",func(w http.ResponseWriter,r *http.Request){
if _,ok:=h.admin(w,r);!ok{return}
ctx,cancel:=context.WithTimeout(r.Context(),5*time.Second);defer cancel()
catalog,err:=h.auth.db.Catalog(ctx);if err!=nil{fail(w,503,"catalog unavailable");return}
changes,err:=h.auth.db.CatalogChanges(ctx);if err!=nil{fail(w,503,"catalog audit unavailable");return}
respond(w,200,map[string]any{"catalog":catalog,"changes":changes})
})
mux.HandleFunc("POST /api/control/catalog",func(w http.ResponseWriter,r *http.Request){
actor,ok:=h.admin(w,r);if !ok{return}
var change store.CatalogChange
r.Body=http.MaxBytesReader(w,r.Body,32768)
decoder:=json.NewDecoder(r.Body);decoder.DisallowUnknownFields()
if err:=decoder.Decode(&change);err!=nil{fail(w,400,"invalid catalog change");return}
var extra any;if err:=decoder.Decode(&extra);err!=io.EOF{fail(w,400,"invalid catalog change");return}
ctx,cancel:=context.WithTimeout(r.Context(),5*time.Second);defer cancel()
result,err:=h.auth.db.PublishCatalog(ctx,actor,change)
switch{
case errors.Is(err,store.ErrCatalogConflict),errors.Is(err,store.ErrCatalogRequest):fail(w,409,err.Error())
case errors.Is(err,store.ErrCatalogInvalid):fail(w,400,err.Error())
case err!=nil:fail(w,503,"catalog publication unavailable")
default:respond(w,200,result)
}
})
func (h *HTTP) catalogRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/control/catalog", func(w http.ResponseWriter, r *http.Request) {
if _, ok := h.admin(w, r); !ok {
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
catalog, err := h.auth.db.Catalog(ctx)
if err != nil {
fail(w, 503, "catalog unavailable")
return
}
changes, err := h.auth.db.CatalogChanges(ctx)
if err != nil {
fail(w, 503, "catalog audit unavailable")
return
}
respond(w, 200, map[string]any{"catalog": catalog, "changes": changes})
})
mux.HandleFunc("POST /api/control/catalog", func(w http.ResponseWriter, r *http.Request) {
actor, ok := h.admin(w, r)
if !ok {
return
}
var change store.CatalogChange
r.Body = http.MaxBytesReader(w, r.Body, 32768)
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&change); err != nil {
fail(w, 400, "invalid catalog change")
return
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
fail(w, 400, "invalid catalog change")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
result, err := h.auth.db.PublishCatalog(ctx, actor, change)
switch {
case errors.Is(err, store.ErrCatalogConflict), errors.Is(err, store.ErrCatalogRequest):
fail(w, 409, err.Error())
case errors.Is(err, store.ErrCatalogInvalid):
fail(w, 400, err.Error())
case err != nil:
fail(w, 503, "catalog publication unavailable")
default:
respond(w, 200, result)
}
})
}
// Caller holds s.mu. Checkpoint a catalog before it can reach a snapshot.
func(s *Service) refreshCatalog(ctx context.Context)error{
ctx,cancel:=context.WithTimeout(ctx,5*time.Second);defer cancel()
catalog,err:=s.lease.DB.CatalogAfter(ctx,s.world.CatalogVersion);if err!=nil{return err};if catalog==nil{return nil}
if err=s.world.ApplyCatalog(*catalog);err!=nil{return err};return s.persist(ctx,"")
func (s *Service) refreshCatalog(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
catalog, err := s.lease.DB.CatalogAfter(ctx, s.world.CatalogVersion)
if err != nil {
return err
}
if catalog == nil {
return nil
}
if err = s.world.ApplyCatalog(*catalog); err != nil {
return err
}
return s.persist(ctx, "")
}
@@ -1,44 +1,103 @@
package transport
import (
"context"
"encoding/json"
"path/filepath"
"testing"
"time"
"aeternalis/server/content"
"aeternalis/server/internal/game"
"aeternalis/server/internal/store"
"aeternalis/server/content"
"aeternalis/server/internal/game"
"aeternalis/server/internal/store"
"context"
"encoding/json"
"path/filepath"
"testing"
"time"
)
func TestPublishedTitlesAndStatsReachDurableCharacterViews(t *testing.T) {
ctx:=context.Background()
db,err:=store.Open(ctx,"file:"+filepath.ToSlash(filepath.Join(t.TempDir(),"catalog-owner.db")));if err!=nil{t.Fatal(err)};defer db.Close()
b,err:=content.Load();if err!=nil{t.Fatal(err)}
w:=game.New("catalog-owner",23,time.Now(),b)
lease,_,err:=db.Acquire(ctx,w);if err!=nil{t.Fatal(err)}
svc:=NewService(w,lease)
initial,err:=db.Catalog(ctx);if err!=nil{t.Fatal(err)}
title:=content.Title{ID:"published_strength",Name:"Published Strength",Scope:"personal",Requirements:[]content.TitleRequirement{{Metric:"level",AtLeast:1}},Effects:content.TitleEffects{Modifiers:[]content.StatModifier{{Stat:"strength",Flat:4,Percent:50}}}}
result,err:=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:initial.Version,Op:"create-title",Reason:"Integration title",Title:&title});if err!=nil{t.Fatal(err)}
view,err:=svc.join(ctx,"catalog-player","Catalog Player","");if err!=nil{t.Fatal(err)}
if len(view.Titles)!=1||view.Titles[0].ID!=title.ID||view.Stats["strength"].Final!=6||view.Stats["meleeDamage"].Final!=24{t.Fatal("published title missing from committed join",view)}
c:=w.Characters[view.Entity.ID];c.View.Entity.HP=20;c.DailySeconds=321
rules:=initial.Stats;rules.Base["healthPoints"]=150
result,err=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:result.Version,Op:"update-stats",Reason:"Integration formula",Stats:&rules});if err!=nil{t.Fatal(err)}
if err=svc.controls(ctx);err!=nil{t.Fatal(err)}
record,err:=db.ActiveCharacter(ctx,c.Account);if err!=nil{t.Fatal(err)}
if record.Character.View.Entity.MaxHP!=150||record.Character.View.Entity.HP!=20||record.Character.DailySeconds!=321||record.Character.View.Titles[0].Effects.Modifiers[0].Flat!=4{t.Fatal("catalog changed health, budget or earned title")}
worlds,err:=db.Worlds(ctx);if err!=nil||worlds[0].CatalogVersion!=result.Version{t.Fatal("applied version not checkpointed",err)}
if err=lease.Release(ctx);err!=nil{t.Fatal(err)}
nextLease,raw,err:=db.Acquire(ctx,game.New(w.ID,23,time.Now(),b));if err!=nil{t.Fatal(err)};defer nextLease.Release(ctx)
var restored game.World;if err=json.Unmarshal(raw,&restored);err!=nil{t.Fatal(err)}
if err=restored.Restore(time.Now());err!=nil{t.Fatal(err)}
if restored.CatalogVersion!=result.Version||restored.Characters[c.View.Entity.ID].View.Stats["healthPoints"].Final!=150{t.Fatal("restart lost published stats")}
// Old ownership may no longer expose a speculative formula revision.
rules.Base["healthPoints"]=200
if _,err=db.PublishCatalog(ctx,"operator",store.CatalogChange{ID:game.ID(),ExpectedVersion:result.Version,Op:"update-stats",Reason:"Stale owner test",Stats:&rules});err!=nil{t.Fatal(err)}
if err=svc.refreshCatalog(ctx);err==nil||svc.ready.Load(){t.Fatal("stale owner continued after failed catalog checkpoint")}
record,err=db.ActiveCharacter(ctx,c.Account);if err!=nil{t.Fatal(err)}
if record.Character.View.Entity.MaxHP!=150{t.Fatal("failed checkpoint published speculative stats")}
ctx := context.Background()
db, err := store.Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "catalog-owner.db")))
if err != nil {
t.Fatal(err)
}
defer db.Close()
b, err := content.Load()
if err != nil {
t.Fatal(err)
}
w := game.New("catalog-owner", 23, time.Now(), b)
lease, _, err := db.Acquire(ctx, w)
if err != nil {
t.Fatal(err)
}
svc := NewService(w, lease)
initial, err := db.Catalog(ctx)
if err != nil {
t.Fatal(err)
}
title := content.Title{ID: "published_strength", Name: "Published Strength", Scope: "personal", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 1}}, Effects: content.TitleEffects{Modifiers: []content.StatModifier{{Stat: "strength", Flat: 4, Percent: 50}}}}
result, err := db.PublishCatalog(ctx, "operator", store.CatalogChange{ID: game.ID(), ExpectedVersion: initial.Version, Op: "create-title", Reason: "Integration title", Title: &title})
if err != nil {
t.Fatal(err)
}
view, err := svc.join(ctx, "catalog-player", "Catalog Player", "")
if err != nil {
t.Fatal(err)
}
if len(view.Titles) != 1 || view.Titles[0].ID != title.ID || view.Stats["strength"].Final != 6 || view.Stats["meleeDamage"].Final != 24 {
t.Fatal("published title missing from committed join", view)
}
c := w.Characters[view.Entity.ID]
c.View.Entity.HP = 20
c.DailySeconds = 321
rules := initial.Stats
rules.Base["healthPoints"] = 150
result, err = db.PublishCatalog(ctx, "operator", store.CatalogChange{ID: game.ID(), ExpectedVersion: result.Version, Op: "update-stats", Reason: "Integration formula", Stats: &rules})
if err != nil {
t.Fatal(err)
}
if err = svc.controls(ctx); err != nil {
t.Fatal(err)
}
record, err := db.ActiveCharacter(ctx, c.Account)
if err != nil {
t.Fatal(err)
}
if record.Character.View.Entity.MaxHP != 150 || record.Character.View.Entity.HP != 20 || record.Character.DailySeconds != 321 || record.Character.View.Titles[0].Effects.Modifiers[0].Flat != 4 {
t.Fatal("catalog changed health, budget or earned title")
}
worlds, err := db.Worlds(ctx)
if err != nil || worlds[0].CatalogVersion != result.Version {
t.Fatal("applied version not checkpointed", err)
}
if err = lease.Release(ctx); err != nil {
t.Fatal(err)
}
nextLease, raw, err := db.Acquire(ctx, game.New(w.ID, 23, time.Now(), b))
if err != nil {
t.Fatal(err)
}
defer nextLease.Release(ctx)
var restored game.World
if err = json.Unmarshal(raw, &restored); err != nil {
t.Fatal(err)
}
if err = restored.Restore(time.Now()); err != nil {
t.Fatal(err)
}
if restored.CatalogVersion != result.Version || restored.Characters[c.View.Entity.ID].View.Stats["healthPoints"].Final != 150 {
t.Fatal("restart lost published stats")
}
// Old ownership may no longer expose a speculative formula revision.
rules.Base["healthPoints"] = 200
if _, err = db.PublishCatalog(ctx, "operator", store.CatalogChange{ID: game.ID(), ExpectedVersion: result.Version, Op: "update-stats", Reason: "Stale owner test", Stats: &rules}); err != nil {
t.Fatal(err)
}
if err = svc.refreshCatalog(ctx); err == nil || svc.ready.Load() {
t.Fatal("stale owner continued after failed catalog checkpoint")
}
record, err = db.ActiveCharacter(ctx, c.Account)
if err != nil {
t.Fatal(err)
}
if record.Character.View.Entity.MaxHP != 150 {
t.Fatal("failed checkpoint published speculative stats")
}
}
+4 -1
View File
@@ -176,7 +176,10 @@ func (h *HTTP) Handler() http.Handler {
h.s.mu.Lock()
defer h.s.mu.Unlock()
if c := h.s.world.ForAccount(account); c != nil && active != nil && active.WorldID == h.s.world.ID && active.ID == c.View.Entity.ID {
if !h.s.ready.Load() { fail(w, 503, "world unavailable"); return }
if !h.s.ready.Load() {
fail(w, 503, "world unavailable")
return
}
respond(w, 200, map[string]any{"character": c.View, "world": h.s.world.Info()})
return
}