Add persistent destructible world structures and building kits
This commit is contained in:
@@ -17,6 +17,7 @@ var ErrCatalogInvalid = errors.New("invalid catalog change")
|
||||
var catalogRequestID = regexp.MustCompile(`^[a-f0-9-]{32,36}$`)
|
||||
|
||||
type CatalogChange struct {
|
||||
Building *content.BuildingDefinition `json:"building,omitempty"`
|
||||
Power *content.PowerDefinition `json:"power,omitempty"`
|
||||
Weapon *content.GatheringSkill `json:"weapon,omitempty"`
|
||||
CraftRecipe *content.CraftRecipe `json:"craftRecipe,omitempty"`
|
||||
@@ -53,7 +54,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.Power != nil && c.Power.ID != "") || (c.Weapon != nil && c.Weapon.ID != "") || (c.CraftRecipe != nil && c.CraftRecipe.ID != "") || (c.CraftProfession != nil && c.CraftProfession.ID != "") || (c.CraftMaterial != nil && c.CraftMaterial.ID != "") || (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 != "")
|
||||
return (c.Building != nil && c.Building.ID != "") || (c.Power != nil && c.Power.ID != "") || (c.Weapon != nil && c.Weapon.ID != "") || (c.CraftRecipe != nil && c.CraftRecipe.ID != "") || (c.CraftProfession != nil && c.CraftProfession.ID != "") || (c.CraftMaterial != nil && c.CraftMaterial.ID != "") || (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 {
|
||||
@@ -138,6 +139,13 @@ func decodeCatalog(version int64, raw string) (content.Catalog, error) {
|
||||
}
|
||||
catalog.RestorePowers(b)
|
||||
}
|
||||
if catalog.Building == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return catalog, e
|
||||
}
|
||||
catalog.RestoreBuilding(b)
|
||||
}
|
||||
return catalog, catalog.Validate()
|
||||
}
|
||||
func (d *DB) Catalog(ctx context.Context) (content.Catalog, error) {
|
||||
@@ -173,12 +181,14 @@ func (c CatalogChange) validate() error {
|
||||
}
|
||||
valid := false
|
||||
count := 0
|
||||
for _, present := range []bool{c.Power != nil, c.Weapon != nil, c.CraftRecipe != nil, c.CraftProfession != nil, c.CraftMaterial != nil, c.CraftRules != nil, 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} {
|
||||
for _, present := range []bool{c.Building != nil, c.Power != nil, c.Weapon != nil, c.CraftRecipe != nil, c.CraftProfession != nil, c.CraftMaterial != nil, c.CraftRules != nil, 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-building":
|
||||
valid = c.Building != nil
|
||||
case "create-power":
|
||||
valid = c.Power != nil
|
||||
case "create-weapon":
|
||||
|
||||
@@ -26,6 +26,7 @@ func TestServerAssignedCatalogIDsSurviveReplayAndRestart(t *testing.T) {
|
||||
var last CatalogChange
|
||||
var lastResult CatalogResult
|
||||
for _, draft := range []CatalogChange{
|
||||
{Op: "create-building", Building: &content.BuildingDefinition{Name: "Same name", Model: "wall", Width: 4, Depth: 1, Height: 2, HP: 250, Profession: "builder", Level: 1}},
|
||||
{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}}}},
|
||||
@@ -86,7 +87,7 @@ func TestServerAssignedCatalogIDsSurviveReplayAndRestart(t *testing.T) {
|
||||
t.Fatal("restart changed replay identity", err)
|
||||
}
|
||||
audits, err := reopened.CatalogChanges(ctx)
|
||||
if err != nil || len(audits) != 8 || audits[0].Target != lastResult.ID {
|
||||
if err != nil || len(audits) != len(seen) || audits[0].Target != lastResult.ID {
|
||||
t.Fatal("generated ID not persisted in audit", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ 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.Building != nil {
|
||||
resolved.Building.ID = ""
|
||||
}
|
||||
if resolved.Power != nil {
|
||||
resolved.Power.ID = ""
|
||||
}
|
||||
@@ -139,7 +142,24 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
|
||||
}
|
||||
powers := *c.Powers
|
||||
c.Powers = &powers
|
||||
if c.Building == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return invalid(e)
|
||||
}
|
||||
c.RestoreBuilding(b)
|
||||
}
|
||||
building := *c.Building
|
||||
c.Building = &building
|
||||
switch change.Op {
|
||||
case "create-building":
|
||||
v := *change.Building
|
||||
if v.ID == "" {
|
||||
v.ID = "building_" + game.ID()
|
||||
}
|
||||
c.Building.Definitions = append(slices.Clone(c.Building.Definitions), v)
|
||||
resolved.Building = &v
|
||||
target = v.ID
|
||||
case "create-power":
|
||||
v := *change.Power
|
||||
if v.ID == "" {
|
||||
|
||||
Reference in New Issue
Block a user