server/critical_nano_test.go (13293B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "slices" 7 "testing" 8 "time" 9 ) 10 11 func TestCriticalNanosChangeOrdinaryFastAttackAndFlingThresholds(t *testing.T) { 12 for _, fixture := range selfAndCriticalNanoFixtures(t) { 13 var bonus uint32 14 for _, modifier := range fixture.Modifiers { 15 if modifier[0] == 379 { 16 bonus = uint32(modifier[1]) 17 } 18 } 19 if bonus == 0 { 20 continue 21 } 22 for _, attack := range []string{"ordinary", "fast", "fling"} { 23 for _, roll := range []uint32{bonus + 2, bonus + 3} { 24 t.Run(fmt.Sprintf("%d/%s/%d", fixture.Program, attack, roll), func(t *testing.T) { 25 world, conn := newSelfNanoWorld(t) 26 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 27 player.level, player.profession = 200, uint16(fixture.Profession) 28 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 29 player.inventory.LearnedNanos = []uint32{fixture.Program} 30 if err := world.saveUser(player); err != nil { 31 t.Fatal(err) 32 } 33 target.health, target.position = 1000, [3]float32{1, 0, 0} 34 if attack != "ordinary" { 35 template := uint32(209283) 36 if attack == "fling" { 37 template = 123742 38 } 39 if granted, err := world.grantItem(player, template); err != nil || !granted { 40 t.Fatal(granted, err) 41 } 42 if attack == "fling" { 43 inventory := player.inventory 44 inventory.Items = slices.Clone(inventory.Items) 45 inventory.Items[2].Ammo = 3 46 if err := world.saveInventory(player, inventory); err != nil { 47 t.Fatal(err) 48 } 49 } 50 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil { 51 t.Fatal(err) 52 } 53 if client.equipment == nil { 54 t.Fatal("original weapon was rejected") 55 } 56 world.advanceEquipment(client.equipment.readyAt) 57 } 58 castTestBuff(t, world, 1, 1, fixture.Program) 59 world.combatRoll = func(limit uint32) uint32 { 60 if limit == 10000 { 61 return 0 62 } 63 if limit == 100 { 64 return roll 65 } 66 return limit - 1 67 } 68 conn.packets = nil 69 70 var packet []byte 71 switch attack { 72 case "ordinary": 73 if err := world.handleCombat(client, attackPacket(1, target.id)[20:]); err != nil { 74 t.Fatal(err) 75 } 76 world.advanceCombat(player.nextAttack) 77 packet = conn.packets[1] 78 case "fast": 79 if err := world.handleWeaponSpecial(client, fastAttackRequest(1, target.id)[20:]); err != nil { 80 t.Fatal(err) 81 } 82 packet = conn.packets[0] 83 case "fling": 84 if err := world.handleWeaponSpecial(client, flingShotRequest(1, target.id)[20:]); err != nil { 85 t.Fatal(err) 86 } 87 packet = conn.packets[0] 88 } 89 90 critical := roll == bonus+2 91 want, result := uint32(16), uint32(4) 92 if critical { 93 want, result = 26, 5 94 } 95 if attack == "fast" { 96 want = 65 97 if critical { 98 want = 81 99 } 100 } else if attack == "fling" { 101 want = 39 102 if critical { 103 want = 78 104 } 105 } 106 if target.health != 1000-want || player.nanoBonus(379) != int32(bonus) { 107 t.Fatal("authored critical modifier did not shift the shared weapon threshold", target.health, player.attackRating()) 108 } 109 if attack == "ordinary" { 110 if binary.BigEndian.Uint32(packet[29:33]) != want || binary.BigEndian.Uint32(packet[53:57]) != result { 111 t.Fatal("ordinary nano-boosted critical result mismatch", packet) 112 } 113 } else if binary.BigEndian.Uint32(packet[29:33]) != 6 || binary.BigEndian.Uint32(packet[33:37]) != want { 114 t.Fatal("named special changed its critical result layout", packet) 115 } 116 if attack == "fling" && player.inventory.Items[2].Ammo != 3 { 117 t.Fatal("critical buff made Fling deduct pre-15.5.5 ammunition") 118 } 119 }) 120 } 121 } 122 } 123 } 124 125 func TestCriticalNanoExpiryAndDeathRemoveEveryContribution(t *testing.T) { 126 for _, fixture := range selfAndCriticalNanoFixtures(t) { 127 critical := false 128 for _, modifier := range fixture.Modifiers { 129 critical = critical || modifier[0] == 379 130 } 131 if !critical { 132 continue 133 } 134 for _, operation := range []string{"expiry", "death"} { 135 t.Run(fmt.Sprintf("%d/%s", fixture.Program, operation), func(t *testing.T) { 136 world, _ := newSelfNanoWorld(t) 137 player := world.characters[1] 138 player.level, player.profession = 200, uint16(fixture.Profession) 139 player.inventory.LearnedNanos = []uint32{fixture.Program} 140 castTestBuff(t, world, 1, 1, fixture.Program) 141 expiry := player.activeNanos[0].expiresAt 142 143 if operation == "expiry" { 144 world.advanceNanos(expiry.Add(-time.Nanosecond)) 145 if len(player.activeNanos) != 1 || player.nanoBonus(379) == 0 { 146 t.Fatal("critical buff expired before its exact deadline") 147 } 148 world.advanceNanos(expiry) 149 } else { 150 defeatReclaimPlayer(t, world) 151 } 152 world.advanceNanos(expiry.Add(time.Hour)) 153 cold := newTestInventoryServer(t).characters[1] 154 155 if player.nanoBonus(379) != 0 || cold.nanoBonus(379) != 0 || len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || 156 player.usedNCU() != 0 || cold.usedNCU() != 0 || player.nanoBonus(151) != 0 || cold.nanoBonus(151) != 0 { 157 t.Fatal("critical nano lifecycle retained a critical or accompanying skill contribution") 158 } 159 if operation == "death" && (player.health != 0 || cold.health != 0) { 160 t.Fatal("critical nano lifecycle revived a dead player") 161 } 162 }) 163 } 164 } 165 } 166 167 func TestTargetedCriticalNanosPreserveRecipientAdmission(t *testing.T) { 168 for _, fixture := range selfAndCriticalNanoFixtures(t) { 169 if fixture.SelfOnly { 170 continue 171 } 172 for _, scenario := range []string{"valid", "zero", "unknown", "NPC", "dead", "other zone", "other side", "far", "loading", "full NCU"} { 173 t.Run(fmt.Sprintf("%d/%s", fixture.Program, scenario), func(t *testing.T) { 174 world, _ := newSelfNanoWorld(t) 175 player, client := world.characters[1], world.clients[1] 176 player.profession = uint16(fixture.Profession) 177 player.inventory.LearnedNanos = []uint32{fixture.Program} 178 recipient := *player 179 recipient.id, recipient.name, recipient.profession = 2, "Recipient", 1 180 recipient.inventory = playerInventory{} 181 world.characters[2], world.nextCharacter = &recipient, 2 182 world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true} 183 if err := world.saveUser(&recipient); err != nil { 184 t.Fatal(err) 185 } 186 for _, template := range []uint32{173155, 168658} { 187 if granted, err := world.grantItem(&recipient, template); err != nil || !granted { 188 t.Fatal(granted, err) 189 } 190 } 191 inventory := recipient.inventory 192 inventory.Items = slices.Clone(inventory.Items) 193 inventory.Items[0].Slot, inventory.Items[1].Slot = 35, 22 194 if err := world.saveInventory(&recipient, inventory); err != nil { 195 t.Fatal(err) 196 } 197 target := uint32(2) 198 switch scenario { 199 case "zero": 200 target = 0 201 case "unknown": 202 target = 999 203 case "NPC": 204 target = 1000000 205 case "dead": 206 recipient.health = 0 207 case "other zone": 208 recipient.playfield++ 209 case "other side": 210 recipient.appearance ^= 1 << 9 211 case "far": 212 recipient.position = [3]float32{100, 0, 0} 213 case "loading": 214 world.clients[2].loading = true 215 case "full NCU": 216 recipient.inventory.Items = nil 217 } 218 219 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, target, 53019, fixture.Program})[20:]); err != nil { 220 t.Fatal(err) 221 } 222 if client.nanoCast != nil { 223 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 224 } 225 226 if len(player.activeNanos) != 0 || player.nanoBonus(379) != 0 || player.nanoBonus(151) != 0 { 227 t.Fatal("targeted critical buff fell back to the caster") 228 } 229 if scenario == "valid" { 230 cold := newTestInventoryServer(t).characters[2] 231 if len(recipient.activeNanos) != 1 || recipient.activeNanos[0].caster != 1 || recipient.usedNCU() != fixture.NCU || 232 player.nano != 50000-fixture.NanoCost || recipient.nano != 50000 || len(cold.activeNanos) != 1 { 233 t.Fatal("critical buff was charged or applied to the wrong actor") 234 } 235 for _, modifier := range fixture.Modifiers { 236 if recipient.nanoBonus(uint32(modifier[0])) != modifier[1] || cold.nanoBonus(uint32(modifier[0])) != modifier[1] { 237 t.Fatal("targeted buff lost a companion modifier", modifier) 238 } 239 } 240 } else if client.nanoCast != nil || player.nano != 50000 || player.nanoReserved != 0 || len(recipient.activeNanos) != 0 { 241 t.Fatal("invalid recipient consumed nano or received a critical effect") 242 } 243 }) 244 } 245 } 246 } 247 248 func TestCriticalNanoDatabaseFailuresPreserveTheCompleteContract(t *testing.T) { 249 for _, program := range []uint32{32065, 95526} { 250 for _, operation := range []string{"insert", "commit", "cancel", "expire"} { 251 t.Run(fmt.Sprintf("%d/%s", program, operation), func(t *testing.T) { 252 world, conn := newSelfNanoWorld(t) 253 player, client := world.characters[1], world.clients[1] 254 player.profession = 2 255 if program == 32065 { 256 player.profession = 5 257 } 258 player.inventory.LearnedNanos = []uint32{program} 259 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, program})[20:]); err != nil { 260 t.Fatal(err) 261 } 262 at := client.nanoCast.readyAt.Add(time.Nanosecond) 263 statement := "CREATE TRIGGER fail_buff BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'test add'); END" 264 if operation == "commit" { 265 statement = `CREATE TABLE pending_buff (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); 266 CREATE TRIGGER fail_buff AFTER INSERT ON active_nanos BEGIN INSERT INTO pending_buff VALUES (999999); END` 267 } else if operation == "cancel" || operation == "expire" { 268 world.advanceNanos(at) 269 at = player.activeNanos[0].expiresAt 270 statement = "CREATE TRIGGER fail_buff BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'test remove'); END" 271 } 272 if _, err := world.db.Exec(statement); err != nil { 273 t.Fatal(err) 274 } 275 beforeNano, beforeCrit, beforeAimed, beforeNCU := player.nano, player.nanoBonus(379), player.nanoBonus(151), player.usedNCU() 276 conn.packets = nil 277 278 var err error 279 if operation == "cancel" { 280 err = world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, program})[20:]) 281 } else { 282 world.advanceNanos(at) 283 } 284 cold := newTestInventoryServer(t).characters[1] 285 286 if operation == "insert" || operation == "commit" { 287 if player.nano != 50000 || cold.nano != 50000 || player.nanoReserved != 0 || client.nanoCast != nil || 288 len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.nanoBonus(379) != 0 || player.nanoBonus(151) != 0 || 289 len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 { 290 t.Fatal("failed critical effect published a modifier or lost its refund") 291 } 292 } else { 293 if operation == "cancel" && err == nil || len(conn.packets) != 0 || player.nano != beforeNano || cold.nano != beforeNano || 294 player.nanoBonus(379) != beforeCrit || cold.nanoBonus(379) != beforeCrit || player.nanoBonus(151) != beforeAimed || 295 cold.nanoBonus(151) != beforeAimed || player.usedNCU() != beforeNCU || cold.usedNCU() != beforeNCU { 296 t.Fatal("failed removal lost part of a critical contract or published success", err) 297 } 298 if _, err := world.db.Exec("DROP TRIGGER fail_buff"); err != nil { 299 t.Fatal(err) 300 } 301 world.advanceNanos(at.Add(time.Second)) 302 if player.nanoBonus(379) != 0 || player.nanoBonus(151) != 0 || len(player.activeNanos) != 0 || len(conn.packets) != 1 { 303 t.Fatal("critical effect removal did not retry exactly once") 304 } 305 } 306 }) 307 } 308 } 309 } 310 311 func TestCriticalNanoAddsToWornChance(t *testing.T) { 312 for _, roll := range []uint32{7, 8} { 313 t.Run(fmt.Sprint(roll), func(t *testing.T) { 314 world, conn := newSelfNanoWorld(t) 315 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 316 player.profession = 5 317 player.inventory.LearnedNanos = []uint32{32065} 318 if granted, err := world.grantItem(player, 160568); err != nil || !granted { 319 t.Fatal(granted, err) 320 } 321 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 18)[20:]); err != nil { 322 t.Fatal(err) 323 } 324 if client.equipment == nil { 325 t.Fatal("original Agent helmet was rejected") 326 } 327 world.advanceEquipment(client.equipment.readyAt) 328 castTestBuff(t, world, 1, 1, 32065) 329 target.position, target.health = [3]float32{1, 0, 0}, 1000 330 world.combatRoll = func(limit uint32) uint32 { 331 if limit == 10000 { 332 return 0 333 } 334 if limit == 100 { 335 return roll 336 } 337 return limit - 1 338 } 339 conn.packets = nil 340 341 if err := world.handleCombat(client, attackPacket(1, target.id)[20:]); err != nil { 342 t.Fatal(err) 343 } 344 world.advanceCombat(player.nextAttack) 345 346 want := uint32(5) 347 if roll == 8 { 348 want = 4 349 } 350 if player.equipmentBonus(379) != 1 || player.nanoBonus(379) != 4 || binary.BigEndian.Uint32(conn.packets[1][53:57]) != want { 351 t.Fatal("critical nano replaced or double-counted the worn contribution") 352 } 353 }) 354 } 355 }