290 lines
10 KiB
Go
290 lines
10 KiB
Go
package game
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
)
|
|
|
|
func TestLevelUpsBankPointsAndCap(t *testing.T) {
|
|
w, c := fixture(t)
|
|
if c.View.Level != 1 || c.View.StatPoints != 0 || c.View.ExperienceToNextLevel != 100 {
|
|
t.Fatal("incorrect new life progression")
|
|
}
|
|
w.grantXP(c, 99)
|
|
if c.View.Level != 1 || c.View.Experience != 99 {
|
|
t.Fatal("premature level")
|
|
}
|
|
w.grantXP(c, 151)
|
|
if c.View.Level != 3 || c.View.Experience != 40 || c.View.StatPoints != 20 || c.View.ExperienceToNextLevel != 140 {
|
|
t.Fatalf("multi-level award lost points or XP: %+v", c.View)
|
|
}
|
|
c.Progression.MaxLevel = 4
|
|
w.grantXP(c, 1000000)
|
|
if c.View.Level != 4 || c.View.StatPoints != 30 || c.View.Experience != 0 || c.View.ExperienceToNextLevel != 0 {
|
|
t.Fatal("level cap failed")
|
|
}
|
|
w.grantXP(c, 1000000)
|
|
if c.View.StatPoints != 30 {
|
|
t.Fatal("cap farming granted extra points")
|
|
}
|
|
}
|
|
|
|
func TestOnlySuccessfulActionsGrantXP(t *testing.T) {
|
|
w, c := fixture(t)
|
|
id := c.View.Entity.ID
|
|
w.now = w.now.Add(2 * time.Second)
|
|
for seq, op := range []string{"grant-xp", "level-up", "spend-stat", "grant-title", "remove-title", "gather", "craft", "attack"} {
|
|
if w.Intent(id, p.Intent{Op: op, Seq: int64(seq + 1), Target: "invalid"}) == nil {
|
|
t.Fatalf("invalid %s accepted", op)
|
|
}
|
|
}
|
|
if c.View.Experience != 0 || c.View.StatPoints != 0 || len(w.PendingTitles) != 0 {
|
|
t.Fatal("failed intent awarded progression")
|
|
}
|
|
c.View.Entity.Position = w.terrain.Position(17, 12)
|
|
if err := w.Intent(id, p.Intent{Op: "gather", Seq: 9, Target: "arrival:ore"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Experience != w.Balance.Progression.GatherXP {
|
|
t.Fatal("missing gather XP")
|
|
}
|
|
if w.Intent(id, p.Intent{Op: "gather", Seq: 10, Target: "arrival:ore"}) == nil {
|
|
t.Fatal("duplicate gather")
|
|
}
|
|
if c.View.Experience != w.Balance.Progression.GatherXP {
|
|
t.Fatal("failed gather gained XP")
|
|
}
|
|
c.View.Inventory["herb"] = 2
|
|
if err := w.craft(c, "bandage"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := c.View.Experience
|
|
if before != w.Balance.Progression.GatherXP+w.Balance.Progression.CraftXP {
|
|
t.Fatal("missing craft XP")
|
|
}
|
|
w.Mobs["test"] = &Mob{Entity: p.Entity{ID: "test", HP: w.Balance.PlayerDamage + 1, Position: c.View.Entity.Position}}
|
|
if err := w.Intent(id, p.Intent{Op: "attack", Seq: 12, Target: "test"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Experience != before {
|
|
t.Fatal("nonlethal hit granted kill XP")
|
|
}
|
|
w.now = w.now.Add(time.Second)
|
|
if err := w.Intent(id, p.Intent{Op: "attack", Seq: 13, Target: "test"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Experience != before+w.Balance.Progression.MobKillXP || c.WorldKills != 1 {
|
|
t.Fatal("missing kill XP")
|
|
}
|
|
w.now = w.now.Add(time.Second)
|
|
if w.Intent(id, p.Intent{Op: "attack", Seq: 14, Target: "test"}) == nil {
|
|
t.Fatal("dead mob killed twice")
|
|
}
|
|
if c.View.Kills != 1 || c.View.Experience != before+w.Balance.Progression.MobKillXP {
|
|
t.Fatal("duplicate kill progression")
|
|
}
|
|
}
|
|
|
|
func TestProgressionSurvivesRestartDeathAndTransfer(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.grantXP(c, 275)
|
|
c.View.Titles = []p.TitleAward{{ID: "bound", Name: "Bound", Scope: "personal", Effects: p.TitleEffects{Damage: 2, Modifiers: []p.StatModifier{}}}}
|
|
c.DailySeconds = 123
|
|
c.WorldKills = 7
|
|
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)
|
|
}
|
|
loaded := restored.Characters[c.View.Entity.ID]
|
|
if loaded.View.Level != 3 || loaded.View.Experience != 65 || loaded.View.StatPoints != 20 || !reflect.DeepEqual(loaded.View.Titles, c.View.Titles) {
|
|
t.Fatal("restart reset progression")
|
|
}
|
|
loaded.View.Entity.State = "eligible"
|
|
changed := w.Balance
|
|
rules := *changed.Progression
|
|
rules.BaseXP = 1
|
|
rules.StatPointsPerLevel = 100
|
|
changed.Progression = &rules
|
|
destination := New("destination", 999, w.now, changed)
|
|
if err = destination.Import(loaded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.View.ExperienceToNextLevel != 140 || loaded.Progression.StatPointsPerLevel != 10 || loaded.DailySeconds != 123 || loaded.WorldKills != 0 || loaded.View.StatPoints != 20 || len(loaded.View.Titles) != 1 {
|
|
t.Fatal("transfer altered progression, titles, or daily budget")
|
|
}
|
|
destination.kill(loaded, "test death")
|
|
destination.grantXP(loaded, 1000)
|
|
if loaded.View.StatPoints != 20 || loaded.View.Experience != 65 || len(loaded.View.Titles) != 1 {
|
|
t.Fatal("death erased progression or allowed postmortem XP")
|
|
}
|
|
replacement, err := destination.Join(loaded.Account, "Replacement")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if replacement.View.Level != 1 || replacement.View.StatPoints != 0 || replacement.View.Entity.ID == loaded.View.Entity.ID {
|
|
t.Fatal("replacement inherited character levels")
|
|
}
|
|
if destination.Connect(loaded.View.Entity.ID) == nil {
|
|
t.Fatal("progression resurrected dead character")
|
|
}
|
|
}
|
|
|
|
func TestLegacySnapshotProgressionMigrationIsIdempotent(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.Progression = nil
|
|
w.Balance.Titles = nil
|
|
c.Progression = nil
|
|
c.View.Level = 0
|
|
c.View.ExperienceToNextLevel = 0
|
|
c.View.Titles = nil
|
|
c.View.Kills = 42
|
|
c.View.Skills["gathering"] = 99
|
|
c.DailySeconds = 456
|
|
w.kill(c, "legacy test death")
|
|
if err := w.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Level != 1 || c.View.Experience != 0 || c.View.StatPoints != 0 || c.View.Titles == nil || c.DailySeconds != 456 || c.View.Entity.State != "dead" {
|
|
t.Fatal("legacy migration changed life/budget or backfilled XP")
|
|
}
|
|
if err := w.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.StatPoints != 0 || len(w.PendingTitles) != 0 {
|
|
t.Fatal("migration minted grants")
|
|
}
|
|
}
|
|
|
|
func TestTitleRequirementsAndPendingAwardsHaveNoEffects(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.Titles = []content.Title{{ID: "trial", Name: "Trial", Scope: "world", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 2}, {Metric: "worldKills", AtLeast: 1}}, Effects: content.TitleEffects{Damage: 3, MoveSpeed: -2}}}
|
|
w.grantXP(c, 100)
|
|
w.evaluateTitles(c)
|
|
if len(w.PendingTitles) != 0 {
|
|
t.Fatal("requirements were not ANDed")
|
|
}
|
|
c.WorldKills = 1
|
|
w.evaluateTitles(c)
|
|
w.evaluateTitles(c)
|
|
if len(w.PendingTitles) != 1 || len(c.View.Titles) != 0 || w.playerSpeed(c) != w.Balance.PlayerSpeed {
|
|
t.Fatal("candidate duplicated or applied before durable award")
|
|
}
|
|
award := w.PendingTitles[0].Award
|
|
if award.WorldID != w.ID || award.Scope != "world" || award.Effects.MoveSpeed != -2 || award.EarnedAt == "" {
|
|
t.Fatal("candidate lost provenance or negative effect")
|
|
}
|
|
c.View.Titles = append(c.View.Titles, award)
|
|
w.RefreshStats(c)
|
|
w.PendingTitles = nil
|
|
w.Balance.Titles[0].Effects.MoveSpeed = 99
|
|
c.WorldKills = 0
|
|
w.evaluateTitles(c)
|
|
if len(w.PendingTitles) != 0 || w.playerSpeed(c) != w.Balance.PlayerSpeed-2 {
|
|
t.Fatal("lost requirement or edited catalog changed earned award")
|
|
}
|
|
}
|
|
|
|
func TestSignedTitleEffectsAreServerAuthoritative(t *testing.T) {
|
|
w, c := fixture(t)
|
|
c.View.Titles = []p.TitleAward{{ID: "blessing", Effects: p.TitleEffects{Damage: 5, MoveSpeed: 2}}, {ID: "curse", Effects: p.TitleEffects{Damage: -2, MoveSpeed: -1}}}
|
|
w.RefreshStats(c)
|
|
w.now = w.now.Add(2 * time.Second)
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "move", Seq: 1, X: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := c.View.Entity.Position
|
|
w.Advance(w.now.Add(50*time.Millisecond), .05)
|
|
if math.Abs(distance(before, c.View.Entity.Position)-(w.Balance.PlayerSpeed+1)*.05) > 1e-8 {
|
|
t.Fatal("title movement effect not applied")
|
|
}
|
|
w.Mobs["target"] = &Mob{Entity: p.Entity{ID: "target", HP: 100, Position: c.View.Entity.Position}}
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 2, Target: "target"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Mobs["target"].Entity.HP != 100-w.Balance.PlayerDamage-3 {
|
|
t.Fatal("signed damage effects not applied")
|
|
}
|
|
c.View.Titles = append(c.View.Titles, p.TitleAward{ID: "heavy", Effects: p.TitleEffects{Damage: -1000, MoveSpeed: -1000}})
|
|
w.RefreshStats(c)
|
|
if w.playerSpeed(c) != 0 {
|
|
t.Fatal("negative speed reversed movement")
|
|
}
|
|
w.now = w.now.Add(time.Second)
|
|
hp := w.Mobs["target"].Entity.HP
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 3, Target: "target"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Mobs["target"].Entity.HP != hp {
|
|
t.Fatal("negative damage healed target")
|
|
}
|
|
w.Balance.DailyPlaySeconds = 1
|
|
c.View.Entity.HP = 20
|
|
w.Advance(w.now.Add(2*time.Second), 2)
|
|
if c.Online || c.View.RemainingSeconds != 0 || c.View.Entity.HP != 50 {
|
|
t.Fatal("title effects weakened daily cap")
|
|
}
|
|
}
|
|
|
|
func TestBossXPAndSurvivalMilestoneGrantedOnce(t *testing.T) {
|
|
w, c := fixture(t)
|
|
offline, _ := w.Join("offline", "Offline")
|
|
dead, _ := w.Join("dead", "Dead")
|
|
w.kill(dead, "test")
|
|
w.now = w.now.Add(time.Second)
|
|
if err := w.TriggerBoss(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.BossHP = 1
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 1, Target: "boss"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.BossKills != 1 || c.WorldsSurvived != 1 || offline.WorldsSurvived != 1 || dead.WorldsSurvived != 0 || c.View.Level != 4 || c.View.Experience != 150 || c.View.StatPoints != 30 {
|
|
t.Fatal("incorrect boss progression")
|
|
}
|
|
if w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Seq: 2, Target: "boss"}) == nil {
|
|
t.Fatal("repeat boss kill")
|
|
}
|
|
if c.BossKills != 1 || c.View.StatPoints != 30 {
|
|
t.Fatal("duplicate boss rewards")
|
|
}
|
|
}
|
|
|
|
func TestCollapseTitleMilestonesKeepDeterministicOrderAndDeath(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.Titles = []content.Title{{ID: "worlds_fallen", Name: "World's Fallen", Scope: "world", Requirements: []content.TitleRequirement{{Metric: "deaths", AtLeast: 1}}, Effects: content.TitleEffects{Damage: -1}}}
|
|
other, err := w.Join("other", "Other")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.Online = false
|
|
ids := []string{c.View.Entity.ID, other.View.Entity.ID}
|
|
sort.Strings(ids)
|
|
w.Advance(w.CreatedAt.Add(time.Duration(w.Balance.SeasonSeconds+w.Balance.CollapseSeconds+1)*time.Second), .05)
|
|
if w.Phase != "lost" || len(w.PendingTitles) != 2 {
|
|
t.Fatal("collapse did not queue death milestones")
|
|
}
|
|
for i, id := range ids {
|
|
if w.PendingTitles[i].CharacterID != id || w.Characters[id].View.Entity.State != "dead" || len(w.Characters[id].View.Titles) != 0 {
|
|
t.Fatal("simultaneous deaths changed award order, life state, or prematurely granted title")
|
|
}
|
|
w.kill(w.Characters[id], "duplicate")
|
|
}
|
|
if len(w.PendingTitles) != 2 {
|
|
t.Fatal("duplicate death milestone")
|
|
}
|
|
}
|