Add biome revisions readable content and visible projectile outcomes
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"aeternalis/server/internal/game"
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBiomeRevisionsReuseResourcesAndRejectStaleEdits(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "biomes.db")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
initial, err := db.Catalog(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
draft := CatalogChange{ID: game.ID(), ExpectedVersion: initial.Version, Reason: "Reuse existing trees", Op: "create-biome", Biome: &content.BiomeDefinition{Name: "Wooded valley", Color: "#445566", Weight: 2, Resources: []string{"tree"}}}
|
||||
made, err := db.PublishCatalog(ctx, "admin", draft)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
edit := CatalogChange{ID: game.ID(), ExpectedVersion: made.Version, Reason: "Add existing ore", Op: "update-biome", TargetID: made.ID, Biome: &content.BiomeDefinition{Name: "Wooded valley", Color: "#334455", Weight: 3, Resources: []string{"tree", "ore"}}}
|
||||
preview, err := db.PreviewCatalog(ctx, edit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if preview.Resolved.Biome.ID != made.ID || preview.Resolved.Biome.Revision != 2 {
|
||||
t.Fatal("preview replaced permanent identity")
|
||||
}
|
||||
unchanged, _ := db.Catalog(ctx)
|
||||
if unchanged.Version != made.Version || len(unchanged.Gathering.BiomeHistory) != 0 {
|
||||
t.Fatal("preview mutated history")
|
||||
}
|
||||
revised, err := db.PublishCatalog(ctx, "admin", edit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err := db.PublishCatalog(ctx, "admin", edit)
|
||||
if err != nil || replay != revised {
|
||||
t.Fatal("retry changed revision", err)
|
||||
}
|
||||
current, err := db.Catalog(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(current.Gathering.Resources) != len(initial.Gathering.Resources) || len(current.Gathering.Biomes) != len(initial.Gathering.Biomes)+1 {
|
||||
t.Fatal("revision duplicated resource or biome")
|
||||
}
|
||||
if len(current.Gathering.BiomeHistory) != 1 || current.Gathering.BiomeHistory[0].Revision != 1 || len(current.Gathering.BiomeHistory[0].Resources) != 1 {
|
||||
t.Fatal("old revision not preserved")
|
||||
}
|
||||
edit.ID = game.ID()
|
||||
if _, err = db.PublishCatalog(ctx, "admin", edit); !errors.Is(err, ErrCatalogConflict) {
|
||||
t.Fatal("stale edit accepted", err)
|
||||
}
|
||||
edit.ExpectedVersion = current.Version
|
||||
edit.Biome.Resources = []string{"missing"}
|
||||
if _, err = db.PublishCatalog(ctx, "admin", edit); !errors.Is(err, ErrCatalogInvalid) {
|
||||
t.Fatal("unknown resource accepted", err)
|
||||
}
|
||||
edit.ID = game.ID()
|
||||
edit.Biome.Resources = []string{}
|
||||
if _, err = db.PublishCatalog(ctx, "admin", edit); err != nil {
|
||||
t.Fatal("empty biome rejected", err)
|
||||
}
|
||||
current, _ = db.Catalog(ctx)
|
||||
for _, b := range current.Gathering.Biomes {
|
||||
if b.ID == made.ID && (b.Resources == nil || len(b.Resources) != 0 || b.Revision != 3) {
|
||||
t.Fatal("empty resource selection lost on save")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ var ErrCatalogInvalid = errors.New("invalid catalog change")
|
||||
var catalogRequestID = regexp.MustCompile(`^[a-f0-9-]{32,36}$`)
|
||||
|
||||
type CatalogChange struct {
|
||||
TargetID string `json:"targetId,omitempty"`
|
||||
Attack *content.CombatAttack `json:"attack,omitempty"`
|
||||
Mob *content.MobDefinition `json:"mob,omitempty"`
|
||||
Loot *content.LootTable `json:"loot,omitempty"`
|
||||
@@ -141,6 +142,12 @@ func (d *DB) CatalogAfter(ctx context.Context, after int64) (*content.Catalog, e
|
||||
return &catalog, err
|
||||
}
|
||||
func (c CatalogChange) validate() error {
|
||||
if (c.Op == "update-biome") != (c.TargetID != "") {
|
||||
return fmt.Errorf("%w: only biome edits require an existing target", ErrCatalogInvalid)
|
||||
}
|
||||
if c.Biome != nil && c.Biome.Revision != 0 {
|
||||
return fmt.Errorf("%w: revisions are assigned by the server", ErrCatalogInvalid)
|
||||
}
|
||||
if !catalogRequestID.MatchString(c.ID) || c.ExpectedVersion < 1 || len(c.Reason) < 5 || len(c.Reason) > 240 {
|
||||
return fmt.Errorf("%w: request ID, current version and 5-240 character reason required", ErrCatalogInvalid)
|
||||
}
|
||||
@@ -166,6 +173,8 @@ func (c CatalogChange) validate() error {
|
||||
valid = c.Resource != nil
|
||||
case "create-biome":
|
||||
valid = c.Biome != nil
|
||||
case "update-biome":
|
||||
valid = c.Biome != nil && c.Biome.ID == ""
|
||||
case "create-consumable":
|
||||
valid = c.Consumable != nil
|
||||
case "create-title":
|
||||
|
||||
@@ -53,7 +53,7 @@ func (d *DB) PreviewCatalog(ctx context.Context, change CatalogChange) (CatalogP
|
||||
if resolved.Resource != nil {
|
||||
resolved.Resource.ID = ""
|
||||
}
|
||||
if resolved.Biome != nil {
|
||||
if resolved.Biome != nil && change.Op != "update-biome" {
|
||||
resolved.Biome.ID = ""
|
||||
}
|
||||
if resolved.Consumable != nil {
|
||||
@@ -153,16 +153,44 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
v.ID = "resource_" + game.ID()
|
||||
}
|
||||
resolved.Resource = &v
|
||||
for i, b := range c.Gathering.Biomes {
|
||||
if slices.Contains(v.Biomes, b.ID) {
|
||||
if b.Resources == nil {
|
||||
b.Resources = []string{}
|
||||
for _, r := range c.Gathering.Resources {
|
||||
if slices.Contains(r.Biomes, b.ID) {
|
||||
b.Resources = append(b.Resources, r.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
b.Resources = append(slices.Clone(b.Resources), v.ID)
|
||||
reviseBiome(c.Gathering, i, b)
|
||||
}
|
||||
}
|
||||
c.Gathering.Resources = append(slices.Clone(c.Gathering.Resources), v)
|
||||
target = v.ID
|
||||
case "create-biome":
|
||||
v := *change.Biome
|
||||
v.Revision = 1
|
||||
if v.ID == "" {
|
||||
v.ID = "biome_" + game.ID()
|
||||
}
|
||||
resolved.Biome = &v
|
||||
c.Gathering.Biomes = append(slices.Clone(c.Gathering.Biomes), v)
|
||||
target = v.ID
|
||||
case "update-biome":
|
||||
i := slices.IndexFunc(c.Gathering.Biomes, func(b content.BiomeDefinition) bool { return b.ID == change.TargetID })
|
||||
if i < 0 {
|
||||
return invalid(fmt.Errorf("biome not found"))
|
||||
}
|
||||
v := *change.Biome
|
||||
if v.Resources == nil {
|
||||
return invalid(fmt.Errorf("select biome resources, or an empty list"))
|
||||
}
|
||||
reviseBiome(c.Gathering, i, v)
|
||||
v = c.Gathering.Biomes[i]
|
||||
resolved.Biome = &v
|
||||
target = v.ID
|
||||
case "create-consumable":
|
||||
v := *change.Consumable
|
||||
if v.ID == "" {
|
||||
@@ -247,3 +275,20 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
}
|
||||
return c, target, resolved, nil
|
||||
}
|
||||
|
||||
func reviseBiome(g *content.GatheringContent, index int, next content.BiomeDefinition) {
|
||||
previous := g.Biomes[index]
|
||||
previous.Revision = max(1, previous.Revision)
|
||||
if previous.Resources == nil {
|
||||
previous.Resources = []string{}
|
||||
for _, r := range g.Resources {
|
||||
if slices.Contains(r.Biomes, previous.ID) {
|
||||
previous.Resources = append(previous.Resources, r.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
g.BiomeHistory = append(slices.Clone(g.BiomeHistory), previous)
|
||||
next.ID, next.Revision = previous.ID, previous.Revision+1
|
||||
g.Biomes = slices.Clone(g.Biomes)
|
||||
g.Biomes[index] = next
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user