aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/loot_distribution_test.go (7127B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"net/http/httptest"
      7 	"slices"
      8 	"strings"
      9 	"testing"
     10 )
     11 
     12 func TestDistributedLootCommitsOneItemPerPoolAndPickup(t *testing.T) {
     13 	world, _ := newKillExperienceWorld(t, "unarmed")
     14 	group := gamedata.NPCGroups["127/district/1/fixed/33"]
     15 	target := world.characters[1000000]
     16 	target.npcDefinitionID, target.monsterData, target.level, target.appearance = group.ID, group.MonsterData, 20, group.Appearance
     17 	target.combatProfile = nil
     18 	world.combatRoll = func(uint32) uint32 { return 0 }
     19 	before := world.nextItem
     20 
     21 	attackForExperience(t, world, "unarmed")
     22 	cold := newTestInventoryServer(t)
     23 	table := gamedata.NPCLootTables[group.ID]
     24 	expected := len(table.Drops)
     25 	for _, roll := range table.Pools {
     26 		if len(gamedata.LootPools[roll.Pool].Candidates[20]) > 0 {
     27 			expected++
     28 		}
     29 	}
     30 	for _, current := range []*server{world, cold} {
     31 		corpse := current.characters[target.id].corpse
     32 		if corpse == nil || len(corpse.items) != expected || current.nextItem != before+uint32(expected) || corpse.expiresAt.IsZero() || len(current.npcRespawns) == 0 {
     33 			t.Fatal("pooled rewards or corpse lifecycle not durable", corpse, expected)
     34 		}
     35 		slot := len(table.Drops)
     36 		for _, roll := range table.Pools {
     37 			if len(gamedata.LootPools[roll.Pool].Candidates[20]) == 0 {
     38 				continue
     39 			}
     40 			item := corpse.items[slot]
     41 			slot++
     42 			if item.Template != gamedata.LootPools[roll.Pool].Candidates[20][0] || !gamedata.ItemQualitySupported(item.Template, item.Quality) {
     43 				t.Fatal("pool selection or QL changed", item)
     44 			}
     45 		}
     46 	}
     47 	if !target.attackableNPC() {
     48 		t.Fatal("named humanoid loot is unreachable")
     49 	}
     50 	if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil {
     51 		t.Fatal(err)
     52 	}
     53 	for slot := 0; slot < expected; slot++ {
     54 		request := lootTakeRequest()
     55 		binary.BigEndian.PutUint32(request[20:24], uint32(slot))
     56 		if err := world.handleInventory(world.clients[1], request); err != nil {
     57 			t.Fatal(err)
     58 		}
     59 	}
     60 	cold = newTestInventoryServer(t)
     61 	if len(cold.characters[1000000].corpse.items) != 0 || len(cold.characters[1].inventory.Items) != expected {
     62 		t.Fatal("pooled pickup did not persist")
     63 	}
     64 }
     65 
     66 func TestDistributedLootFailureDoesNotCommitDeath(t *testing.T) {
     67 	world, _ := newKillExperienceWorld(t, "unarmed")
     68 	target := world.characters[1000000]
     69 	group := gamedata.NPCGroups["127/district/1/fixed/33"]
     70 	target.npcDefinitionID, target.monsterData, target.level, target.appearance = group.ID, group.MonsterData, 20, group.Appearance
     71 	target.combatProfile = nil
     72 	world.combatRoll = func(uint32) uint32 { return 0 }
     73 	if err := world.flushWorld(); err != nil {
     74 		t.Fatal(err)
     75 	}
     76 	if _, err := world.db.Exec("CREATE TRIGGER fail_pool BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'pool reward'); END"); err != nil {
     77 		t.Fatal(err)
     78 	}
     79 
     80 	attackForExperience(t, world, "unarmed")
     81 	cold := newTestInventoryServer(t)
     82 	if target.health != 1 || target.corpse != nil || cold.characters[target.id].health != 1 || cold.characters[target.id].corpse != nil {
     83 		t.Fatal("failed pool reward committed death")
     84 	}
     85 	if _, err := world.db.Exec("DROP TRIGGER fail_pool"); err != nil {
     86 		t.Fatal(err)
     87 	}
     88 	attackForExperience(t, world, "unarmed")
     89 	if target.corpse == nil || len(target.corpse.items) == 0 {
     90 		t.Fatal("retry lost distributed rewards")
     91 	}
     92 }
     93 
     94 func TestDistributedCatalogShowsItemOddsAndNamedLocations(t *testing.T) {
     95 	world := newTestServer(t)
     96 	sources := world.itemCatalog().families
     97 	disc, _ := world.itemCatalog().sourcePage(147073, 1)
     98 	source := slices.IndexFunc(disc, func(s *itemSource) bool { return s.pool == "instruction_discs" && s.chance == 15 })
     99 	if source < 0 {
    100 		t.Fatal("CellAO disc is not indexed")
    101 	}
    102 	got := disc[source]
    103 	if got.candidates[0] <= 1 || got.candidates[1] < got.candidates[0] || itemLootChance(got) == "15%" || len(got.locations) == 0 {
    104 		t.Fatal("pool chance misrepresented as per-item chance", got)
    105 	}
    106 	if itemLootChance(&itemSource{chance: 20, pool: "test", candidates: [2]int{4, 10}}) != "2-5%" {
    107 		t.Fatal("wrong pool probability range")
    108 	}
    109 	if !slices.ContainsFunc(sources[204748], func(s *itemSource) bool {
    110 		return strings.Contains(s.name, "Guardian of Tomorrow") && s.chance == 25 && s.locations[0].playfield == 1931
    111 	}) {
    112 		t.Fatal("documented Temple drop lost")
    113 	}
    114 	if !slices.ContainsFunc(sources[157856], func(s *itemSource) bool {
    115 		return strings.Contains(s.name, "Tarasque") && s.locations[0].playfield == 505 && strings.Contains(s.locations[0].name, "Camelot entrance")
    116 	}) {
    117 		t.Fatal("Tarasque reward does not link to the Camelot entrance")
    118 	}
    119 	if !slices.ContainsFunc(sources[158601], func(s *itemSource) bool {
    120 		return strings.Contains(s.name, "Tarasque") && strings.Contains(s.evidence, "item 158601 description") && s.locations[0].playfield == 505 && strings.Contains(s.locations[0].name, "Camelot entrance")
    121 	}) {
    122 		t.Fatal("original description's Gall Stone source lost")
    123 	}
    124 	page := httptest.NewRecorder()
    125 	adminTestHandler(world).ServeHTTP(page, httptest.NewRequest("GET", "/admin/items/catalog/147073", nil))
    126 	for _, text := range []string{"Pool selection", "eligible families", "Item probability varies with NPC level", "admin/map?playfield="} {
    127 		if !strings.Contains(page.Body.String(), text) {
    128 			t.Fatal("pool detail missing", text)
    129 		}
    130 	}
    131 	if strings.Count(page.Body.String(), "<div data-world-map data-map-overview") != 1 {
    132 		t.Fatal("missing single source overview")
    133 	}
    134 }
    135 
    136 func TestDistributionUsesAppropriateVendingDepartments(t *testing.T) {
    137 	for _, scenario := range []struct{ shop, template uint32 }{
    138 		{118050, 116644}, // Map - 4 Holes in Maps.
    139 		{121035, 130583}, // Chocolate Cake in Food.
    140 		{155599, 150449}, // Nano crystal construction book in Basic Bookstore.
    141 		{99481, 157244},  // Suppression Spray in medical supplies.
    142 		{99493, 155923},  // QL250 robot tool belongs in the superior department.
    143 	} {
    144 		if !slices.ContainsFunc(gamedata.ShopStock[scenario.shop], func(row gamedata.ShopItem) bool { return row.Template == scenario.template }) {
    145 			t.Fatal("missing department stock", scenario)
    146 		}
    147 	}
    148 	if slices.ContainsFunc(gamedata.ShopStock[99491], func(row gamedata.ShopItem) bool { return row.Template == 155923 }) {
    149 		t.Fatal("high-QL robot tool admitted to the basic department")
    150 	}
    151 }
    152 
    153 func TestShopComponentWithoutPackageSizeDeliversOne(t *testing.T) {
    154 	world, _ := newPurchaseWorld(t)
    155 	client := world.clients[1]
    156 	index := slices.IndexFunc(client.shop.stock, func(entry gamedata.ShopItem) bool { return entry.Template == 159676 })
    157 	if index < 0 {
    158 		t.Fatal("Steel Arrowheads missing from tool vendor")
    159 	}
    160 
    161 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{51035, 3221488799}, [2]uint32{111, uint32(index)})[20:]); err != nil {
    162 		t.Fatal(err)
    163 	}
    164 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    165 		t.Fatal(err)
    166 	}
    167 	cold := newTestInventoryServer(t)
    168 	if !slices.ContainsFunc(cold.characters[1].inventory.Items, func(item ownedItem) bool { return item.Template == 159676 && item.Count == 1 }) {
    169 		t.Fatal("component purchase did not deliver one durable unit")
    170 	}
    171 }