Add persistent dark mode and server-assigned content IDs
This commit is contained in:
@@ -5,6 +5,7 @@ type Protocol struct {
|
||||
Catalog *ContentCatalog `json:"catalog,omitempty"`
|
||||
CatalogChange *ContentChange `json:"catalogChange,omitempty"`
|
||||
CatalogPreview *ContentPreview `json:"catalogPreview,omitempty"`
|
||||
CatalogResult *ContentResult `json:"catalogResult,omitempty"`
|
||||
Intent Intent `json:"intent"`
|
||||
Snapshot Snapshot `json:"snapshot"`
|
||||
}
|
||||
@@ -82,17 +83,51 @@ type TitleEffects struct {
|
||||
}
|
||||
|
||||
type ContentChange struct {
|
||||
EffectSet *EffectSet `json:"effectSet,omitempty"`
|
||||
EffectSetIDS []string `json:"effectSetIds,omitempty"`
|
||||
ExpectedVersion int64 `json:"expectedVersion"`
|
||||
ID string `json:"id"`
|
||||
Item *ItemDefinition `json:"item,omitempty"`
|
||||
Op string `json:"op"`
|
||||
Reason string `json:"reason"`
|
||||
RequirementSet *RequirementSet `json:"requirementSet,omitempty"`
|
||||
RequirementSetIDS []string `json:"requirementSetIds,omitempty"`
|
||||
Stats *ContentStatsRules `json:"stats,omitempty"`
|
||||
Title *TitleDefinition `json:"title,omitempty"`
|
||||
EffectSet *EffectSetDraft `json:"effectSet,omitempty"`
|
||||
EffectSetIDS []string `json:"effectSetIds,omitempty"`
|
||||
ExpectedVersion int64 `json:"expectedVersion"`
|
||||
ID string `json:"id"`
|
||||
Item *ItemDraft `json:"item,omitempty"`
|
||||
Op string `json:"op"`
|
||||
Reason string `json:"reason"`
|
||||
RequirementSet *RequirementSetDraft `json:"requirementSet,omitempty"`
|
||||
RequirementSetIDS []string `json:"requirementSetIds,omitempty"`
|
||||
Stats *ContentStatsRules `json:"stats,omitempty"`
|
||||
Title *TitleDraft `json:"title,omitempty"`
|
||||
}
|
||||
|
||||
type EffectSetDraft struct {
|
||||
Description string `json:"description"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Modifiers []StatModifier `json:"modifiers"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ItemDraft struct {
|
||||
Description string `json:"description"`
|
||||
DropChance float64 `json:"dropChance"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Modifiers []StatModifier `json:"modifiers"`
|
||||
Name string `json:"name"`
|
||||
Requirements ItemRequirements `json:"requirements"`
|
||||
Slot string `json:"slot"`
|
||||
Weight float64 `json:"weight"`
|
||||
}
|
||||
|
||||
type RequirementSetDraft struct {
|
||||
Description string `json:"description"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Requirements []ContentRequirement `json:"requirements"`
|
||||
}
|
||||
|
||||
type TitleDraft struct {
|
||||
Description string `json:"description"`
|
||||
Effects TitleEffects `json:"effects"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Requirements []ContentRequirement `json:"requirements"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
type ContentPreview struct {
|
||||
@@ -101,6 +136,11 @@ type ContentPreview struct {
|
||||
Resolved ContentChange `json:"resolved"`
|
||||
}
|
||||
|
||||
type ContentResult struct {
|
||||
ID string `json:"id"`
|
||||
Version int64 `json:"version"`
|
||||
}
|
||||
|
||||
type Intent struct {
|
||||
Op string `json:"op"`
|
||||
Seq int64 `json:"seq"`
|
||||
|
||||
@@ -30,8 +30,16 @@ type CatalogChange struct {
|
||||
EffectSetIDs []string `json:"effectSetIds,omitempty"`
|
||||
}
|
||||
type CatalogResult struct {
|
||||
Version int64 `json:"version"`
|
||||
Version int64 `json:"version"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// Explicit definition IDs are reserved for trusted seed/import code. Browser
|
||||
// publications omit them; the server assigns an ID after resolving request replay.
|
||||
func (c CatalogChange) HasDefinitionID() bool {
|
||||
return (c.Title != nil && c.Title.ID != "") || (c.Item != nil && c.Item.ID != "") || (c.RequirementSet != nil && c.RequirementSet.ID != "") || (c.EffectSet != nil && c.EffectSet.ID != "")
|
||||
}
|
||||
|
||||
type CatalogAudit struct {
|
||||
ID string `json:"id"`
|
||||
Actor string `json:"actor"`
|
||||
@@ -168,7 +176,7 @@ func (d *DB) PublishCatalog(ctx context.Context, actor string, change CatalogCha
|
||||
}
|
||||
var previousActor, previousRequest string
|
||||
var result CatalogResult
|
||||
err = tx.QueryRowContext(ctx, d.q(`SELECT actor,request_json,result_version FROM catalog_changes WHERE id=?`), change.ID).Scan(&previousActor, &previousRequest, &result.Version)
|
||||
err = tx.QueryRowContext(ctx, d.q(`SELECT actor,request_json,result_version,target FROM catalog_changes WHERE id=?`), change.ID).Scan(&previousActor, &previousRequest, &result.Version, &result.ID)
|
||||
if err == nil {
|
||||
if previousActor != actor || previousRequest != string(request) {
|
||||
return CatalogResult{}, ErrCatalogRequest
|
||||
@@ -207,7 +215,7 @@ func (d *DB) PublishCatalog(ctx context.Context, actor string, change CatalogCha
|
||||
if err = tx.Commit(); err != nil {
|
||||
return CatalogResult{}, err
|
||||
}
|
||||
return CatalogResult{Version: catalog.Version}, nil
|
||||
return CatalogResult{Version: catalog.Version, ID: target}, nil
|
||||
}
|
||||
func (d *DB) CatalogChanges(ctx context.Context) ([]CatalogAudit, error) {
|
||||
rows, err := d.sql.QueryContext(ctx, `SELECT id,actor,op,target,reason,result_version,created_at FROM catalog_changes ORDER BY result_version DESC LIMIT 100`)
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"aeternalis/server/internal/game"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServerAssignedCatalogIDsSurviveReplayAndRestart(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dsn := "file:" + filepath.ToSlash(filepath.Join(t.TempDir(), "ids.db"))
|
||||
db, err := Open(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
catalog, err := db.Catalog(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var last CatalogChange
|
||||
var lastResult CatalogResult
|
||||
for _, draft := range []CatalogChange{
|
||||
{Op: "create-requirement-set", RequirementSet: &content.RequirementSet{Name: "Same name", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 1}}}},
|
||||
{Op: "create-effect-set", EffectSet: &content.EffectSet{Name: "Same name", Modifiers: []content.StatModifier{{Stat: "defence", Flat: 2}}}},
|
||||
{Op: "create-title", Title: &content.Title{Name: "Same name", Scope: "personal", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 2}}}},
|
||||
{Op: "create-item", Item: &content.ItemDefinition{Name: "Same name", Slot: "ring", Requirements: content.ItemRequirements{Level: 1}}},
|
||||
} {
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
draft.ID = game.ID()
|
||||
draft.ExpectedVersion = catalog.Version
|
||||
draft.Reason = "Server identity verification"
|
||||
before, _ := json.Marshal(draft)
|
||||
preview, err := db.PreviewCatalog(ctx, draft)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if preview.Resolved.HasDefinitionID() {
|
||||
t.Fatal("preview reserved or exposed a permanent ID")
|
||||
}
|
||||
after, _ := json.Marshal(draft)
|
||||
if string(before) != string(after) {
|
||||
t.Fatal("preview mutated submitted draft")
|
||||
}
|
||||
unchanged, err := db.Catalog(ctx)
|
||||
if err != nil || unchanged.Version != catalog.Version {
|
||||
t.Fatal("preview changed version", err)
|
||||
}
|
||||
result, err := db.PublishCatalog(ctx, "operator", draft)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.ID == "" || seen[result.ID] {
|
||||
t.Fatal("missing or reused generated ID", result)
|
||||
}
|
||||
seen[result.ID] = true
|
||||
replay, err := db.PublishCatalog(ctx, "operator", draft)
|
||||
if err != nil || replay != result {
|
||||
t.Fatal("retry created a different identity", err)
|
||||
}
|
||||
if _, err = db.PublishCatalog(ctx, "other", draft); !errors.Is(err, ErrCatalogRequest) {
|
||||
t.Fatal("different actor replay accepted", err)
|
||||
}
|
||||
catalog, err = db.Catalog(ctx)
|
||||
if err != nil || catalog.Version != result.Version {
|
||||
t.Fatal("version mismatch", err)
|
||||
}
|
||||
last, lastResult = draft, result
|
||||
}
|
||||
}
|
||||
if err = db.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reopened, err := Open(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
result, err := reopened.PublishCatalog(ctx, "operator", last)
|
||||
if err != nil || result != lastResult {
|
||||
t.Fatal("restart changed replay identity", err)
|
||||
}
|
||||
audits, err := reopened.CatalogChanges(ctx)
|
||||
if err != nil || len(audits) != 8 || audits[0].Target != lastResult.ID {
|
||||
t.Fatal("generated ID not persisted in audit", err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"aeternalis/server/internal/game"
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
@@ -32,6 +33,21 @@ func (d *DB) PreviewCatalog(ctx context.Context, change CatalogChange) (CatalogP
|
||||
if err != nil {
|
||||
return CatalogPreview{}, err
|
||||
}
|
||||
// Preview validates a provisional identity but does not reserve or expose it.
|
||||
if !change.HasDefinitionID() {
|
||||
if resolved.Title != nil {
|
||||
resolved.Title.ID = ""
|
||||
}
|
||||
if resolved.Item != nil {
|
||||
resolved.Item.ID = ""
|
||||
}
|
||||
if resolved.RequirementSet != nil {
|
||||
resolved.RequirementSet.ID = ""
|
||||
}
|
||||
if resolved.EffectSet != nil {
|
||||
resolved.EffectSet.ID = ""
|
||||
}
|
||||
}
|
||||
return CatalogPreview{BasedOnVersion: c.Version, ProposedVersion: next.Version, Resolved: resolved}, nil
|
||||
}
|
||||
|
||||
@@ -51,6 +67,9 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
switch change.Op {
|
||||
case "create-title":
|
||||
title := *change.Title
|
||||
if title.ID == "" {
|
||||
title.ID = "title_" + game.ID()
|
||||
}
|
||||
if len(title.Requirements) > 8 {
|
||||
return invalid(fmt.Errorf("at most eight inline requirements are allowed"))
|
||||
}
|
||||
@@ -73,6 +92,9 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
target = title.ID
|
||||
case "create-item":
|
||||
item := *change.Item
|
||||
if item.ID == "" {
|
||||
item.ID = "item_" + game.ID()
|
||||
}
|
||||
// Validate inline values before merging, so sets cannot mask invalid input.
|
||||
if err := item.Validate(); err != nil {
|
||||
return invalid(err)
|
||||
@@ -97,11 +119,21 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
case "update-stats":
|
||||
c.Stats = *change.Stats
|
||||
case "create-requirement-set":
|
||||
c.RequirementSets = append(slices.Clone(c.RequirementSets), *change.RequirementSet)
|
||||
target = change.RequirementSet.ID
|
||||
set := *change.RequirementSet
|
||||
if set.ID == "" {
|
||||
set.ID = "requirement_" + game.ID()
|
||||
}
|
||||
resolved.RequirementSet = &set
|
||||
c.RequirementSets = append(slices.Clone(c.RequirementSets), set)
|
||||
target = set.ID
|
||||
case "create-effect-set":
|
||||
c.EffectSets = append(slices.Clone(c.EffectSets), *change.EffectSet)
|
||||
target = change.EffectSet.ID
|
||||
set := *change.EffectSet
|
||||
if set.ID == "" {
|
||||
set.ID = "effect_" + game.ID()
|
||||
}
|
||||
resolved.EffectSet = &set
|
||||
c.EffectSets = append(slices.Clone(c.EffectSets), set)
|
||||
target = set.ID
|
||||
}
|
||||
c.Version++
|
||||
if err := c.Validate(); err != nil {
|
||||
|
||||
@@ -48,6 +48,10 @@ func (h *HTTP) catalogRoutes(mux *http.ServeMux) {
|
||||
fail(w, 400, "invalid catalog change")
|
||||
return
|
||||
}
|
||||
if change.HasDefinitionID() {
|
||||
fail(w, 400, "Content IDs are assigned by the server. Refresh the editor and omit the definition ID.")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
var result any
|
||||
|
||||
@@ -80,7 +80,12 @@ func TestControlAuthorizationAndExecution(t *testing.T) {
|
||||
if request("GET", "/api/control", "", token, "") != 200 {
|
||||
t.Fatal("admin denied")
|
||||
}
|
||||
previewBody := `{"id":"` + game.ID() + `","expectedVersion":1,"op":"create-effect-set","reason":"Preview effects","effectSet":{"id":"test_effect","name":"Test effect","description":"","modifiers":[{"stat":"strength","flat":2,"percent":0}]}}`
|
||||
previewBody := `{"id":"` + game.ID() + `","expectedVersion":1,"op":"create-effect-set","reason":"Preview effects","effectSet":{"name":"Test effect","description":"","modifiers":[{"stat":"strength","flat":2,"percent":0}]}}`
|
||||
for _, path := range []string{"/api/control/catalog", "/api/control/catalog/preview"} {
|
||||
if request("POST", path, strings.Replace(previewBody, `"effectSet":{`, `"effectSet":{"id":"client_chosen",`, 1), token, server.URL) != 400 {
|
||||
t.Fatal("client selected a permanent content ID")
|
||||
}
|
||||
}
|
||||
if request("POST", "/api/control/catalog/preview", previewBody, token, "https://evil.example") != 403 {
|
||||
t.Fatal("cross-origin preview")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user