Add ranged combat area effects and admin-authored mobs
This commit is contained in:
@@ -17,6 +17,10 @@ var ErrCatalogInvalid = errors.New("invalid catalog change")
|
||||
var catalogRequestID = regexp.MustCompile(`^[a-f0-9-]{32,36}$`)
|
||||
|
||||
type CatalogChange struct {
|
||||
Attack *content.CombatAttack `json:"attack,omitempty"`
|
||||
Mob *content.MobDefinition `json:"mob,omitempty"`
|
||||
Loot *content.LootTable `json:"loot,omitempty"`
|
||||
Spawn *content.MobSpawn `json:"spawn,omitempty"`
|
||||
GatheringSkill *content.GatheringSkill `json:"gatheringSkill,omitempty"`
|
||||
Resource *content.ResourceDefinition `json:"resource,omitempty"`
|
||||
Biome *content.BiomeDefinition `json:"biome,omitempty"`
|
||||
@@ -42,7 +46,7 @@ type CatalogResult struct {
|
||||
// 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.GatheringSkill != nil && c.GatheringSkill.ID != "") || (c.Resource != nil && c.Resource.ID != "") || (c.Biome != nil && c.Biome.ID != "") || (c.Consumable != nil && c.Consumable.ID != "") || (c.Title != nil && c.Title.ID != "") || (c.Item != nil && c.Item.ID != "") || (c.RequirementSet != nil && c.RequirementSet.ID != "") || (c.EffectSet != nil && c.EffectSet.ID != "")
|
||||
return (c.Attack != nil && c.Attack.ID != "") || (c.Mob != nil && c.Mob.ID != "") || (c.Loot != nil && c.Loot.ID != "") || (c.Spawn != nil && c.Spawn.ID != "") || (c.GatheringSkill != nil && c.GatheringSkill.ID != "") || (c.Resource != nil && c.Resource.ID != "") || (c.Biome != nil && c.Biome.ID != "") || (c.Consumable != nil && c.Consumable.ID != "") || (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 {
|
||||
@@ -106,6 +110,13 @@ func decodeCatalog(version int64, raw string) (content.Catalog, error) {
|
||||
}
|
||||
catalog.Gathering = b.Gathering
|
||||
}
|
||||
if catalog.Combat == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return catalog, e
|
||||
}
|
||||
catalog.Combat = b.Combat
|
||||
}
|
||||
return catalog, catalog.Validate()
|
||||
}
|
||||
func (d *DB) Catalog(ctx context.Context) (content.Catalog, error) {
|
||||
@@ -135,12 +146,20 @@ func (c CatalogChange) validate() error {
|
||||
}
|
||||
valid := false
|
||||
count := 0
|
||||
for _, present := range []bool{c.GatheringSkill != nil, c.Resource != nil, c.Biome != nil, c.Consumable != nil, c.Title != nil, c.Item != nil, c.Stats != nil, c.RequirementSet != nil, c.EffectSet != nil} {
|
||||
for _, present := range []bool{c.Attack != nil, c.Mob != nil, c.Loot != nil, c.Spawn != nil, c.GatheringSkill != nil, c.Resource != nil, c.Biome != nil, c.Consumable != nil, c.Title != nil, c.Item != nil, c.Stats != nil, c.RequirementSet != nil, c.EffectSet != nil} {
|
||||
if present {
|
||||
count++
|
||||
}
|
||||
}
|
||||
switch c.Op {
|
||||
case "create-attack":
|
||||
valid = c.Attack != nil
|
||||
case "create-mob":
|
||||
valid = c.Mob != nil
|
||||
case "create-loot":
|
||||
valid = c.Loot != nil
|
||||
case "create-spawn":
|
||||
valid = c.Spawn != nil
|
||||
case "create-gathering-skill":
|
||||
valid = c.GatheringSkill != nil
|
||||
case "create-resource":
|
||||
|
||||
@@ -35,6 +35,18 @@ func (d *DB) PreviewCatalog(ctx context.Context, change CatalogChange) (CatalogP
|
||||
}
|
||||
// Preview validates a provisional identity but does not reserve or expose it.
|
||||
if !change.HasDefinitionID() {
|
||||
if resolved.Attack != nil {
|
||||
resolved.Attack.ID = ""
|
||||
}
|
||||
if resolved.Mob != nil {
|
||||
resolved.Mob.ID = ""
|
||||
}
|
||||
if resolved.Loot != nil {
|
||||
resolved.Loot.ID = ""
|
||||
}
|
||||
if resolved.Spawn != nil {
|
||||
resolved.Spawn.ID = ""
|
||||
}
|
||||
if resolved.GatheringSkill != nil {
|
||||
resolved.GatheringSkill.ID = ""
|
||||
}
|
||||
@@ -85,7 +97,48 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
}
|
||||
gathering := *c.Gathering
|
||||
c.Gathering = &gathering
|
||||
if c.Combat == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return invalid(e)
|
||||
}
|
||||
c.Combat = b.Combat
|
||||
}
|
||||
combat := *c.Combat
|
||||
c.Combat = &combat
|
||||
switch change.Op {
|
||||
case "create-attack":
|
||||
v := *change.Attack
|
||||
if v.ID == "" {
|
||||
v.ID = "attack_" + game.ID()
|
||||
}
|
||||
resolved.Attack = &v
|
||||
c.Combat.Attacks = append(slices.Clone(c.Combat.Attacks), v)
|
||||
target = v.ID
|
||||
case "create-mob":
|
||||
v := *change.Mob
|
||||
if v.ID == "" {
|
||||
v.ID = "mob_" + game.ID()
|
||||
}
|
||||
resolved.Mob = &v
|
||||
c.Combat.Mobs = append(slices.Clone(c.Combat.Mobs), v)
|
||||
target = v.ID
|
||||
case "create-loot":
|
||||
v := *change.Loot
|
||||
if v.ID == "" {
|
||||
v.ID = "loot_" + game.ID()
|
||||
}
|
||||
resolved.Loot = &v
|
||||
c.Combat.Loot = append(slices.Clone(c.Combat.Loot), v)
|
||||
target = v.ID
|
||||
case "create-spawn":
|
||||
v := *change.Spawn
|
||||
if v.ID == "" {
|
||||
v.ID = "spawn_" + game.ID()
|
||||
}
|
||||
resolved.Spawn = &v
|
||||
c.Combat.Spawns = append(slices.Clone(c.Combat.Spawns), v)
|
||||
target = v.ID
|
||||
case "create-gathering-skill":
|
||||
v := *change.GatheringSkill
|
||||
if v.ID == "" {
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"aeternalis/server/internal/game"
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCombatCatalogReferencesAndReplay(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "combat.db")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
catalog, _ := db.Catalog(ctx)
|
||||
publish := func(change CatalogChange) string {
|
||||
t.Helper()
|
||||
change.ID = game.ID()
|
||||
change.ExpectedVersion = catalog.Version
|
||||
change.Reason = "Combat content test"
|
||||
if _, err := db.PreviewCatalog(ctx, change); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := db.PublishCatalog(ctx, "admin", change)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := db.PublishCatalog(ctx, "admin", change)
|
||||
if err != nil || r != again {
|
||||
t.Fatal("changed replay", err)
|
||||
}
|
||||
catalog, _ = db.Catalog(ctx)
|
||||
return r.ID
|
||||
}
|
||||
attack := catalog.Combat.Attacks[0]
|
||||
attack.ID = ""
|
||||
attack.Name = "Test shot"
|
||||
id := publish(CatalogChange{Op: "create-attack", Attack: &attack})
|
||||
loot := publish(CatalogChange{Op: "create-loot", Loot: &content.LootTable{Name: "Loot", Entries: []content.LootEntry{{Item: "herb", Amount: 1, Chance: 100, Weight: 1}}}})
|
||||
mob := catalog.Combat.Mobs[0]
|
||||
mob.ID = ""
|
||||
mob.AttackID = id
|
||||
mob.LootID = loot
|
||||
mid := publish(CatalogChange{Op: "create-mob", Mob: &mob})
|
||||
publish(CatalogChange{Op: "create-spawn", Spawn: &content.MobSpawn{Name: "Region", MobID: mid, Count: 2, Respawn: 60, Biomes: []string{"forest"}}})
|
||||
publish(CatalogChange{Op: "create-item", Item: &content.ItemDefinition{Name: "Bow", Slot: "weapon", Requirements: content.ItemRequirements{Level: 1}, AttackID: id}})
|
||||
attack.Duration = 2
|
||||
attack.Radius = 0
|
||||
bad := CatalogChange{ID: game.ID(), ExpectedVersion: catalog.Version, Reason: "Invalid area test", Op: "create-attack", Attack: &attack}
|
||||
if _, err := db.PublishCatalog(ctx, "admin", bad); !errors.Is(err, ErrCatalogInvalid) {
|
||||
t.Fatal("invalid area accepted", err)
|
||||
}
|
||||
mob.AttackID = "missing"
|
||||
bad.Op = "create-mob"
|
||||
bad.Attack = nil
|
||||
bad.Mob = &mob
|
||||
if _, err := db.PublishCatalog(ctx, "admin", bad); !errors.Is(err, ErrCatalogInvalid) {
|
||||
t.Fatal("missing attack reference accepted", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user