194 lines
6.6 KiB
Go
194 lines
6.6 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"encoding/json"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func gearCount(items []p.ItemInstance, definition string) int {
|
|
n := 0
|
|
for _, item := range items {
|
|
if item.DefinitionID == definition {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
func testGear(id, slot string, modifiers ...p.StatModifier) p.ItemInstance {
|
|
return p.ItemInstance{ID: id, DefinitionID: "test_" + id, Name: "Test " + id, Slot: slot, Weight: 2, Requirements: p.ItemRequirements{Level: 1, Stats: map[string]float64{}, Titles: []string{}}, Modifiers: append([]p.StatModifier{}, modifiers...)}
|
|
}
|
|
func TestEquipmentSlotsAndRequirements(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.grantXP(c, 100)
|
|
for n := 0; n < 5; n++ {
|
|
if err := w.allocateStat(c, "strength"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, slot := range equipmentSlots {
|
|
category := slot
|
|
if len(slot) == 5 && slot[:4] == "ring" {
|
|
category = "ring"
|
|
}
|
|
item := testGear("slot_"+slot, category, p.StatModifier{Stat: "strength", Flat: 1, Percent: 10})
|
|
c.View.Items = append(c.View.Items, item)
|
|
if err := w.equip(c, item.ID, slot); err != nil {
|
|
t.Fatal(slot, err)
|
|
}
|
|
}
|
|
if len(c.View.Equipment) != 21 {
|
|
t.Fatal("missing equipment slots")
|
|
}
|
|
if w.equip(c, "missing", "weapon") == nil || w.equip(c, "slot_ring1", "ring9") == nil {
|
|
t.Fatal("invalid ownership/slot accepted")
|
|
}
|
|
if w.equip(c, "slot_ring1", "ring2") == nil {
|
|
t.Fatal("same instance equipped twice")
|
|
}
|
|
if err := w.unequip(c, "helmet"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gated := testGear("gated", "helmet", p.StatModifier{Stat: "strength", Flat: 100})
|
|
gated.Requirements.Stats["strength"] = 6
|
|
c.View.Items = append(c.View.Items, gated)
|
|
if w.equip(c, gated.ID, "helmet") == nil {
|
|
t.Fatal("equipment satisfied its own prerequisite")
|
|
}
|
|
item := ownedItem(c, gated.ID)
|
|
item.Requirements.Stats["strength"] = 5
|
|
item.Requirements.Level = 3
|
|
item.Requirements.Titles = []string{"trial"}
|
|
if w.equip(c, item.ID, "helmet") == nil {
|
|
t.Fatal("missing level accepted")
|
|
}
|
|
item.Requirements.Level = 2
|
|
if w.equip(c, item.ID, "helmet") == nil {
|
|
t.Fatal("missing title accepted")
|
|
}
|
|
c.View.Titles = append(c.View.Titles, p.TitleAward{ID: "trial"})
|
|
if err := w.equip(c, item.ID, "helmet"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.View.Titles = append(c.View.Titles, p.TitleAward{ID: "curse", Effects: p.TitleEffects{Modifiers: []p.StatModifier{{Stat: "strength", Flat: -1}}}})
|
|
w.RefreshStats(c)
|
|
if c.View.Equipment["helmet"] != "" || ownedItem(c, gated.ID) == nil {
|
|
t.Fatal("invalidated gear stayed active or was lost")
|
|
}
|
|
}
|
|
func TestEquipmentDeathDropLootAndRestart(t *testing.T) {
|
|
w, c := fixture(t)
|
|
ring := testGear("unique_ring", "ring", p.StatModifier{Stat: "luck", Flat: 2})
|
|
c.View.Items = append(c.View.Items, ring)
|
|
if err := w.equip(c, ring.ID, "ring8"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := w.dropItem(c, ring.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ownedItem(c, ring.ID) != nil || c.View.Equipment["ring8"] != "" {
|
|
t.Fatal("drop retained ownership")
|
|
}
|
|
var pile string
|
|
for id := range w.Corpses {
|
|
pile = id
|
|
}
|
|
if err := w.lootCorpse(c, pile); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.lootCorpse(c, pile) == nil {
|
|
t.Fatal("looted twice")
|
|
}
|
|
if err := w.equip(c, ring.ID, "ring8"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.DailySeconds = 123
|
|
raw, _ := json.Marshal(w)
|
|
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)
|
|
}
|
|
survivor := restored.Characters[c.View.Entity.ID]
|
|
survivor.View.Entity.State = "eligible"
|
|
next := New("gear-next", 345, w.now, w.Balance)
|
|
if err := next.Import(survivor); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if survivor.DailySeconds != 123 || survivor.View.Equipment["ring8"] != ring.ID || ownedItem(survivor, ring.ID) == nil {
|
|
t.Fatal("passage changed item or daily usage")
|
|
}
|
|
count := len(survivor.View.Items)
|
|
next.kill(survivor, "test death")
|
|
corpse := next.Corpses[survivor.RevivalCorpse]
|
|
if len(corpse.Items) != count || len(survivor.View.Items) != 0 || len(survivor.View.Equipment) != 0 {
|
|
t.Fatal("death duplicated/lost items")
|
|
}
|
|
replacement, err := next.Join(c.Account, "Replacement")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
replacement.View.Entity.Position = corpse.Entity.Position
|
|
if err = next.lootCorpse(replacement, corpse.Entity.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ownedItem(replacement, ring.ID) == nil || next.Connect(survivor.View.Entity.ID) == nil {
|
|
t.Fatal("loot changed identity or resurrected life")
|
|
}
|
|
}
|
|
func TestLegacyEquipmentMigrationAndAtomicFailures(t *testing.T) {
|
|
w, c := fixture(t)
|
|
original := len(c.View.Items)
|
|
c.View.Inventory["spear"] = 2
|
|
w.Corpses["legacy"] = &Corpse{Entity: p.Entity{ID: "legacy", Position: c.View.Entity.Position}, Inventory: map[string]int64{"spear": 1, "ore": 3}}
|
|
for n := 0; n < 2; n++ {
|
|
if err := w.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(c.View.Items) != original+2 || len(w.Corpses["legacy"].Items) != 1 || c.View.Inventory["spear"] != 0 {
|
|
t.Fatal("migration duplicated/lost gear")
|
|
}
|
|
}
|
|
corpse := w.Corpses["legacy"]
|
|
corpse.Items = append(corpse.Items, c.View.Items[0])
|
|
before := c.View.Inventory["ore"]
|
|
if w.lootCorpse(c, "legacy") == nil || c.View.Inventory["ore"] != before || w.Corpses["legacy"] == nil {
|
|
t.Fatal("failed loot changed inventory")
|
|
}
|
|
if w.Restore(w.now) == nil {
|
|
t.Fatal("duplicate ID restored")
|
|
}
|
|
delete(w.Corpses, "legacy")
|
|
c.View.Inventory["wood"] = 3
|
|
c.View.Inventory["ore"] = 2
|
|
w.Balance.Crafting.Recipes[0].Outputs = map[string]int64{"spear": 100001}
|
|
if w.craft(c, "spear") == nil || c.View.Inventory["wood"] != 3 || c.View.Inventory["ore"] != 2 {
|
|
t.Fatal("failed craft consumed materials")
|
|
}
|
|
c.View.Inventory["wood"] = math.MaxInt64
|
|
w.Corpses["overflow"] = &Corpse{Entity: p.Entity{ID: "overflow", Position: c.View.Entity.Position}, Inventory: map[string]int64{"wood": 1}}
|
|
if w.lootCorpse(c, "overflow") == nil || c.View.Inventory["wood"] != math.MaxInt64 {
|
|
t.Fatal("overflow loot changed inventory")
|
|
}
|
|
}
|
|
func TestCatalogEquipmentCanDropFromSentinels(t *testing.T) {
|
|
w, c := fixture(t)
|
|
w.Balance.Items = append(w.Balance.Items, content.ItemDefinition{ID: "drop_ring", Name: "Dropped Ring", Slot: "ring", Weight: 1, DropChance: 100, Requirements: content.ItemRequirements{Level: 1}})
|
|
w.now = w.now.Add(time.Second)
|
|
w.Mobs["test"] = &Mob{Entity: p.Entity{ID: "test", HP: 1, Position: c.View.Entity.Position}}
|
|
if err := w.attack(c, "test"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gearCount(c.View.Items, "drop_ring") != 1 {
|
|
t.Fatal("authored gear never dropped")
|
|
}
|
|
if w.attack(c, "test") == nil || gearCount(c.View.Items, "drop_ring") != 1 {
|
|
t.Fatal("repeated kill duplicated gear")
|
|
}
|
|
}
|