server/omni_pol_contracts_test.go (10969B)
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 TestOmniPolGeneratedPickupKeepsSelectedQuality(t *testing.T) { 13 world, output := newKillExperienceWorld(t, "unarmed") 14 player, client := world.characters[1], world.clients[1] 15 player.appearance = 0x515 16 player.stats.Abilities.Stamina, player.stats.Abilities.Strength = 274, 249 17 if err := world.saveUser(player); err != nil { 18 t.Fatal(err) 19 } 20 // Controlled membership tests delivery, not historical OmniPol creature drops. 21 profile := *world.characters[1000000].combatProfile 22 profile.Loot = []gamedata.NPCLoot{{Template: 22196, Quality: [2]uint32{100, 100}, Chance: 100}} 23 world.characters[1000000].combatProfile = &profile 24 world.combatRoll = func(uint32) uint32 { return 0 } 25 identity := world.nextItem + 1 26 var wire []byte 27 for _, value := range []uint32{identity, 85591, 22196, 100, 0} { 28 wire = binary.BigEndian.AppendUint32(wire, value) 29 } 30 31 attackForExperience(t, world, "unarmed") 32 if err := world.handleLootUse(client, lootUseRequest(1)); err != nil { 33 t.Fatal(err) 34 } 35 shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) }) 36 for range 2 { 37 if err := world.handleInventory(client, lootTakeRequest()); err != nil { 38 t.Fatal(err) 39 } 40 } 41 world = newTestInventoryServer(t) 42 client = &worldClient{id: 1, conn: output, target: true, ready: true} 43 world.clients[1] = client 44 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil { 45 t.Fatal("picked-up gloves did not start wear", err) 46 } 47 world.advanceEquipment(client.equipment.readyAt) 48 worn := newTestInventoryServer(t) 49 if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil { 50 t.Fatal("picked-up gloves did not start removal", err) 51 } 52 world.advanceEquipment(client.equipment.readyAt) 53 removed := newTestInventoryServer(t) 54 55 expected := ownedItem{ID: identity, Type: 51022, Template: 22196, Quality: 100, Slot: 24, Count: 1} 56 if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 { 57 t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC") 58 } 59 expected.Slot = 64 60 if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 || 61 len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity { 62 t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves") 63 } 64 } 65 66 func TestOmniPolArmorQualitiesWearRemoveAndRecover(t *testing.T) { 67 for _, scenario := range []struct { 68 low, high, slot, quality, value uint32 69 stamina, strength uint16 70 armor [8]int32 71 }{ 72 {85505, 22331, 27, 1, 1031, 9, 9, [8]int32{3, 3, 3, 3, 3, 1, 3, 1}}, 73 {85505, 22331, 27, 2, 1055, 12, 11, [8]int32{6, 6, 6, 6, 6, 2, 5, 2}}, 74 {85505, 22331, 27, 20, 9968, 60, 55, [8]int32{62, 62, 62, 55, 55, 16, 47, 16}}, 75 {85505, 22331, 27, 100, 243682, 273, 248, [8]int32{312, 312, 312, 273, 273, 78, 234, 78}}, 76 {85505, 22331, 27, 200, 981463, 540, 490, [8]int32{625, 625, 625, 546, 546, 156, 468, 156}}, 77 {85552, 22246, 18, 1, 1237, 9, 9, [8]int32{3, 3, 3, 3, 3, 1, 3, 1}}, 78 {85552, 22246, 18, 2, 1266, 12, 11, [8]int32{7, 7, 7, 6, 6, 2, 6, 2}}, 79 {85552, 22246, 18, 20, 11962, 60, 55, [8]int32{74, 74, 74, 65, 65, 19, 56, 19}}, 80 {85552, 22246, 18, 100, 292417, 273, 248, [8]int32{375, 375, 375, 328, 328, 94, 281, 94}}, 81 {85552, 22246, 18, 200, 1177756, 540, 490, [8]int32{750, 750, 750, 656, 656, 187, 562, 187}}, 82 {85591, 22196, 24, 1, 412, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 83 {85591, 22196, 24, 2, 421, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 1}}, 84 {85591, 22196, 24, 20, 3987, 60, 55, [8]int32{25, 25, 25, 22, 22, 7, 19, 7}}, 85 {85591, 22196, 24, 100, 97472, 273, 248, [8]int32{125, 125, 125, 109, 109, 31, 94, 31}}, 86 {85591, 22196, 24, 101, 99443, 276, 251, [8]int32{126, 126, 126, 110, 110, 32, 94, 32}}, 87 {85591, 22196, 24, 199, 388652, 537, 488, [8]int32{249, 249, 249, 217, 217, 62, 186, 62}}, 88 {85591, 22196, 24, 200, 392585, 540, 490, [8]int32{250, 250, 250, 218, 218, 62, 187, 62}}, 89 {85631, 22142, 30, 1, 618, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 90 {85631, 22142, 30, 2, 632, 12, 11, [8]int32{3, 3, 3, 3, 3, 1, 2, 1}}, 91 {85631, 22142, 30, 20, 5980, 60, 55, [8]int32{37, 37, 37, 32, 32, 10, 28, 10}}, 92 {85631, 22142, 30, 100, 146208, 273, 248, [8]int32{187, 187, 187, 164, 164, 47, 140, 47}}, 93 {85631, 22142, 30, 200, 588878, 540, 490, [8]int32{375, 375, 375, 328, 328, 93, 281, 93}}, 94 {85679, 22064, 21, 1, 1650, 9, 9, [8]int32{5, 5, 5, 5, 5, 1, 5, 1}}, 95 {85679, 22064, 21, 2, 1689, 12, 11, [8]int32{10, 10, 10, 9, 9, 2, 9, 2}}, 96 {85679, 22064, 21, 20, 15950, 60, 55, [8]int32{100, 100, 100, 88, 88, 25, 76, 25}}, 97 {85679, 22064, 21, 100, 389891, 273, 248, [8]int32{500, 500, 500, 438, 438, 125, 376, 125}}, 98 {85679, 22064, 21, 200, 1570342, 540, 490, [8]int32{1000, 1000, 1000, 875, 875, 250, 750, 250}}, 99 {85735, 21955, 25, 1, 412, 9, 9, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}}, 100 {85735, 21955, 25, 2, 421, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 1}}, 101 {85735, 21955, 23, 2, 421, 12, 11, [8]int32{2, 2, 2, 2, 2, 1, 2, 1}}, 102 {85735, 21955, 25, 20, 3987, 60, 55, [8]int32{25, 25, 25, 22, 22, 7, 19, 7}}, 103 {85735, 21955, 25, 100, 97472, 273, 248, [8]int32{125, 125, 125, 109, 109, 31, 94, 31}}, 104 {85735, 21955, 25, 200, 392585, 540, 490, [8]int32{250, 250, 250, 218, 218, 62, 187, 62}}, 105 } { 106 for _, anchor := range []uint32{scenario.low, scenario.high} { 107 t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) { 108 world, output := newEquipmentWorld(t) 109 player, client := world.characters[1], world.clients[1] 110 player.appearance = 0x515 111 player.stats.Abilities.Stamina, player.stats.Abilities.Strength = scenario.stamina+1, scenario.strength+1 112 if err := world.saveUser(player); err != nil { 113 t.Fatal(err) 114 } 115 if granted, err := world.grantItem(player, anchor); err != nil || !granted { 116 t.Fatal("grant armor", granted, err) 117 } 118 player.inventory.Items[1].Quality = scenario.quality 119 if err := world.saveInventory(player, player.inventory); err != nil { 120 t.Fatal(err) 121 } 122 weapon := player.inventory.Items[0] 123 124 for _, ability := range []*uint16{&player.stats.Abilities.Stamina, &player.stats.Abilities.Strength} { 125 *ability -= 1 126 if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 { 127 t.Fatal("threshold equality equipped or lost the armor", err) 128 } 129 *ability += 1 130 } 131 for _, side := range []uint16{0, 1} { 132 player.appearance = 0x115 | side<<9 133 if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 { 134 t.Fatal("non-Omni character equipped or lost faction armor", side, err) 135 } 136 } 137 player.appearance = 0x515 138 output.packets = nil 139 if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil { 140 t.Fatal("qualified armor did not start wear", err) 141 } 142 world.advanceEquipment(client.equipment.readyAt) 143 worn := newTestInventoryServer(t) 144 low, high := scenario.low, scenario.high 145 if scenario.quality == 1 { 146 high = low 147 } else if scenario.quality == 200 { 148 low = high 149 } 150 var action []byte 151 for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} { 152 action = binary.BigEndian.AppendUint32(action, word) 153 } 154 if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) { 155 t.Fatal("native wear lost the selected quality or endpoint pair") 156 } 157 if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil { 158 t.Fatal("armor did not start removal", err) 159 } 160 world.advanceEquipment(client.equipment.readyAt) 161 removed := newTestInventoryServer(t) 162 163 expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1} 164 if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value { 165 t.Fatal("cold armor identity, quality, value or unrelated item changed") 166 } 167 expected.Slot = 65 168 if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 { 169 t.Fatal("removal lost or replaced an instance") 170 } 171 for index, bonus := range scenario.armor { 172 if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 { 173 t.Fatal("selected armor effects or their removal changed", index) 174 } 175 } 176 }) 177 } 178 } 179 } 180 181 func TestOmniPolSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) { 182 for _, scenario := range []struct{ quality, sale uint32 }{ 183 {2, 21}, {100, 4873}, {101, 4972}, {200, 19629}, 184 } { 185 t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) { 186 world, _ := newShopWorld(t) 187 player, client := world.characters[1], world.clients[1] 188 if granted, err := world.grantItem(player, 85591); err != nil || !granted { 189 t.Fatal(granted, err) 190 } 191 player.inventory.Items[1].Quality = scenario.quality 192 if err := world.saveInventory(player, player.inventory); err != nil { 193 t.Fatal(err) 194 } 195 before := append([]ownedItem(nil), player.inventory.Items...) 196 offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:] 197 198 if err := world.handleTrade(client, offer); err != nil { 199 t.Fatal(err) 200 } 201 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil { 202 t.Fatal(err) 203 } 204 cancelled := newTestInventoryServer(t).characters[1].inventory 205 if err := world.handleShopUse(client, shopUseRequest(2)); err != nil { 206 t.Fatal(err) 207 } 208 if err := world.handleTrade(client, offer); err != nil { 209 t.Fatal(err) 210 } 211 for range 2 { 212 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil { 213 t.Fatal(err) 214 } 215 } 216 sold := newTestInventoryServer(t).characters[1].inventory 217 218 if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 { 219 t.Fatal("cancelled sale changed the selected instance or wallet", cancelled) 220 } 221 if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale { 222 t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold) 223 } 224 }) 225 } 226 }