server/incoming_equipment_test.go (4549B)
1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "slices" 7 "testing" 8 "time" 9 ) 10 11 func TestIncomingCombatPermitsEquipmentAdmissionAndCompletion(t *testing.T) { 12 for _, move := range []string{"equip", "replace", "remove"} { 13 for _, alreadyAttacked := range []bool{false, true} { 14 t.Run(fmt.Sprintf("%s/attackedBeforeRequest=%t", move, alreadyAttacked), func(t *testing.T) { 15 world, _ := newEquipmentReplacementWorld(t) 16 player, npc, client := world.characters[1], world.characters[1000000], world.clients[1] 17 source, destination := uint32(64), uint32(6) 18 expected := []ownedItem{ 19 {ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1}, 20 {ID: 2, Type: 51018, Template: 122691, Quality: 3, Slot: 6, Count: 1}, 21 } 22 if move == "equip" { 23 player.inventory.Items[0].Slot, expected[0].Slot = 65, 65 24 } else if move == "remove" { 25 source, destination, expected[0].Slot, expected[1].Slot = 6, 111, 65, 64 26 } 27 if err := world.saveInventory(player, player.inventory); err != nil { 28 t.Fatal(err) 29 } 30 before := slices.Clone(player.inventory.Items) 31 npc.nextAttack, npc.rechargeUntil = time.Now().Add(10*time.Second), time.Now().Add(8*time.Second) 32 attack, recharge := npc.nextAttack, npc.rechargeUntil 33 if alreadyAttacked { 34 npc.fighting, world.fighting[npc.id] = player.id, npc 35 } 36 37 if err := world.handleInventory(client, itemMovePacket(1, source, destination)[20:]); err != nil || client.equipment == nil { 38 t.Fatal("another actor's attack refused valid equipment", err) 39 } 40 deadline := client.equipment.readyAt 41 npc.fighting, world.fighting[npc.id] = player.id, npc 42 world.advanceEquipment(deadline.Add(-time.Nanosecond)) 43 if !slices.Equal(player.inventory.Items, before) { 44 t.Fatal("equipment committed before its original delay") 45 } 46 world.advanceEquipment(deadline) 47 cold := newTestInventoryServer(t).characters[1] 48 49 if client.equipment != nil || !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.inventory.Items, expected) || 50 player.fighting != 0 || npc.fighting != 1 || npc.nextAttack != attack || npc.rechargeUntil != recharge { 51 t.Fatal("incoming combat interrupted equipment, changed attack timing or lost an instance", player.inventory.Items) 52 } 53 }) 54 } 55 } 56 } 57 58 func TestIncomingHitDuringEquipmentPreservesTheOpponentAttackClock(t *testing.T) { 59 world, _ := newEquipmentReplacementWorld(t) 60 player, npc, client := world.characters[1], world.characters[1000000], world.clients[1] 61 world.startAttack(npc, player, time.Now(), make(map[*worldClient][][]byte)) 62 first := npc.nextAttack 63 64 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil { 65 t.Fatal("incoming combat refused replacement", err) 66 } 67 deadline := client.equipment.readyAt 68 world.advanceCombat(first) 69 attack, recharge := npc.nextAttack, npc.rechargeUntil 70 if player.health >= 100 || client.equipment == nil || player.inventory.Items[0].Slot != 6 { 71 t.Fatal("incoming hit was suppressed or prematurely committed equipment") 72 } 73 world.advanceEquipment(deadline) 74 75 if player.inventory.Items[0].Slot != 64 || player.inventory.Items[1].Slot != 6 || player.fighting != 0 || 76 npc.fighting != player.id || npc.nextAttack != attack || npc.rechargeUntil != recharge || !recharge.After(first) { 77 t.Fatal("replacement stopped, restarted or accelerated the opponent") 78 } 79 } 80 81 func TestIncomingCombatDoesNotBypassOwnAttackOrRechargeEquipmentGates(t *testing.T) { 82 for _, phase := range []string{"attack", "recharge"} { 83 t.Run(phase, func(t *testing.T) { 84 world, output := newEquipmentReplacementWorld(t) 85 player, npc, client := world.characters[1], world.characters[1000000], world.clients[1] 86 npc.fighting, world.fighting[npc.id] = player.id, npc 87 if phase == "attack" { 88 player.fighting, world.fighting[player.id] = npc.id, player 89 } else { 90 player.rechargeUntil = time.Now().Add(time.Second) 91 } 92 before, err := readTestItemStore(world) 93 if err != nil { 94 t.Fatal(err) 95 } 96 output.packets = nil 97 98 err = world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]) 99 after, readErr := readTestItemStore(world) 100 101 if err != nil || readErr != nil || !bytes.Equal(before, after) || client.equipment != nil || len(output.packets) != 2 || 102 player.inventory.Items[0].Slot != 6 || player.inventory.Items[1].Slot != 64 || npc.fighting != player.id { 103 t.Fatal("incoming combat bypassed an independent own-attack/recharge gate", err, readErr) 104 } 105 }) 106 } 107 }