server/nano_heal_cast_test.go (10201B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "testing" 6 "time" 7 ) 8 9 func TestImprovedHealingFailureRollsBackAndRetries(t *testing.T) { 10 for _, failure := range []string{"health", "ignored health", "commit"} { 11 t.Run(failure, func(t *testing.T) { 12 world, conn := newHealingCrystalWorld(t) 13 player, client := world.characters[1], world.clients[1] 14 player.stats.Body.NanoPool = 20 15 player.health, player.nano = 1, 30 16 player.inventory.LearnedNanos = []uint32{28665} 17 if err := world.saveUser(player); err != nil { 18 t.Fatal(err) 19 } 20 request := characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28665})[20:] 21 if err := world.handleNanoCast(client, request); err != nil || client.nanoCast == nil { 22 t.Fatal("heal admission failed", err) 23 } 24 at := client.nanoCast.readyAt.Add(time.Nanosecond) 25 statement := "CREATE TRIGGER fail_heal BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(FAIL, 'test heal'); END" 26 if failure == "ignored health" { 27 statement = "CREATE TRIGGER fail_heal BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(IGNORE); END" 28 } else if failure == "commit" { 29 statement = "CREATE TABLE invalid_heal (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_heal AFTER UPDATE OF health ON characters BEGIN INSERT INTO invalid_heal VALUES (999999); END" 30 } 31 if _, err := world.db.Exec(statement); err != nil { 32 t.Fatal(err) 33 } 34 conn.packets = nil 35 36 world.advanceNanos(at) 37 cold := newTestInventoryServer(t).characters[1] 38 39 if player.health != 1 || player.nano != 30 || player.nanoReserved != 0 || client.nanoCast != nil || 40 !player.nanoRechargeUntil.IsZero() || cold.health != 1 || cold.nano != 30 { 41 t.Fatalf("failed heal published partial health, debit or recharge: health=%d nano=%d reserved=%d cast=%v recharge=%v cold=%d/%d", player.health, player.nano, player.nanoReserved, client.nanoCast, player.nanoRechargeUntil, cold.health, cold.nano) 42 } 43 for _, packet := range conn.packets { 44 if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c || 45 binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 107 { 46 t.Fatal("failed heal published success") 47 } 48 } 49 if _, err := world.db.Exec("DROP TRIGGER fail_heal"); err != nil { 50 t.Fatal(err) 51 } 52 if err := world.handleNanoCast(client, request); err != nil || client.nanoCast == nil { 53 t.Fatal("failed heal could not retry", err) 54 } 55 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 56 if player.health < 17 || player.health > 31 || player.nano != 17 { 57 t.Fatal("heal retry did not apply one result") 58 } 59 }) 60 } 61 } 62 63 func TestImprovedHealingCastRestoresHealthAndCommitsCostAndRecharge(t *testing.T) { 64 world, conn := newHealingCrystalWorld(t) 65 player, client := world.characters[1], world.clients[1] 66 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 67 t.Fatal(err) 68 } 69 player.stats.Body.NanoPool = 20 70 player.health, player.nano = 1, 30 71 if err := world.savePosition(player); err != nil { 72 t.Fatal(err) 73 } 74 conn.packets = nil 75 76 request := characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28665})[20:] 77 before := time.Now() 78 if err := world.handleNanoCast(client, request); err != nil { 79 t.Fatal(err) 80 } 81 after := time.Now() 82 if client.nanoCast == nil { 83 t.Fatal("learned Improved Healing was rejected") 84 } 85 if player.health != 1 || player.nano != 17 || player.nanoReserved != 13 { 86 t.Fatal("cast admission changed health or reserved the wrong cost") 87 } 88 at := client.nanoCast.readyAt 89 if at.Before(before.Add(2464999914*time.Nanosecond)) || at.After(after.Add(2464999914*time.Nanosecond)) { 90 t.Fatal("Improved Healing did not use its authored attack time and native initiative reduction", at.Sub(before)) 91 } 92 world.advanceNanos(at) 93 if player.health != 1 || client.nanoCast == nil { 94 t.Fatal("heal completed before the native effect gate") 95 } 96 world.advanceNanos(at.Add(time.Nanosecond)) 97 cold := newTestInventoryServer(t).characters[1] 98 99 if player.health < 17 || player.health > 31 || player.nano != 17 || player.nanoReserved != 0 || client.nanoCast != nil || 100 cold.health != player.health || cold.nano != 17 || !cold.nanoRechargeUntil.Equal(player.nanoRechargeUntil) || !player.nanoRechargeUntil.After(at) || len(player.activeNanos) != 0 { 101 t.Fatalf("heal did not commit one immediate result: health=%d nano=%d recharge=%v", player.health, player.nano, player.nanoRechargeUntil) 102 } 103 completed, snapshots := 0, 0 104 for _, packet := range conn.packets { 105 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 107 { 106 completed++ 107 } 108 if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c && binary.BigEndian.Uint32(packet[29:33]) == player.health { 109 snapshots++ 110 } 111 } 112 if completed != 1 || snapshots != 1 { 113 t.Fatalf("heal completion/absolute health publication: %d/%d", completed, snapshots) 114 } 115 } 116 117 func TestImprovedHealingCancellationHasNoDelayedResult(t *testing.T) { 118 world, conn := newHealingCrystalWorld(t) 119 player, client := world.characters[1], world.clients[1] 120 player.stats.Body.NanoPool = 20 121 player.health, player.nano = 1, 30 122 player.inventory.LearnedNanos = []uint32{28665} 123 if err := world.saveUser(player); err != nil { 124 t.Fatal(err) 125 } 126 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28665})[20:]); err != nil || client.nanoCast == nil { 127 t.Fatal("heal admission failed", err) 128 } 129 deadline := client.nanoCast.readyAt 130 conn.packets = nil 131 132 for range 2 { 133 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{108})[20:]); err != nil { 134 t.Fatal(err) 135 } 136 } 137 world.advanceNanos(deadline.Add(time.Minute)) 138 cold := newTestInventoryServer(t).characters[1] 139 140 if player.health != 1 || player.nano != 30 || player.nanoReserved != 0 || client.nanoCast != nil || 141 !player.nanoRechargeUntil.IsZero() || cold.health != 1 || cold.nano != 30 || cold.nanoReserved != 0 { 142 t.Fatal("cancelled heal retained cost, recharge or a delayed result") 143 } 144 if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 { 145 t.Fatal("cancellation published a duplicate refund or result") 146 } 147 } 148 149 func TestImprovedHealingRejectsInvalidAdmission(t *testing.T) { 150 for _, scenario := range []string{"unlearned", "profession", "BM", "MM", "nano", "dead", "NPC", "missing target", "moving"} { 151 t.Run(scenario, func(t *testing.T) { 152 world, _ := newHealingCrystalWorld(t) 153 player, client := world.characters[1], world.clients[1] 154 player.health, player.nano = 1, 30 155 player.inventory.LearnedNanos = []uint32{28665} 156 target := uint32(1) 157 switch scenario { 158 case "unlearned": 159 player.inventory.LearnedNanos = nil 160 case "profession": 161 player.profession = 1 162 case "BM": 163 player.stats.NanoAiding.BiologicalMetamorphosis = 6 164 case "MM": 165 player.stats.NanoAiding.MatterMetamorphosis = 4 166 case "nano": 167 player.nano = 12 168 case "dead": 169 player.health = 0 170 case "NPC": 171 target = 1000000 172 case "missing target": 173 target = 999 174 case "moving": 175 player.movement.forward = 1 176 } 177 health, nano := player.health, player.nano 178 179 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, target, 53019, 28665})[20:]); err != nil { 180 t.Fatal(err) 181 } 182 world.advanceNanos(time.Now().Add(time.Minute)) 183 184 if client.nanoCast != nil || player.health != health || player.nano != nano || player.nanoReserved != 0 || !player.nanoRechargeUntil.IsZero() { 185 t.Fatal("invalid heal changed health, nano or recharge") 186 } 187 }) 188 } 189 } 190 191 func TestImprovedHealingRecipientClampAndRecharge(t *testing.T) { 192 world, conn := newHealingCrystalWorld(t) 193 player, client := world.characters[1], world.clients[1] 194 player.stats.Body.NanoPool = 20 195 player.nano, player.inventory.LearnedNanos = 30, []uint32{28665} 196 if err := world.saveUser(player); err != nil { 197 t.Fatal(err) 198 } 199 patient := *player 200 patient.id, patient.name, patient.inventory = 2, "Patient", playerInventory{} 201 // Existing ability buffs fill all eight NCU; direct healing needs no free NCU. 202 patient.activeNanos = []activeNano{ 203 {program: 26342, caster: 1, expiresAt: time.Now().Add(time.Minute)}, 204 {program: 27086, caster: 1, expiresAt: time.Now().Add(time.Minute)}, 205 {program: 26360, caster: 1, expiresAt: time.Now().Add(time.Minute)}, 206 } 207 patient.health = patient.maximumHealth() - 1 208 if patient.acceptsNano(28665) { 209 t.Fatal("fixture must reject treating the heal as an NCU buff") 210 } 211 recipient := &combatTestConn{} 212 world.characters[2], world.nextCharacter = &patient, 2 213 world.clients[2] = &worldClient{id: 2, conn: recipient, ready: true, target: true} 214 if err := world.saveUser(&patient); err != nil { 215 t.Fatal(err) 216 } 217 request := characterActionPacket(1, [5]uint32{19, 50000, 2, 53019, 28665})[20:] 218 219 if err := world.handleNanoCast(client, request); err != nil || client.nanoCast == nil { 220 t.Fatal("friendly heal was rejected for NCU", err) 221 } 222 completed := client.nanoCast.readyAt.Add(time.Nanosecond) 223 world.advanceNanos(completed) 224 world.handleNanoCast(client, request) 225 world.advanceNanos(completed.Add(time.Nanosecond)) 226 cold := newTestInventoryServer(t).characters[2] 227 228 if patient.health != patient.maximumHealth() || cold.health != patient.health || player.nano != 17 || client.nanoCast != nil || 229 player.nanoRechargeUntil.Sub(completed) != 2499999944*time.Nanosecond || len(patient.activeNanos) != 3 || len(cold.activeNanos) != 3 || patient.fighting != 0 { 230 t.Fatal("heal failed to clamp, consumed NCU, started combat or bypassed recharge") 231 } 232 for _, check := range []struct { 233 packets [][]byte 234 action uint32 235 }{{conn.packets, 130}, {recipient.packets, 129}} { 236 found := 0 237 for _, packet := range check.packets { 238 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == check.action { 239 found++ 240 if binary.BigEndian.Uint32(packet[45:49]) != 1 { 241 t.Fatal("heal feedback reported the rolled amount instead of restored HP") 242 } 243 } 244 } 245 if found != 1 { 246 t.Fatal("missing or duplicate private healing feedback", check.action, found) 247 } 248 } 249 }