package game import ( "encoding/json" "math" "testing" "time" "aeternalis/server/content" p "aeternalis/server/internal/protocol" ) func TestLevelRewardsAndPrimaryAllocation(t *testing.T) { w, c := fixture(t) w.grantXP(c, 100) if c.View.Level != 2 || c.View.StatPoints != 10 || c.View.PendingVitalChoices != 1 || c.View.Stats["stamina"].Final != 105 { t.Fatalf("wrong level rewards: %+v", c.View) } for i := 0; i < 3; i++ { if err := w.allocateStat(c, "constitution"); err != nil { t.Fatal(err) } } if c.View.StatPoints != 7 || c.View.Stats["constitution"].Final != 3 || c.View.Entity.MaxHP != 115 { t.Fatal("constitution did not update derived health") } if err := w.chooseVital(c, "health"); err != nil { t.Fatal(err) } if c.View.Entity.MaxHP != 120 || c.View.HealthChoices != 1 || c.View.PendingVitalChoices != 0 { t.Fatal("health choice not applied") } if w.chooseVital(c, "mana") == nil || w.allocateStat(c, "healthPoints") == nil { t.Fatal("duplicate vital or direct derived allocation accepted") } w.grantXP(c, 150) if err := w.chooseVital(c, "mana"); err != nil { t.Fatal(err) } if c.View.Stats["manaPoints"].Final != 55 || c.View.Stats["stamina"].Final != 110 || c.View.StatPoints != 17 { t.Fatal("second level rewards incorrect") } for c.View.StatPoints > 0 { if err := w.allocateStat(c, "dexterity"); err != nil { t.Fatal(err) } } before := c.View.Stats["dexterity"].Final if w.allocateStat(c, "dexterity") == nil || c.View.Stats["dexterity"].Final != before { t.Fatal("overspent banked points") } } func TestStatBreakdownUsesEffectivePrimaryThenFlatAndPercent(t *testing.T) { w, c := fixture(t) w.grantXP(c, 100) for i := 0; i < 4; i++ { if err := w.allocateStat(c, "strength"); err != nil { t.Fatal(err) } } c.View.Titles = []p.TitleAward{{ID: "mixed", Effects: p.TitleEffects{Modifiers: []p.StatModifier{{Stat: "strength", Flat: 2, Percent: 50}, {Stat: "meleeDamage", Flat: 3, Percent: 20}, {Stat: "moveSpeed", Percent: -25}}}}} w.RefreshStats(c) strength := c.View.Stats["strength"] if strength.Base != 4 || strength.Flat != 2 || strength.Percent != 50 || strength.Final != 9 { t.Fatalf("bad primary breakdown: %+v", strength) } damage := c.View.Stats["meleeDamage"] if damage.Base != 27 || damage.Flat != 3 || damage.Percent != 20 || math.Abs(damage.Final-36) > 1e-8 { t.Fatalf("bad derived breakdown: %+v", damage) } if w.playerSpeed(c) != 9 { t.Fatal("negative percentage movement not applied") } c.View.Titles = append(c.View.Titles, p.TitleAward{ID: "curse", Effects: p.TitleEffects{Modifiers: []p.StatModifier{{Stat: "healthPoints", Percent: -100}, {Stat: "moveSpeed", Percent: -100}}}}) w.RefreshStats(c) if c.View.Entity.MaxHP != 50 || w.playerSpeed(c) != 0 { t.Fatal("vital safety floor or zero speed bound failed") } } func TestStatsMigrationAndTransferPreserveEarnedRewards(t *testing.T) { w, c := fixture(t) c.View.Level = 3 c.View.Experience = 25 c.View.StatPoints = 10 c.StatsVersion = 0 c.View.AllocatedStats = nil c.View.Stats = nil c.View.PendingVitalChoices = 0 old := *c.Progression old.StatPointsPerLevel = 5 old.StaminaPerLevel = 0 old.VitalPointsPerChoice = 0 c.Progression = &old w.Balance.Stats = nil c.DailySeconds = 321 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) } if err = restored.Restore(w.now); err != nil { t.Fatal(err) } migrated := restored.Characters[c.View.Entity.ID] if migrated.View.StatPoints != 20 || migrated.View.PendingVitalChoices != 2 || migrated.View.Stats["stamina"].Final != 110 || migrated.DailySeconds != 321 { t.Fatal("legacy level rewards not migrated once") } if err = restored.Restore(w.now); err != nil { t.Fatal(err) } if migrated.View.StatPoints != 20 || migrated.View.PendingVitalChoices != 2 { t.Fatal("repeated restore duplicated rewards") } if err = restored.allocateStat(migrated, "wisdom"); err != nil { t.Fatal(err) } if err = restored.chooseVital(migrated, "mana"); err != nil { t.Fatal(err) } migrated.View.Entity.State = "eligible" next := New("stats-next", 999, w.now, restored.Balance) if err = next.Import(migrated); err != nil { t.Fatal(err) } if migrated.View.StatPoints != 19 || migrated.View.AllocatedStats["wisdom"] != 1 || migrated.View.PendingVitalChoices != 1 || migrated.View.ManaChoices != 1 || migrated.DailySeconds != 321 { t.Fatal("transfer altered rewards or daily budget") } next.kill(migrated, "test") if next.Connect(migrated.View.Entity.ID) == nil { t.Fatal("stat feature resurrected a life") } if next.Intent(migrated.View.Entity.ID, p.Intent{Op: "allocate-stat", Target: "strength", Seq: 1}) == nil { t.Fatal("dead life allocated a point") } } func TestCatalogChangesDoNotHealOrRewriteDeadStats(t *testing.T) { w, c := fixture(t) dead, err := w.Join("dead-stats", "Dead") if err != nil { t.Fatal(err) } w.kill(dead, "test") prior := dead.View.Stats["healthPoints"].Final c.View.Entity.HP = 20 c.DailySeconds = 123 catalog, err := content.DefaultCatalog() if err != nil { t.Fatal(err) } catalog.Version = 2 catalog.Stats.Base["healthPoints"] = 200 if err = w.ApplyCatalog(catalog); err != nil { t.Fatal(err) } if c.View.Entity.MaxHP != 200 || c.View.Entity.HP != 20 || c.DailySeconds != 123 || dead.View.Entity.State != "dead" || dead.View.Stats["healthPoints"].Final != prior { t.Fatal("catalog changed life/budget/history") } w.Balance.DailyPlaySeconds = 124 w.Advance(w.now.Add(2*time.Second), 2) if c.Online || c.View.RemainingSeconds != 0 || c.View.Entity.HP != 50 { t.Fatal("expanded stats weakened daily cap") } }