package game import ( p "aeternalis/server/internal/protocol" "encoding/json" "fmt" "maps" "math" "reflect" "testing" "time" ) // Existing action regressions use the new ingredient path with a certain success // roll. A staged copy also exercises failure rollback without a transport fixture. func (w *World) craft(c *Character, name string) error { staged := *w staged.Characters = maps.Clone(w.Characters) copy := *c raw, _ := json.Marshal(c.View) var view p.Character _ = json.Unmarshal(raw, &view) copy.View = view staged.Characters[c.View.Entity.ID] = © content := *w.Balance.Crafting content.Rules.BaseFailure = 0 content.Rules.MinFailure = 0 content.Rules.MaxFailure = 0 staged.Balance.Crafting = &content for _, r := range content.Recipes { if r.ID == "recipe_"+name { _, e := staged.AttemptCraft(©, p.CraftRequest{ID: ID(), CharacterID: c.View.Entity.ID, Profession: r.Profession, Ingredients: r.Inputs}) if e != nil { return e } *c = copy w.RandomCounter = staged.RandomCounter w.PendingTitles = staged.PendingTitles return nil } } return fmt.Errorf("unknown test recipe %s", name) } func TestCraftFailureDiscoveryAndHubRules(t *testing.T) { w, c := fixture(t) v := w.Balance.Crafting v.Rules.BaseFailure = 100 v.Rules.MinFailure = 100 v.Rules.MaxFailure = 100 c.View.Inventory["wood"] = 20 c.View.Inventory["ore"] = 20 req := p.CraftRequest{ID: ID(), CharacterID: c.View.Entity.ID, Profession: "weapon_smithing", Ingredients: map[string]int64{"wood": 3, "ore": 2}} beforeXP := c.View.Experience result, e := w.AttemptCraft(c, req) if e != nil || !result.Valid || result.Success || result.Recipe == nil { t.Fatal("valid failed craft was not discovered", result, e) } if c.View.Crafting["weapon_smithing"].XP != 0 || c.View.Experience != beforeXP || c.View.Inventory["wood"] != 17 || c.View.Inventory["ore"] != 18 { t.Fatal("failed craft XP/loss mismatch") } w.now = w.now.Add(2 * time.Second) req.Ingredients = map[string]int64{"wood": 1} result, e = w.AttemptCraft(c, req) if e != nil || result.Valid || result.Success || len(c.View.DiscoveredRecipes) != 1 { t.Fatal("unknown recipe gave discovery", e) } w.now = w.now.Add(2 * time.Second) w.Kind = "hub" w.Phase = "hub" v.Rules.BaseFailure = 0 v.Rules.MinFailure = 0 v.Rules.MaxFailure = 0 req.Ingredients = map[string]int64{"wood": 3, "ore": 2} result, e = w.AttemptCraft(c, req) if e != nil || !result.Success { t.Fatal(e) } if c.View.Experience != beforeXP || c.View.Crafting["weapon_smithing"].XP != 0 || c.View.Skills["crafting"] != 0 { t.Fatal("hub craft gained XP") } var crafted *p.ItemInstance for i := range c.View.Items { if c.View.Items[i].DefinitionID == "spear" { crafted = &c.View.Items[i] } } fixed := 0 for _, item := range w.Balance.Items { if item.ID == "spear" { fixed = len(item.Modifiers) } } if crafted == nil || crafted.Provenance == nil || crafted.Provenance.CrafterName != "Traveler" || crafted.Provenance.WorldID != w.ID || len(crafted.Modifiers) != fixed+1 { t.Fatal("crafted gear missing provenance or stat roll") } before := len(c.View.Items) if _, e = w.AttemptCraft(c, req); e == nil || len(c.View.Items) != before { t.Fatal("cooldown bypass") } } func TestCraftedStackSplitDeathLootAndReplacement(t *testing.T) { w, c := fixture(t) w.Balance.Crafting.Rules.BaseFailure = 0 w.Balance.Crafting.Rules.MinFailure = 0 w.Balance.Crafting.Rules.MaxFailure = 0 c.View.Inventory["herb"] = 8 for i := range w.Balance.Crafting.Recipes { if w.Balance.Crafting.Recipes[i].ID == "recipe_bandage" { w.Balance.Crafting.Recipes[i].Outputs["bandage"] = 3 } } if e := w.craft(c, "bandage"); e != nil { t.Fatal(e) } before := c.View.CraftedStacks[0].Provenance batchID := c.View.CraftedStacks[0].ID if e := w.dropItem(c, "bandage"); e != nil { t.Fatal(e) } var drop *Corpse for _, v := range w.Corpses { drop = v } if drop == nil || drop.CraftedStacks[0].ID == batchID || drop.CraftedStacks[0].Quantity != 1 || c.View.CraftedStacks[0].Quantity != 2 || !reflect.DeepEqual(drop.CraftedStacks[0].Provenance, before) { t.Fatal("partial drop lost/duplicated provenance") } other, _ := w.Join("other", "Other") other.View.Entity.Position = c.View.Entity.Position if e := w.lootCorpse(other, drop.Entity.ID); e != nil { t.Fatal(e) } if !reflect.DeepEqual(other.View.CraftedStacks[0].Provenance, before) { t.Fatal("pickup changed crafter") } w.kill(c, "test") corpse := w.Corpses[c.RevivalCorpse] if len(c.View.CraftedStacks) != 0 || len(corpse.CraftedStacks) != 1 { t.Fatal("death duplicated crafted stock") } if e := w.lootCorpse(other, corpse.Entity.ID); e != nil { t.Fatal(e) } TakeStack(other, "bandage", 2) if other.View.Inventory["bandage"] != 1 || len(other.View.CraftedStacks) != 1 || other.View.CraftedStacks[0].Quantity != 1 { t.Fatal("consumption left invalid provenance") } raw, _ := json.Marshal(w) var restored World if e := json.Unmarshal(raw, &restored); e != nil { t.Fatal(e) } if e := restored.Restore(w.now); e != nil { t.Fatal(e) } replacement, e := restored.Join(c.Account, "Replacement") if e != nil { t.Fatal(e) } if len(replacement.View.DiscoveredRecipes) != 0 || replacement.View.Crafting["alchemist"].XP != 0 { t.Fatal("replacement inherited crafting progression") } } func TestCraftRequirementsAndQuality(t *testing.T) { w, c := fixture(t) c.View.Inventory["wood"] = 3 c.View.Inventory["ore"] = 2 for i := range w.Balance.Crafting.Recipes { if w.Balance.Crafting.Recipes[i].ID == "recipe_spear" { w.Balance.Crafting.Recipes[i].Stats = map[string]float64{"strength": 10000} } } counter := w.RandomCounter _, e := w.AttemptCraft(c, p.CraftRequest{ID: ID(), CharacterID: c.View.Entity.ID, Profession: "weapon_smithing", Ingredients: map[string]int64{"wood": 3, "ore": 2}}) if e == nil || w.RandomCounter != counter || c.View.Inventory["wood"] != 3 || len(c.View.DiscoveredRecipes) != 0 { t.Fatal("unmet requirements consumed materials or rolled") } item := w.Balance.Items[1] sumLow, sumHigh := float64(0), float64(0) for range 200 { start := w.RandomCounter low := w.craftModifiers(item, 1) w.RandomCounter = start high := w.craftModifiers(item, 100) if len(low) != 1 || len(high) != 1 || low[0].Stat != high[0].Stat { t.Fatal("pool selection not preserved") } if high[0].Flat < low[0].Flat || math.IsNaN(high[0].Flat) { t.Fatal("higher proficiency worsened corresponding roll") } sumLow += low[0].Flat sumHigh += high[0].Flat } if sumHigh <= sumLow { t.Fatal("quality distribution did not improve") } }