server/armor_test.go (11008B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "net" 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 func newArmorWorld(t *testing.T) (*server, *combatTestConn) { 14 t.Helper() 15 world, conn := newEquipmentWorld(t) 16 world.characters[1].stats.Abilities.Strength, world.characters[1].stats.Abilities.Agility = 8, 8 17 for _, template := range []uint32{85686, 85638} { 18 if granted, err := world.grantItem(world.characters[1], template); err != nil || !granted { 19 t.Fatalf("grant armor: %v %v", granted, err) 20 } 21 } 22 return world, conn 23 } 24 25 func TestArmorNativeMoveDelayAndRestart(t *testing.T) { 26 world, conn := newArmorWorld(t) 27 observer := &combatTestConn{} 28 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 29 world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true, players: map[uint32]bool{1: true}} 30 before, err := readTestItemStore(world) 31 if err != nil { 32 t.Fatal(err) 33 } 34 request := inventoryMovePacket(1, 65, 21)[20:] 35 36 started := time.Now() 37 if err := world.handleInventory(world.clients[1], request); err != nil { 38 t.Fatal(err) 39 } 40 finish := world.clients[1].equipment.readyAt 41 if finish.Before(started.Add(100*time.Millisecond)) || finish.After(time.Now().Add(100*time.Millisecond)) { 42 t.Fatalf("armor delay differs from the catalog: %v", finish.Sub(started)) 43 } 44 world.handleInventory(world.clients[1], request) 45 world.advanceEquipment(finish.Add(-time.Nanosecond)) 46 unchanged, err := readTestItemStore(world) 47 if err != nil || !bytes.Equal(before, unchanged) || len(conn.packets) != 0 { 48 t.Fatalf("pending wear changed state: %v", err) 49 } 50 world.advanceEquipment(finish) 51 restarted := newTestInventoryServer(t) 52 53 if !reflect.DeepEqual(restarted.characters[1].inventory.Items, []ownedItem{{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}, {ID: 2, Type: 51022, Template: 85686, Quality: 1, Slot: 21, Count: 1}, {ID: 3, Type: 51022, Template: 85638, Quality: 1, Slot: 66, Count: 1}}) { 54 t.Fatalf("armor identity or slot did not persist: %+v", restarted.characters[1].inventory) 55 } 56 if len(conn.packets) != 2 || len(observer.packets) != 1 || 57 binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x47537a24 || 58 binary.BigEndian.Uint32(conn.packets[0][45:49]) != 21 || 59 binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x35505644 || 60 !bytes.Equal(conn.packets[1][16:], observer.packets[0][16:]) { 61 t.Fatalf("armor replication: owner=%x observer=%x", conn.packets, observer.packets) 62 } 63 var expected []byte 64 for _, value := range []uint32{85686, 85686, 1, 1, 6, 102, 21, 0, 0} { 65 expected = binary.BigEndian.AppendUint32(expected, value) 66 } 67 if !bytes.Equal(conn.packets[1][29:65], expected) { 68 t.Fatalf("native wear payload: %x", conn.packets[1]) 69 } 70 } 71 72 func TestArmorTwoPiecesAndRemovalNotifyOnlyChangedItem(t *testing.T) { 73 for _, slot := range []uint32{21, 30} { 74 t.Run(map[uint32]string{21: "chest", 30: "boots"}[slot], func(t *testing.T) { 75 world, conn := newArmorWorld(t) 76 for _, move := range [][2]uint32{{65, 21}, {66, 30}} { 77 world.handleInventory(world.clients[1], inventoryMovePacket(1, move[0], move[1])[20:]) 78 world.advanceEquipment(world.clients[1].equipment.readyAt) 79 } 80 initial := equippedItemPackets(world.characters[1]) 81 conn.packets = nil 82 83 world.handleInventory(world.clients[1], itemMovePacket(1, slot, 111)[20:]) 84 world.advanceEquipment(world.clients[1].equipment.readyAt) 85 world.handleInventory(world.clients[1], itemMovePacket(1, slot, 111)[20:]) 86 restarted := newTestInventoryServer(t) 87 88 if len(initial) != 2 || len(conn.packets) != 4 || 89 binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x35505644 || 90 binary.BigEndian.Uint32(conn.packets[0][45:49]) != 7 || 91 binary.BigEndian.Uint32(conn.packets[0][53:57]) != slot || 92 binary.BigEndian.Uint32(conn.packets[1][29:33]) != 102 || 93 binary.BigEndian.Uint32(conn.packets[1][45:49]) != 111 { 94 t.Fatalf("removal repeated or changed another modifier: %x", conn.packets) 95 } 96 for index, action := range []uint32{146, 112} { 97 packet := conn.packets[index+2] 98 if binary.BigEndian.Uint32(packet[16:20]) != 0x5e477770 || binary.BigEndian.Uint32(packet[29:33]) != action || 99 binary.BigEndian.Uint32(packet[33:37]) != 102 || binary.BigEndian.Uint32(packet[37:41]) != slot { 100 t.Fatalf("stale removal did not correct only its empty armor row: %x", packet) 101 } 102 } 103 packets := equippedItemPackets(restarted.characters[1]) 104 other := uint32(30) 105 if slot == 30 { 106 other = 21 107 } 108 if len(packets) != 1 || binary.BigEndian.Uint32(packets[0][60:64]) != other { 109 t.Fatalf("reconnect restored removed armor or lost the other piece: %x", packets) 110 } 111 if len(restarted.characters[1].inventory.Items) != 3 { 112 t.Fatal("armor removal lost an item") 113 } 114 }) 115 } 116 } 117 118 func TestArmorRejectsRequirementsAndInterruptedWear(t *testing.T) { 119 for _, scenario := range []string{"strength", "agility", "wrong slot", "unsupported quality", "unsupported identity", "changed requirement", "failed save"} { 120 t.Run(scenario, func(t *testing.T) { 121 world, _ := newArmorWorld(t) 122 player := world.characters[1] 123 destination := uint32(21) 124 switch scenario { 125 case "strength": 126 player.stats.Abilities.Strength = 7 127 case "agility": 128 player.stats.Abilities.Agility = 7 129 case "wrong slot": 130 destination = 30 131 case "unsupported quality": 132 player.inventory.Items[1].Quality = 2 133 case "unsupported identity": 134 player.inventory.Items[1].Type = 51018 135 } 136 before := append([]ownedItem(nil), player.inventory.Items...) 137 138 world.handleInventory(world.clients[1], inventoryMovePacket(1, 65, destination)[20:]) 139 if scenario == "changed requirement" || scenario == "failed save" { 140 if world.clients[1].equipment == nil { 141 t.Fatal("valid armor move was rejected") 142 } 143 if scenario == "changed requirement" { 144 player.stats.Abilities.Agility = 7 145 } else { 146 failDatabaseWrites(t, world) 147 } 148 world.advanceEquipment(world.clients[1].equipment.readyAt) 149 } 150 151 if world.clients[1].equipment != nil || !reflect.DeepEqual(before, player.inventory.Items) { 152 t.Fatalf("rejected armor move changed ownership: %+v", player.inventory) 153 } 154 }) 155 } 156 } 157 158 func TestArmorNativeSlotsAndForgedPage(t *testing.T) { 159 world, conn := newArmorWorld(t) 160 body := inventoryMovePacket(1, 65, 21)[20:] 161 binary.BigEndian.PutUint32(body[20:24], 49) 162 decodeInventory1491(body) 163 164 if err := world.handleInventory(world.clients[1], body); err != nil { 165 t.Fatal(err) 166 } 167 world.advanceEquipment(world.clients[1].equipment.readyAt) 168 conn.packets = nil 169 forged := itemMovePacket(1, 21, 111)[20:] 170 binary.BigEndian.PutUint32(forged[16:20], 104) 171 world.handleInventory(world.clients[1], forged) 172 world.clients[1].send(fullCharacterPacket(world.characters[1])) 173 174 if world.characters[1].inventory.Items[1].Slot != 21 || world.clients[1].equipment != nil || len(conn.packets) != 1 || 175 binary.BigEndian.Uint32(conn.packets[0][69:73]) != 21 { 176 t.Fatalf("armor native slot or page validation: %x %+v", conn.packets, world.characters[1].inventory) 177 } 178 } 179 180 func TestArmorCatalogProtectionAndTexture(t *testing.T) { 181 for _, item := range []struct { 182 template uint32 183 armor [8]int32 184 texture int32 185 category int32 186 }{ 187 {85685, [8]int32{1000, 875, 1000, 750, 150, 150, 150, 150}, 8736, 1}, 188 {85686, [8]int32{5, 5, 5, 5, 1, 1, 1, 1}, 8736, 1}, 189 {85638, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}, 9424, 2}, 190 } { 191 t.Run(gamedata.ItemTemplates[item.template].Name, func(t *testing.T) { 192 var armor [8]int32 193 var texture, category int32 194 195 for _, section := range gamedata.ItemTemplates[item.template].Sections { 196 if section.Type != 2 || section.Subtype != 14 { 197 continue 198 } 199 for _, function := range *section.Functions { 200 switch function.ID { 201 case 53045: 202 armor[*function.Arguments[0].Integer-90] += *function.Arguments[1].Integer 203 case 53039: 204 texture, category = *function.Arguments[0].Integer, *function.Arguments[1].Integer 205 } 206 } 207 } 208 209 if armor != item.armor || texture != item.texture || category != item.category { 210 t.Fatalf("catalog wear effects differ: %v %d/%d", armor, texture, category) 211 } 212 }) 213 } 214 } 215 216 func TestArmorFullInventoryRetainsEquippedPiece(t *testing.T) { 217 world, conn := newArmorWorld(t) 218 world.handleInventory(world.clients[1], inventoryMovePacket(1, 65, 21)[20:]) 219 world.advanceEquipment(world.clients[1].equipment.readyAt) 220 for range 28 { 221 if granted, err := world.grantItem(world.characters[1], 121567); err != nil || !granted { 222 t.Fatalf("fill inventory: %v %v", granted, err) 223 } 224 } 225 before, err := readTestItemStore(world) 226 if err != nil { 227 t.Fatal(err) 228 } 229 conn.packets = nil 230 231 err = world.handleInventory(world.clients[1], itemMovePacket(1, 21, 111)[20:]) 232 after, readErr := readTestItemStore(world) 233 234 if err != nil || readErr != nil || !bytes.Equal(before, after) || world.clients[1].equipment != nil || len(conn.packets) != 1 || 235 !bytes.Equal(conn.packets[0], targetPacket(characterActionPacket(1, [5]uint32{146, 102, 21}))) { 236 t.Fatalf("full inventory changed armor or removed its effects: %v %v %x", err, readErr, conn.packets) 237 } 238 } 239 240 func TestArmorReconnectUsesInventoryWithoutRepeatingWear(t *testing.T) { 241 world, _ := newArmorWorld(t) 242 for _, move := range [][2]uint32{{65, 21}, {66, 30}} { 243 world.handleInventory(world.clients[1], inventoryMovePacket(1, move[0], move[1])[20:]) 244 world.advanceEquipment(world.clients[1].equipment.readyAt) 245 } 246 world = newTestInventoryServer(t) 247 startTestService(t, world) 248 client, connection := net.Pipe() 249 defer client.Close() 250 client.SetDeadline(time.Now().Add(5 * time.Second)) 251 done := make(chan error, 1) 252 go func() { defer connection.Close(); done <- world.serveWorldConnection(connection) }() 253 cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}) 254 binary.BigEndian.PutUint16(cookie, 0xdfdf) 255 256 client.Write(cookie) 257 var slots []uint32 258 inventorySeen := false 259 for { 260 packet, err := readPacket(client) 261 if err != nil { 262 t.Fatal(err) 263 } 264 key := binary.BigEndian.Uint32(packet[16:20]) 265 if key == 0x29304349 { 266 inventorySeen = true 267 if binary.BigEndian.Uint32(packet[69:73]) != 21 || binary.BigEndian.Uint32(packet[101:105]) != 30 { 268 t.Fatalf("reconnect inventory omitted armor slots: %x", packet) 269 } 270 } 271 if key == 0x35505644 { 272 if !inventorySeen || binary.BigEndian.Uint32(packet[45:49]) != 6 { 273 t.Fatalf("wear before owner inventory: %x", packet) 274 } 275 slots = append(slots, binary.BigEndian.Uint32(packet[53:57])) 276 } 277 if key == 0x5f52412e { 278 break 279 } 280 } 281 client.Close() 282 err := <-done 283 284 if err != nil || !inventorySeen || len(slots) != 0 { 285 t.Fatalf("reconnect repeated armor wear: slots=%v err=%v", slots, err) 286 } 287 } 288 289 func TestArmorPlayerSpawnEnablesNativeClothingChanges(t *testing.T) { 290 player := &character{id: 1, appearance: 0x515, level: 1} 291 292 packet := targetPacket(characterPacket(player)) 293 294 if binary.BigEndian.Uint32(packet[30:34])&8 == 0 { 295 t.Fatal("player spawn disabled native clothing changes") 296 } 297 }