Add biome revisions readable content and visible projectile outcomes
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBiomeResourceRevisionPreservesExploredChunks(t *testing.T) {
|
||||
w, _ := fixture(t)
|
||||
g := w.Balance.Gathering
|
||||
g.Biomes = []content.BiomeDefinition{{ID: "valley", Name: "Valley", Color: "#445566", Weight: 1, Revision: 1, Resources: []string{"tree"}}}
|
||||
before := w.chunk(2, 2)
|
||||
if len(before.Resources) == 0 {
|
||||
t.Fatal("existing tree could not be reused in a new biome")
|
||||
}
|
||||
for _, r := range before.Resources {
|
||||
if stringValue(r.ResourceID) != "tree" {
|
||||
t.Fatal("unselected resource generated")
|
||||
}
|
||||
}
|
||||
g.Biomes[0].Revision = 2
|
||||
g.Biomes[0].Resources = []string{}
|
||||
delete(w.chunks, before.Key)
|
||||
after := w.chunk(2, 2)
|
||||
if !reflect.DeepEqual(before, after) {
|
||||
t.Fatal("biome revision rewrote an explored chunk")
|
||||
}
|
||||
if len(w.chunk(3, 3).Resources) != 0 {
|
||||
t.Fatal("new chunk ignored empty resource selection")
|
||||
}
|
||||
// Saved geometry/resource identity survives restart independently of the active revision.
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Restore validates all resource biome references; use the normal default list,
|
||||
// while keeping the explored layout under test.
|
||||
b, _ := content.Load()
|
||||
restored.Balance.Gathering = b.Gathering
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(before, restored.chunk(2, 2)) {
|
||||
t.Fatal("restart rewrote explored layout")
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,11 @@ func (t *Terrain) Chunk(x, z int) p.Chunk {
|
||||
}
|
||||
c.Resources = []p.Entity{}
|
||||
for _, r := range t.B.Gathering.Resources {
|
||||
if !slices.Contains(r.Biomes, biome.ID) {
|
||||
allowed := slices.Contains(r.Biomes, biome.ID)
|
||||
if biome.Resources != nil {
|
||||
allowed = slices.Contains(biome.Resources, r.ID)
|
||||
}
|
||||
if !allowed {
|
||||
continue
|
||||
}
|
||||
hash := fnv.New64a()
|
||||
|
||||
@@ -27,12 +27,22 @@ type CombatArea struct {
|
||||
}
|
||||
|
||||
func protocolAttack(a content.CombatAttack) p.CombatAttack {
|
||||
return p.CombatAttack{ID: a.ID, Name: a.Name, Description: a.Description, Delivery: a.Delivery, Effect: a.Effect, Stat: a.Stat, Multiplier: a.Multiplier, Range: a.Range, Cooldown: a.Cooldown, Stamina: a.Stamina, Speed: a.Speed, Radius: a.Radius, Duration: a.Duration, Interval: a.Interval}
|
||||
return p.CombatAttack{ID: a.ID, Name: a.Name, Description: a.Description, Delivery: a.Delivery, Effect: a.Effect, Stat: a.Stat, Multiplier: a.Multiplier, Range: a.Range, Cooldown: a.Cooldown, Stamina: a.Stamina, Speed: a.Speed, DexteritySpeed: &a.DexteritySpeed, StrengthSpeed: &a.StrengthSpeed, Radius: a.Radius, Duration: a.Duration, Interval: a.Interval}
|
||||
}
|
||||
func (w *World) playerAttack(c *Character) p.CombatAttack {
|
||||
for _, i := range c.View.Items {
|
||||
if i.ID == c.View.Equipment["weapon"] && i.Attack != nil {
|
||||
return *i.Attack
|
||||
a := *i.Attack
|
||||
if a.Delivery == "projectile" {
|
||||
if a.DexteritySpeed != nil {
|
||||
a.Speed += c.View.Stats["dexterity"].Final * *a.DexteritySpeed
|
||||
}
|
||||
if a.StrengthSpeed != nil {
|
||||
a.Speed += c.View.Stats["strength"].Final * *a.StrengthSpeed
|
||||
}
|
||||
a.Speed = math.Max(1, math.Min(100, a.Speed))
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
return p.CombatAttack{ID: "basic_melee", Name: "Melee attack", Delivery: "melee", Effect: "damage", Stat: "meleeDamage", Multiplier: 1, Range: w.Balance.AttackRange, Cooldown: w.Balance.AttackCooldown, Speed: 1, Interval: 1}
|
||||
@@ -137,6 +147,7 @@ func (w *World) attack(c *Character, target string) error {
|
||||
shot := Projectile{ID: w.combatID("projectile"), Source: c.View.Entity.ID, Player: true, Target: target, Position: from, Destination: to, Attack: a, Power: power}
|
||||
if a.Delivery == "projectile" {
|
||||
w.Projectiles[shot.ID] = &shot
|
||||
w.projectileVisual(shot, "launch", "")
|
||||
} else {
|
||||
shot.Position = to
|
||||
w.impact(shot, target)
|
||||
@@ -254,6 +265,7 @@ func (w *World) updateCombat(dt float64) {
|
||||
for _, id := range ids {
|
||||
shot := w.Projectiles[id]
|
||||
if !w.sourceActive(shot.Source, shot.Player) {
|
||||
w.projectileVisual(*shot, "impact", "cancelled")
|
||||
delete(w.Projectiles, id)
|
||||
w.dirty = true
|
||||
continue
|
||||
@@ -302,6 +314,13 @@ func (w *World) updateCombat(dt float64) {
|
||||
copy.Position = hitPoint
|
||||
}
|
||||
delete(w.Projectiles, id)
|
||||
outcome := "miss"
|
||||
if hit != "" {
|
||||
outcome = "hit"
|
||||
} else if blocked {
|
||||
outcome = "terrain"
|
||||
}
|
||||
w.projectileVisual(copy, "impact", outcome)
|
||||
w.impact(copy, hit)
|
||||
} else {
|
||||
shot.Position = end
|
||||
@@ -326,6 +345,16 @@ func (w *World) updateCombat(dt float64) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Short, bounded event history lets a launch and impact survive between network
|
||||
// snapshots. This is presentation data; damage is resolved only by updateCombat.
|
||||
func (w *World) projectileVisual(shot Projectile, phase, outcome string) {
|
||||
w.ProjectileVisuals = append(w.ProjectileVisuals, p.ProjectileVisual{ID: w.combatID("visual"), ProjectileID: shot.ID, Phase: phase, Position: shot.Position, Destination: shot.Destination, Speed: shot.Attack.Speed, Outcome: outcome, At: w.Age()})
|
||||
if len(w.ProjectileVisuals) > 1024 {
|
||||
w.ProjectileVisuals = w.ProjectileVisuals[len(w.ProjectileVisuals)-1024:]
|
||||
}
|
||||
w.dirty = true
|
||||
}
|
||||
func (w *World) mobKilled(c *Character, m *Mob) {
|
||||
if m.Entity.State == "dead" {
|
||||
return
|
||||
|
||||
@@ -250,3 +250,32 @@ func TestRegionalSpawnsRespectExploredBiomeAndPlayerDistance(t *testing.T) {
|
||||
t.Fatal("regional slot was missing or duplicated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectileSpeedSampleAndShortFlightEvents(t *testing.T) {
|
||||
w, c, m := combatFixture(t)
|
||||
dex, str := float64(2), float64(1)
|
||||
weapon := &c.View.Items[len(c.View.Items)-1]
|
||||
weapon.Attack.DexteritySpeed = &dex
|
||||
weapon.Attack.StrengthSpeed = &str
|
||||
c.View.Stats["dexterity"] = p.StatValue{Final: 5}
|
||||
c.View.Stats["strength"] = p.StatValue{Final: 3}
|
||||
m.Entity.Position = w.terrain.Position(2, 0)
|
||||
if err := w.attack(c, m.Entity.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(w.ProjectileVisuals) != 1 || w.ProjectileVisuals[0].Phase != "launch" || w.ProjectileVisuals[0].Speed != 48 {
|
||||
t.Fatal("wrong launch speed/event")
|
||||
}
|
||||
c.View.Stats["dexterity"] = p.StatValue{Final: 100}
|
||||
w.updateCombat(.1)
|
||||
if len(w.Projectiles) != 0 || len(w.ProjectileVisuals) != 2 || w.ProjectileVisuals[1].Outcome != "hit" {
|
||||
t.Fatal("short flight lost its hit")
|
||||
}
|
||||
if w.ProjectileVisuals[1].Speed != 48 {
|
||||
t.Fatal("stat change altered in-flight speed")
|
||||
}
|
||||
snapshot := w.Snapshot(c.View.Entity.ID, map[string]bool{})
|
||||
if len(snapshot.ProjectileVisuals) != 2 {
|
||||
t.Fatal("flight between snapshots became invisible")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,6 +312,7 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
m.LastAttack = w.Age()
|
||||
if attack.Delivery == "projectile" {
|
||||
w.Projectiles[shot.ID] = &shot
|
||||
w.projectileVisual(shot, "launch", "")
|
||||
} else {
|
||||
shot.Position = body(aim)
|
||||
w.impact(shot, targetID)
|
||||
|
||||
@@ -70,9 +70,10 @@ type ResourceLayout struct {
|
||||
Resources []p.Entity `json:"resources"`
|
||||
}
|
||||
type World struct {
|
||||
Projectiles map[string]*Projectile `json:"projectiles,omitempty"`
|
||||
Areas map[string]*CombatArea `json:"areas,omitempty"`
|
||||
CombatSequence uint64 `json:"combatSequence"`
|
||||
ProjectileVisuals []p.ProjectileVisual `json:"projectileVisuals,omitempty"`
|
||||
Projectiles map[string]*Projectile `json:"projectiles,omitempty"`
|
||||
Areas map[string]*CombatArea `json:"areas,omitempty"`
|
||||
CombatSequence uint64 `json:"combatSequence"`
|
||||
|
||||
ResourceLayouts map[string]ResourceLayout `json:"resourceLayouts,omitempty"`
|
||||
|
||||
@@ -636,6 +637,13 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
playedDT := math.Max(0, dt)
|
||||
w.now = now
|
||||
w.Tick++
|
||||
kept := w.ProjectileVisuals[:0]
|
||||
for _, v := range w.ProjectileVisuals {
|
||||
if w.Age()-v.At <= 2 {
|
||||
kept = append(kept, v)
|
||||
}
|
||||
}
|
||||
w.ProjectileVisuals = kept
|
||||
dt = math.Max(0, math.Min(dt, 0.25))
|
||||
if w.Phase == "won" || w.Phase == "lost" {
|
||||
return
|
||||
@@ -798,6 +806,12 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
|
||||
visible := func(pos p.Vec3) bool {
|
||||
return allowed[Key(ChunkCoord(pos.X, w.Balance.ChunkSize), ChunkCoord(pos.Z, w.Balance.ChunkSize))]
|
||||
}
|
||||
s.ProjectileVisuals = []p.ProjectileVisual{}
|
||||
for _, v := range w.ProjectileVisuals {
|
||||
if w.Age()-v.At <= 2 && (visible(v.Position) || visible(v.Destination)) {
|
||||
s.ProjectileVisuals = append(s.ProjectileVisuals, v)
|
||||
}
|
||||
}
|
||||
for _, other := range w.Characters {
|
||||
if other.View.Entity.ID != id && other.Online && other.View.Entity.State != "dead" && visible(other.View.Entity.Position) {
|
||||
s.Entities = append(s.Entities, other.View.Entity)
|
||||
|
||||
@@ -29,20 +29,22 @@ type CombatContent struct {
|
||||
}
|
||||
|
||||
type CombatAttack struct {
|
||||
Cooldown float64 `json:"cooldown"`
|
||||
Delivery string `json:"delivery"`
|
||||
Description string `json:"description"`
|
||||
Duration float64 `json:"duration"`
|
||||
Effect string `json:"effect"`
|
||||
ID string `json:"id"`
|
||||
Interval float64 `json:"interval"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Name string `json:"name"`
|
||||
Radius float64 `json:"radius"`
|
||||
Range float64 `json:"range"`
|
||||
Speed float64 `json:"speed"`
|
||||
Stamina float64 `json:"stamina"`
|
||||
Stat string `json:"stat"`
|
||||
Cooldown float64 `json:"cooldown"`
|
||||
Delivery string `json:"delivery"`
|
||||
Description string `json:"description"`
|
||||
DexteritySpeed *float64 `json:"dexteritySpeed,omitempty"`
|
||||
Duration float64 `json:"duration"`
|
||||
Effect string `json:"effect"`
|
||||
ID string `json:"id"`
|
||||
Interval float64 `json:"interval"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Name string `json:"name"`
|
||||
Radius float64 `json:"radius"`
|
||||
Range float64 `json:"range"`
|
||||
Speed float64 `json:"speed"`
|
||||
Stamina float64 `json:"stamina"`
|
||||
Stat string `json:"stat"`
|
||||
StrengthSpeed *float64 `json:"strengthSpeed,omitempty"`
|
||||
}
|
||||
|
||||
type LootTable struct {
|
||||
@@ -108,18 +110,21 @@ type StatModifier struct {
|
||||
}
|
||||
|
||||
type GatheringContent struct {
|
||||
Biomes []BiomeDefinition `json:"biomes"`
|
||||
Consumables []GatheringConsumable `json:"consumables"`
|
||||
Resources []ResourceDefinition `json:"resources"`
|
||||
Skills []GatheringSkill `json:"skills"`
|
||||
BiomeHistory []BiomeDefinition `json:"biomeHistory,omitempty"`
|
||||
Biomes []BiomeDefinition `json:"biomes"`
|
||||
Consumables []GatheringConsumable `json:"consumables"`
|
||||
Resources []ResourceDefinition `json:"resources"`
|
||||
Skills []GatheringSkill `json:"skills"`
|
||||
}
|
||||
|
||||
type BiomeDefinition struct {
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Weight int64 `json:"weight"`
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Resources []string `json:"resources"`
|
||||
Revision *int64 `json:"revision,omitempty"`
|
||||
Weight int64 `json:"weight"`
|
||||
}
|
||||
|
||||
type GatheringConsumable struct {
|
||||
@@ -225,32 +230,37 @@ type ContentChange struct {
|
||||
Resource *ResourceDefinitionDraft `json:"resource,omitempty"`
|
||||
Spawn *MobSpawnDraft `json:"spawn,omitempty"`
|
||||
Stats *ContentStatsRules `json:"stats,omitempty"`
|
||||
TargetID *string `json:"targetId,omitempty"`
|
||||
Title *TitleDraft `json:"title,omitempty"`
|
||||
}
|
||||
|
||||
type CombatAttackDraft struct {
|
||||
Cooldown float64 `json:"cooldown"`
|
||||
Delivery string `json:"delivery"`
|
||||
Description string `json:"description"`
|
||||
Duration float64 `json:"duration"`
|
||||
Effect string `json:"effect"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Interval float64 `json:"interval"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Name string `json:"name"`
|
||||
Radius float64 `json:"radius"`
|
||||
Range float64 `json:"range"`
|
||||
Speed float64 `json:"speed"`
|
||||
Stamina float64 `json:"stamina"`
|
||||
Stat string `json:"stat"`
|
||||
Cooldown float64 `json:"cooldown"`
|
||||
Delivery string `json:"delivery"`
|
||||
Description string `json:"description"`
|
||||
DexteritySpeed *float64 `json:"dexteritySpeed,omitempty"`
|
||||
Duration float64 `json:"duration"`
|
||||
Effect string `json:"effect"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Interval float64 `json:"interval"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Name string `json:"name"`
|
||||
Radius float64 `json:"radius"`
|
||||
Range float64 `json:"range"`
|
||||
Speed float64 `json:"speed"`
|
||||
Stamina float64 `json:"stamina"`
|
||||
Stat string `json:"stat"`
|
||||
StrengthSpeed *float64 `json:"strengthSpeed,omitempty"`
|
||||
}
|
||||
|
||||
type BiomeDefinitionDraft struct {
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Weight int64 `json:"weight"`
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
ID *string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Resources []string `json:"resources"`
|
||||
Revision *int64 `json:"revision,omitempty"`
|
||||
Weight int64 `json:"weight"`
|
||||
}
|
||||
|
||||
type GatheringConsumableDraft struct {
|
||||
@@ -387,6 +397,7 @@ type Snapshot struct {
|
||||
Entities []Entity `json:"entities"`
|
||||
GatherRisks map[string]GatheringRisk `json:"gatherRisks,omitempty"`
|
||||
Message string `json:"message"`
|
||||
ProjectileVisuals []ProjectileVisual `json:"projectileVisuals,omitempty"`
|
||||
Self Character `json:"self"`
|
||||
Tick int64 `json:"tick"`
|
||||
Type string `json:"type"`
|
||||
@@ -437,6 +448,17 @@ type GatheringRisk struct {
|
||||
Seconds float64 `json:"seconds"`
|
||||
}
|
||||
|
||||
type ProjectileVisual struct {
|
||||
At float64 `json:"at"`
|
||||
Destination Vec3 `json:"destination"`
|
||||
ID string `json:"id"`
|
||||
Outcome string `json:"outcome"`
|
||||
Phase string `json:"phase"`
|
||||
Position Vec3 `json:"position"`
|
||||
ProjectileID string `json:"projectileId"`
|
||||
Speed float64 `json:"speed"`
|
||||
}
|
||||
|
||||
type Character struct {
|
||||
ACK int64 `json:"ack"`
|
||||
AllocatedStats map[string]int64 `json:"allocatedStats"`
|
||||
|
||||
@@ -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