aorecomp

Anarchy Online classic in the browser

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


server/leather_armor_test.go (10389B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 )
     11 
     12 func TestLeatherGeneratedPickupKeepsSelectedQuality(t *testing.T) {
     13 	world, output := newKillExperienceWorld(t, "unarmed")
     14 	player, client := world.characters[1], world.clients[1]
     15 	player.stats.Abilities.Sense, player.stats.Abilities.Agility = 258, 233
     16 	if err := world.saveUser(player); err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	// Controlled membership tests delivery, not historical Leather creature drops.
     20 	profile := *world.characters[1000000].combatProfile
     21 	profile.Loot = []gamedata.NPCLoot{{Template: 85601, Quality: [2]uint32{100, 100}, Chance: 100}}
     22 	world.characters[1000000].combatProfile = &profile
     23 	world.combatRoll = func(uint32) uint32 { return 0 }
     24 	identity := world.nextItem + 1
     25 	var wire []byte
     26 	for _, value := range []uint32{identity, 85602, 85601, 100, 0} {
     27 		wire = binary.BigEndian.AppendUint32(wire, value)
     28 	}
     29 
     30 	attackForExperience(t, world, "unarmed")
     31 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) })
     35 	for range 2 {
     36 		if err := world.handleInventory(client, lootTakeRequest()); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	world = newTestInventoryServer(t)
     41 	client = &worldClient{id: 1, conn: output, target: true, ready: true}
     42 	world.clients[1] = client
     43 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil {
     44 		t.Fatal("picked-up gloves did not start wear", err)
     45 	}
     46 	world.advanceEquipment(client.equipment.readyAt)
     47 	worn := newTestInventoryServer(t)
     48 	if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil {
     49 		t.Fatal("picked-up gloves did not start removal", err)
     50 	}
     51 	world.advanceEquipment(client.equipment.readyAt)
     52 	removed := newTestInventoryServer(t)
     53 
     54 	expected := ownedItem{ID: identity, Type: 51022, Template: 85601, Quality: 100, Slot: 24, Count: 1}
     55 	if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 {
     56 		t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC")
     57 	}
     58 	expected.Slot = 64
     59 	if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 ||
     60 		len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity {
     61 		t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves")
     62 	}
     63 }
     64 
     65 func TestLeatherArmorQualitiesWearRemoveAndRecover(t *testing.T) {
     66 	for _, scenario := range []struct {
     67 		low, high, slot, quality, value uint32
     68 		sense, agility                  uint16
     69 		armor                           [8]int32
     70 	}{
     71 		{85481, 85690, 21, 1, 1000, 7, 7, [8]int32{5, 5, 1, 1, 5, 5, 1, 5}},
     72 		{85481, 85690, 21, 2, 1024, 10, 9, [8]int32{9, 10, 2, 2, 9, 9, 2, 6}},
     73 		{85481, 85690, 21, 100, 236297, 257, 232, [8]int32{438, 500, 75, 75, 438, 438, 75, 77}},
     74 		{85481, 85690, 21, 200, 951722, 510, 460, [8]int32{875, 1000, 150, 150, 875, 875, 150, 150}},
     75 		{85521, 85520, 27, 1, 625, 7, 7, [8]int32{3, 3, 1, 1, 3, 3, 1, 1}},
     76 		{85521, 85520, 27, 2, 640, 10, 9, [8]int32{6, 6, 1, 1, 6, 6, 1, 1}},
     77 		{85521, 85520, 27, 100, 147686, 257, 232, [8]int32{273, 312, 47, 47, 273, 273, 47, 47}},
     78 		{85521, 85520, 27, 200, 594826, 510, 460, [8]int32{546, 625, 93, 93, 546, 546, 93, 93}},
     79 		{85562, 85561, 18, 1, 750, 7, 7, [8]int32{3, 3, 1, 1, 3, 3, 1, 1}},
     80 		{85562, 85561, 18, 2, 768, 10, 9, [8]int32{6, 7, 2, 2, 6, 6, 2, 2}},
     81 		{85562, 85561, 18, 100, 177223, 257, 232, [8]int32{328, 375, 56, 56, 328, 328, 56, 56}},
     82 		{85562, 85561, 18, 200, 713791, 510, 460, [8]int32{656, 750, 112, 112, 656, 656, 112, 112}},
     83 		{85602, 85601, 24, 1, 250, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     84 		{85602, 85601, 24, 2, 255, 10, 9, [8]int32{2, 2, 1, 1, 2, 2, 1, 1}},
     85 		{85602, 85601, 24, 5, 346, 17, 16, [8]int32{5, 6, 2, 2, 5, 5, 2, 2}},
     86 		{85602, 85601, 24, 100, 59074, 257, 232, [8]int32{109, 125, 19, 19, 109, 109, 19, 19}},
     87 		{85602, 85601, 24, 101, 60268, 260, 235, [8]int32{110, 126, 19, 19, 110, 110, 19, 19}},
     88 		{85602, 85601, 24, 199, 235547, 507, 458, [8]int32{217, 249, 37, 37, 217, 217, 37, 37}},
     89 		{85602, 85601, 24, 200, 237930, 510, 460, [8]int32{218, 250, 37, 37, 218, 218, 37, 37}},
     90 		{85645, 85644, 30, 1, 375, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     91 		{85645, 85644, 30, 2, 384, 10, 9, [8]int32{3, 3, 1, 1, 3, 3, 1, 1}},
     92 		{85645, 85644, 30, 100, 88611, 257, 232, [8]int32{164, 187, 28, 28, 164, 164, 28, 28}},
     93 		{85645, 85644, 30, 200, 356895, 510, 460, [8]int32{328, 375, 56, 56, 328, 328, 56, 56}},
     94 		{85697, 85696, 21, 1, 1000, 7, 7, [8]int32{5, 5, 1, 1, 5, 5, 1, 1}},
     95 		{85697, 85696, 21, 2, 1024, 10, 9, [8]int32{9, 10, 2, 2, 9, 9, 2, 2}},
     96 		{85697, 85696, 21, 100, 236297, 257, 232, [8]int32{438, 500, 75, 75, 438, 438, 75, 75}},
     97 		{85697, 85696, 21, 200, 951722, 510, 460, [8]int32{875, 1000, 150, 150, 875, 875, 150, 150}},
     98 		{85750, 85749, 25, 1, 250, 7, 7, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     99 		{85750, 85749, 25, 2, 255, 10, 9, [8]int32{2, 2, 1, 1, 2, 2, 1, 1}},
    100 		{85750, 85749, 23, 2, 255, 10, 9, [8]int32{2, 2, 1, 1, 2, 2, 1, 1}},
    101 		{85750, 85749, 25, 100, 59074, 257, 232, [8]int32{109, 125, 19, 19, 109, 109, 19, 19}},
    102 		{85750, 85749, 25, 200, 237930, 510, 460, [8]int32{218, 250, 37, 37, 218, 218, 37, 37}},
    103 	} {
    104 		for _, anchor := range []uint32{scenario.low, scenario.high} {
    105 			t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) {
    106 				world, output := newEquipmentWorld(t)
    107 				player, client := world.characters[1], world.clients[1]
    108 				player.stats.Abilities.Sense, player.stats.Abilities.Agility = scenario.sense+1, scenario.agility+1
    109 				if err := world.saveUser(player); err != nil {
    110 					t.Fatal(err)
    111 				}
    112 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
    113 					t.Fatal("grant armor", granted, err)
    114 				}
    115 				player.inventory.Items[1].Quality = scenario.quality
    116 				if err := world.saveInventory(player, player.inventory); err != nil {
    117 					t.Fatal(err)
    118 				}
    119 				weapon := player.inventory.Items[0]
    120 
    121 				for _, ability := range []*uint16{&player.stats.Abilities.Sense, &player.stats.Abilities.Agility} {
    122 					*ability -= 1
    123 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
    124 						t.Fatal("threshold equality equipped or lost the armor", err)
    125 					}
    126 					*ability += 1
    127 				}
    128 				output.packets = nil
    129 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
    130 					t.Fatal("qualified armor did not start wear", err)
    131 				}
    132 				world.advanceEquipment(client.equipment.readyAt)
    133 				worn := newTestInventoryServer(t)
    134 				low, high := scenario.low, scenario.high
    135 				if scenario.quality == 1 {
    136 					high = low
    137 				} else if scenario.quality == 200 {
    138 					low = high
    139 				}
    140 				var action []byte
    141 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
    142 					action = binary.BigEndian.AppendUint32(action, word)
    143 				}
    144 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
    145 					t.Fatal("native wear lost the selected quality or endpoint pair")
    146 				}
    147 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
    148 					t.Fatal("armor did not start removal", err)
    149 				}
    150 				world.advanceEquipment(client.equipment.readyAt)
    151 				removed := newTestInventoryServer(t)
    152 
    153 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
    154 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
    155 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
    156 				}
    157 				expected.Slot = 65
    158 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    159 					t.Fatal("removal lost or replaced an instance")
    160 				}
    161 				for index, bonus := range scenario.armor {
    162 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    163 						t.Fatal("selected armor effects or their removal changed", index)
    164 					}
    165 				}
    166 			})
    167 		}
    168 	}
    169 }
    170 
    171 func TestLeatherSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) {
    172 	for _, scenario := range []struct{ quality, sale uint32 }{
    173 		{2, 12}, {100, 2953}, {101, 3013}, {200, 11896},
    174 	} {
    175 		t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) {
    176 			world, _ := newShopWorld(t)
    177 			player, client := world.characters[1], world.clients[1]
    178 			if granted, err := world.grantItem(player, 85602); err != nil || !granted {
    179 				t.Fatal(granted, err)
    180 			}
    181 			player.inventory.Items[1].Quality = scenario.quality
    182 			if err := world.saveInventory(player, player.inventory); err != nil {
    183 				t.Fatal(err)
    184 			}
    185 			before := append([]ownedItem(nil), player.inventory.Items...)
    186 			offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]
    187 
    188 			if err := world.handleTrade(client, offer); err != nil {
    189 				t.Fatal(err)
    190 			}
    191 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    192 				t.Fatal(err)
    193 			}
    194 			cancelled := newTestInventoryServer(t).characters[1].inventory
    195 			if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    196 				t.Fatal(err)
    197 			}
    198 			if err := world.handleTrade(client, offer); err != nil {
    199 				t.Fatal(err)
    200 			}
    201 			for range 2 {
    202 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    203 					t.Fatal(err)
    204 				}
    205 			}
    206 			sold := newTestInventoryServer(t).characters[1].inventory
    207 
    208 			if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 {
    209 				t.Fatal("cancelled sale changed the selected instance or wallet", cancelled)
    210 			}
    211 			if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale {
    212 				t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold)
    213 			}
    214 		})
    215 	}
    216 }