server/implants_test.go (13032B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "reflect" 7 "testing" 8 "time" 9 ) 10 11 func newImplantWorld(t *testing.T) (*server, *combatTestConn) { 12 t.Helper() 13 world, conn := newArmorWorld(t) 14 world.characters[1].stats.NanoAiding.Treatment = 9 15 if err := world.saveUser(world.characters[1]); err != nil { 16 t.Fatal(err) 17 } 18 world.clients[1].selectedTarget = [2]uint32{50000, 1} 19 for _, template := range []uint32{43552, 101173} { 20 if granted, err := world.grantItem(world.characters[1], template); err != nil || !granted { 21 t.Fatal(granted, err) 22 } 23 } 24 return world, conn 25 } 26 27 func TestImplantClinicEquipBonusesAndRestart(t *testing.T) { 28 world, conn := newImplantWorld(t) 29 player, client := world.characters[1], world.clients[1] 30 31 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]); err != nil { 32 t.Fatal(err) 33 } 34 for _, move := range [][2]uint32{{64, 6}, {65, 21}, {66, 30}, {68, 37}} { 35 started := time.Now() 36 if err := world.handleInventory(client, inventoryMovePacket(1, move[0], move[1])[20:]); err != nil { 37 t.Fatal(err) 38 } 39 if client.equipment == nil { 40 t.Fatalf("equipment move rejected: %v", move) 41 } 42 if move[1] == 37 && (client.equipment.readyAt.Before(started.Add(100*time.Millisecond)) || client.equipment.readyAt.After(time.Now().Add(100*time.Millisecond))) { 43 t.Fatal("implant did not inherit its native 100 ms equip delay") 44 } 45 world.advanceEquipment(client.equipment.readyAt) 46 } 47 restarted := newTestInventoryServer(t) 48 restored := restarted.characters[1] 49 50 if player.inventory.equipmentBonus(91) != 14 || player.inventory.equipmentBonus(90) != 6 || player.inventory.equipmentBonus(94) != 2 || player.attackWeapon() == nil { 51 t.Fatalf("independent equipment bonuses: %+v", player.inventory.Items) 52 } 53 if !reflect.DeepEqual(restored.inventory.Items, player.inventory.Items) || restored.inventory.LastID != 5 || restored.inventory.equipmentBonus(91) != 14 || 54 restored.implantChangesUntil.UnixNano() != player.implantChangesUntil.UnixNano() || 55 restored.skillLocks[124].UnixNano() != player.skillLocks[124].UnixNano() || restored.stats.NanoAiding.Treatment != 9 { 56 t.Fatalf("implant or clinic state did not persist: %+v", restored) 57 } 58 last := conn.packets[len(conn.packets)-1] 59 if binary.BigEndian.Uint32(last[16:20]) != 0x35505644 || binary.BigEndian.Uint32(last[49:53]) != 103 || binary.BigEndian.Uint32(last[53:57]) != 37 { 60 t.Fatalf("implant wear placement: %x", last) 61 } 62 if len(equippedItemPackets(restored)) != 4 { 63 t.Fatal("observer equipment snapshot lost an item") 64 } 65 packet := fullCharacterPacket(restored) 66 // The first stat list follows the remaining-second skill-lock list. 67 cursor := 60 + len(restored.inventory.Items)*32 68 cursor += 4 + 8*int(binary.BigEndian.Uint32(packet[cursor:])/1009-1) 69 count := int(binary.BigEndian.Uint32(packet[cursor:])/1009 - 1) 70 values := make(map[uint32]uint32) 71 for offset := cursor + 4; offset < cursor+4+count*8; offset += 8 { 72 values[binary.BigEndian.Uint32(packet[offset:])] = binary.BigEndian.Uint32(packet[offset+4:]) 73 } 74 if values[124] != 9 || values[224] != 22 || values[91] != 0 { 75 t.Fatalf("FullCharacter duplicated trickle, equipment bonuses or lost access: %v", values) 76 } 77 } 78 79 func TestImplantRequirementsAndNativeSlotConversion(t *testing.T) { 80 for _, scenario := range []string{"treatment 10", "treatment 11", "strength 5", "strength 6", "no clinic", "wrong slot", "wrong quality", "wrong type", "occupied"} { 81 t.Run(scenario, func(t *testing.T) { 82 world, _ := newImplantWorld(t) 83 player, client := world.characters[1], world.clients[1] 84 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 85 destination := uint32(37) 86 switch scenario { 87 case "treatment 10": 88 player.stats.NanoAiding.Treatment = 8 89 case "strength 5": 90 player.stats.Abilities.Strength = 5 91 case "strength 6": 92 player.stats.Abilities.Strength = 6 93 case "no clinic": 94 player.implantChangesUntil = time.Time{} 95 case "wrong slot": 96 destination = 21 97 case "wrong quality": 98 player.inventory.Items[4].Quality = 200 99 case "wrong type": 100 player.inventory.Items[4].Type = 51005 101 case "occupied": 102 if granted, err := world.grantItem(player, 101173); !granted || err != nil { 103 t.Fatal(granted, err) 104 } 105 player.inventory.Items[5].Slot = 37 106 if err := world.saveInventory(player, player.inventory); err != nil { 107 t.Fatal(err) 108 } 109 } 110 request := inventoryMovePacket(1, 52, destination)[20:] 111 112 decodeInventory1491(request) 113 err := world.handleInventory(client, request) 114 accepted := client.equipment != nil 115 if accepted { 116 world.advanceEquipment(client.equipment.readyAt) 117 } 118 119 if err != nil || accepted != (scenario == "treatment 11" || scenario == "strength 6" || scenario == "occupied") { 120 t.Fatalf("implant eligibility: accepted=%v err=%v", accepted, err) 121 } 122 if scenario == "occupied" && (player.inventory.Items[4].Slot != 37 || player.inventory.Items[5].Slot != 68) { 123 t.Fatal("implant replacement did not exchange its two actual instances") 124 } 125 if scenario != "wrong slot" && binary.BigEndian.Uint32(request[32:36]) != 37 { 126 t.Fatal("ordinary inventory conversion shifted the implant destination") 127 } 128 }) 129 } 130 } 131 132 func TestImplantRemovalRetainsOtherEquipmentAndNeedsClinic(t *testing.T) { 133 world, conn := newImplantWorld(t) 134 player, client := world.characters[1], world.clients[1] 135 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 136 for _, move := range [][2]uint32{{65, 21}, {66, 30}, {68, 37}} { 137 world.handleInventory(client, inventoryMovePacket(1, move[0], move[1])[20:]) 138 world.advanceEquipment(client.equipment.readyAt) 139 } 140 deadline := player.implantChangesUntil 141 world.advanceItemEffects(deadline) 142 before := player.inventory 143 144 world.handleInventory(client, itemMovePacket(1, 37, 111)[20:]) 145 if client.equipment != nil || !reflect.DeepEqual(before, player.inventory) || player.inventory.equipmentBonus(91) != 14 { 146 t.Fatal("implant removed after surgery access expired") 147 } 148 world.advanceItemEffects(player.skillLocks[124]) 149 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 2)[20:]) 150 // Removal does not recheck the implant's install requirements. 151 player.stats.NanoAiding.Treatment, player.stats.Abilities.Strength = 0, 1 152 conn.packets = nil 153 world.handleInventory(client, itemMovePacket(1, 37, 111)[20:]) 154 world.advanceEquipment(client.equipment.readyAt) 155 restarted := newTestInventoryServer(t) 156 157 if restarted.characters[1].inventory.equipmentBonus(91) != 6 || player.inventory.equipmentBonus(90) != 6 || player.inventory.Items[4].Slot != 65 || len(player.inventory.Items) != 5 { 158 t.Fatalf("removing implant changed other equipment: %+v", player.inventory) 159 } 160 if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][45:49]) != 7 || 161 binary.BigEndian.Uint32(conn.packets[0][49:53]) != 103 || binary.BigEndian.Uint32(conn.packets[1][29:33]) != 103 { 162 t.Fatalf("native implant removal: %x", conn.packets) 163 } 164 } 165 166 func TestImplantPendingMoveRechecksClinicAndSave(t *testing.T) { 167 for _, scenario := range []string{"expired clinic", "failed save"} { 168 t.Run(scenario, func(t *testing.T) { 169 world, _ := newImplantWorld(t) 170 player, client := world.characters[1], world.clients[1] 171 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 172 world.handleInventory(client, inventoryMovePacket(1, 68, 37)[20:]) 173 finish := client.equipment.readyAt 174 if scenario == "expired clinic" { 175 player.implantChangesUntil = finish 176 } else { 177 failDatabaseWrites(t, world) 178 } 179 180 world.advanceEquipment(finish) 181 182 if player.inventory.Items[4].Slot != 68 || player.inventory.equipmentBonus(91) != 0 || client.equipment != nil { 183 t.Fatal("interrupted implant installation changed ownership or bonuses") 184 } 185 }) 186 } 187 } 188 189 func TestImplantRemovalRejectsWrongPageAndFullInventory(t *testing.T) { 190 world, _ := newImplantWorld(t) 191 player, client := world.characters[1], world.clients[1] 192 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 193 world.handleInventory(client, inventoryMovePacket(1, 68, 37)[20:]) 194 world.advanceEquipment(client.equipment.readyAt) 195 for { 196 granted, err := world.grantItem(player, 121567) 197 if err != nil { 198 t.Fatal(err) 199 } 200 if !granted { 201 break 202 } 203 } 204 before := player.inventory 205 wrongPage := itemMovePacket(1, 37, 111)[20:] 206 binary.BigEndian.PutUint32(wrongPage[16:20], 102) 207 208 world.handleInventory(client, wrongPage) 209 world.handleInventory(client, itemMovePacket(1, 37, 111)[20:]) 210 211 if client.equipment != nil || !reflect.DeepEqual(before, player.inventory) || player.inventory.equipmentBonus(91) != 8 { 212 t.Fatal("rejected removal lost an implant or its bonus") 213 } 214 } 215 216 func TestPortableClinicTracksNativeSelfSelection(t *testing.T) { 217 world, _ := newImplantWorld(t) 218 client := world.clients[1] 219 client.selectedTarget = [2]uint32{} 220 var body []byte 221 for _, value := range []uint32{50000, 1, 1} { 222 body = binary.BigEndian.AppendUint32(body, value) 223 } 224 request := n3Packet(0x2252445f, 50000, 1, body)[20:] 225 226 if err := world.handleTargetSelection(client, request); err != nil { 227 t.Fatal(err) 228 } 229 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 230 binary.BigEndian.PutUint32(request[8:12], 2) 231 binary.BigEndian.PutUint32(request[20:24], 2) 232 world.handleTargetSelection(client, request) 233 234 if world.characters[1].implantChangesUntil.IsZero() || client.selectedTarget != [2]uint32{50000, 1} { 235 t.Fatal("self selection was not used or foreign selection replaced it") 236 } 237 } 238 239 func TestPortableClinicCooldownAndExpiry(t *testing.T) { 240 world, conn := newImplantWorld(t) 241 player, client := world.characters[1], world.clients[1] 242 started := time.Now() 243 244 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 245 window, cooldown := player.implantChangesUntil, player.skillLocks[124] 246 first := append([][]byte(nil), conn.packets...) 247 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 1)[20:]) 248 world.handleOwnedItemUse(client, backpackUseRequest(1, 67, 2)[20:]) 249 world.advanceItemEffects(window.Add(-time.Nanosecond)) 250 if len(conn.packets) != 4 || !player.implantChangesUntil.Equal(window) || !player.skillLocks[124].Equal(cooldown) { 251 t.Fatal("replay or cooldown use extended clinic effects") 252 } 253 world.advanceItemEffects(window) 254 world.advanceItemEffects(cooldown) 255 world.advanceItemEffects(cooldown.Add(time.Second)) 256 257 if window.Before(started.Add(30*time.Second)) || window.After(time.Now().Add(30*time.Second)) || cooldown.Sub(window) != 570*time.Second { 258 t.Fatalf("clinic duration: window=%v cooldown=%v", window.Sub(started), cooldown.Sub(started)) 259 } 260 if len(first) != 3 || binary.BigEndian.Uint32(first[0][29:33]) != 1 || binary.BigEndian.Uint32(first[1][45:49]) != 3 || 261 binary.BigEndian.Uint32(first[1][57:61]) != 50000 || binary.BigEndian.Uint32(first[1][61:65]) != 1 || 262 binary.BigEndian.Uint32(first[2][29:33]) != 170 || binary.BigEndian.Uint32(first[2][41:45]) != 124 || binary.BigEndian.Uint32(first[2][45:49]) != 600 { 263 t.Fatalf("clinic Use or cooldown wire: %x", first) 264 } 265 if len(conn.packets) != 6 || binary.BigEndian.Uint32(conn.packets[4][29:33]) != 224 || binary.BigEndian.Uint32(conn.packets[4][33:37]) != 6 || 266 binary.BigEndian.Uint32(conn.packets[5][29:33]) != 164 || !player.implantChangesUntil.IsZero() || !player.skillLocks[124].IsZero() { 267 t.Fatalf("clinic expiry repeated or omitted: %x", conn.packets) 268 } 269 } 270 271 func TestPortableClinicRejectsInvalidUseAndFailedSave(t *testing.T) { 272 for _, scenario := range []string{"treatment 5", "no target", "other target", "foreign owner", "foreign user", "wrong type", "wrong quality", "dead", "loading", "failed save"} { 273 t.Run(scenario, func(t *testing.T) { 274 world, conn := newImplantWorld(t) 275 player, client := world.characters[1], world.clients[1] 276 request := backpackUseRequest(1, 67, 1)[20:] 277 switch scenario { 278 case "treatment 5": 279 player.stats.NanoAiding.Treatment = 3 280 case "no target": 281 client.selectedTarget = [2]uint32{} 282 case "other target": 283 client.selectedTarget = [2]uint32{50000, 2} 284 case "foreign owner": 285 binary.BigEndian.PutUint32(request[8:12], 2) 286 case "foreign user": 287 binary.BigEndian.PutUint32(request[36:40], 2) 288 case "wrong type": 289 player.inventory.Items[3].Type = 51022 290 case "wrong quality": 291 player.inventory.Items[3].Quality = 200 292 case "dead": 293 player.health = 0 294 case "loading": 295 client.loading = true 296 case "failed save": 297 failDatabaseWrites(t, world) 298 } 299 before := player.inventory 300 301 err := world.handleOwnedItemUse(client, request) 302 303 if (err != nil) != (scenario == "failed save") || !player.implantChangesUntil.IsZero() || !player.skillLocks[124].IsZero() || !reflect.DeepEqual(before, player.inventory) { 304 t.Fatalf("invalid clinic use changed state: %v", err) 305 } 306 for _, packet := range conn.packets { 307 if !bytes.Equal(packet[16:20], []byte{0x52, 0x52, 0x68, 0x58}) || binary.BigEndian.Uint32(packet[29:33]) != 2 { 308 t.Fatalf("rejected clinic use emitted effects: %x", packet) 309 } 310 } 311 }) 312 } 313 }