server/nano_buffs_test.go (15982B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "slices" 6 "testing" 7 "time" 8 ) 9 10 func newNanoBuffWorld(t *testing.T) (*server, *combatTestConn) { 11 t.Helper() 12 world, conn := newNanoCastingWorld(t) 13 world.nextCharacter = 1 14 player := world.characters[1] 15 player.stats.Abilities.Strength = 10 16 player.stats.Body.NanoPool = 200 17 player.stats.NanoAiding.MatterMetamorphosis = 39 18 player.stats.NanoAiding.BiologicalMetamorphosis = 33 19 player.nano = 100 20 player.inventory.LearnedNanos = []uint32{27169, 26360} 21 if err := world.saveUser(player); err != nil { 22 t.Fatal(err) 23 } 24 if err := world.saveInventory(player, player.inventory); err != nil { 25 t.Fatal(err) 26 } 27 return world, conn 28 } 29 30 func castTestBuff(t *testing.T, world *server, caster, target, program uint32) time.Time { 31 t.Helper() 32 client := world.clients[caster] 33 if err := world.handleNanoCast(client, characterActionPacket(caster, [5]uint32{19, 50000, target, 53019, program})[20:]); err != nil { 34 t.Fatal(err) 35 } 36 if client.nanoCast == nil { 37 t.Fatal("cast was not admitted") 38 } 39 at := client.nanoCast.readyAt.Add(time.Nanosecond) 40 world.advanceNanos(at) 41 return at 42 } 43 44 func TestNanoBuffSelfCastCommitsOneContributionAndExpires(t *testing.T) { 45 world, conn := newNanoBuffWorld(t) 46 player, client := world.characters[1], world.clients[1] 47 request := characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:] 48 49 if err := world.handleNanoCast(client, request); err != nil { 50 t.Fatal(err) 51 } 52 deadline := client.nanoCast.readyAt 53 world.advanceNanos(deadline) 54 if len(player.activeNanos) != 0 || player.nano != 56 || player.nanoReserved != 44 { 55 t.Fatal("effect applied before completion or incorrect reservation") 56 } 57 world.advanceNanos(deadline.Add(time.Nanosecond)) 58 world.advanceNanos(deadline.Add(time.Second)) 59 restored := newTestInventoryServer(t).characters[1] 60 61 if player.effectiveAbility(16) != 22 || player.usedNCU() != 3 || len(player.activeNanos) != 1 || player.nanoReserved != 0 || 62 restored.effectiveAbility(16) != 22 || restored.usedNCU() != 3 || restored.nano != 56 || restored.activeNanos[0].remaining != 20*time.Minute { 63 t.Fatalf("cast did not commit exactly one effect: %+v, restored %+v", player.activeNanos, restored.activeNanos) 64 } 65 if len(conn.packets) != 4 || binary.BigEndian.Uint32(conn.packets[2][29:33]) != 107 || 66 !slices.Equal(conn.packets[3], targetPacket(characterActionPacket(1, [5]uint32{177, 53019, 27169, 1, 120000}))) { 67 t.Fatalf("unexpected native buff delivery: %x", conn.packets) 68 } 69 world.advanceNanos(player.activeNanos[0].expiresAt) 70 world.advanceNanos(deadline.Add(time.Hour)) 71 if player.effectiveAbility(16) != 10 || player.usedNCU() != 0 || len(player.activeNanos) != 0 || len(conn.packets) != 5 || 72 !slices.Equal(conn.packets[4], targetPacket(characterActionPacket(1, [5]uint32{65, 0, 0, 1, 27169}))) || 73 len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 74 t.Fatal("expiration did not remove exactly one durable contribution") 75 } 76 } 77 78 func TestNanoBuffFriendlyTargetAndObserverReceiveRecipientEffect(t *testing.T) { 79 world, casterConn := newNanoBuffWorld(t) 80 player := world.characters[1] 81 recipient := *player 82 recipient.id, recipient.name, recipient.position = 2, "Recipient", [3]float32{0, 0, 12} 83 world.characters[2], world.nextCharacter = &recipient, 2 84 recipientConn := &combatTestConn{} 85 world.clients[1].players = map[uint32]bool{2: true} 86 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: recipientConn, ready: true, target: true} 87 if err := world.saveUser(&recipient); err != nil { 88 t.Fatal(err) 89 } 90 91 castTestBuff(t, world, 1, 2, 27169) 92 93 if player.effectiveAbility(16) != 10 || player.usedNCU() != 0 || player.nano != 56 || 94 recipient.effectiveAbility(16) != 22 || recipient.usedNCU() != 3 || recipient.nano != 100 || 95 newTestInventoryServer(t).characters[2].activeNanos[0].caster != 1 { 96 t.Fatal("buff was charged or applied to the wrong actor") 97 } 98 for _, conn := range []*combatTestConn{casterConn, recipientConn} { 99 if !slices.Equal(conn.packets[len(conn.packets)-1][16:], targetPacket(characterActionPacket(2, [5]uint32{177, 53019, 27169, 1, 120000}))[16:]) { 100 t.Fatalf("wrong recipient/source: %x", conn.packets) 101 } 102 } 103 } 104 105 func TestNanoBuffRejectsInvalidAdmission(t *testing.T) { 106 for _, scenario := range []string{"MM", "BM", "nano", "unlearned", "no target", "empty identity", "item identity", "NPC", "cross side", "range", "loading recipient"} { 107 t.Run(scenario, func(t *testing.T) { 108 world, _ := newNanoBuffWorld(t) 109 player, client := world.characters[1], world.clients[1] 110 recipient := *player 111 recipient.id = 2 112 world.characters[2] = &recipient 113 world.clients[1].players = map[uint32]bool{2: true} 114 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: &combatTestConn{}, ready: true, target: true} 115 target := uint32(2) 116 targetType := uint32(50000) 117 switch scenario { 118 case "MM": 119 player.stats.NanoAiding.MatterMetamorphosis = 38 120 case "BM": 121 player.stats.NanoAiding.BiologicalMetamorphosis = 32 122 case "nano": 123 player.nano = 43 124 case "unlearned": 125 player.inventory.LearnedNanos = nil 126 case "no target": 127 target = 0 128 case "empty identity": 129 targetType, target = 0, 0 130 case "item identity": 131 targetType = 51018 132 case "NPC": 133 target = 1000000 134 case "cross side": 135 recipient.appearance ^= 1 << 9 136 case "range": 137 recipient.position = [3]float32{0, 12.001, 0} 138 case "loading recipient": 139 world.clients[2].loading = true 140 } 141 before := player.nano 142 143 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, targetType, target, 53019, 27169})[20:]) 144 145 if err != nil || client.nanoCast != nil || player.nano != before || len(player.activeNanos) != 0 || len(recipient.activeNanos) != 0 { 146 t.Fatal("invalid buff spent nano or applied an effect", err) 147 } 148 }) 149 } 150 } 151 152 func TestNanoBuffSameStrainUpgradeRefreshAndDowngrade(t *testing.T) { 153 world, conn := newNanoBuffWorld(t) 154 player := world.characters[1] 155 castTestBuff(t, world, 1, 1, 26360) 156 if player.effectiveAbility(16) != 16 || player.usedNCU() != 2 || player.nano != 76 { 157 t.Fatal("Augment Strength values") 158 } 159 player.nanoRechargeUntil = time.Time{} 160 161 castTestBuff(t, world, 1, 1, 27169) 162 player.nano, player.nanoRechargeUntil = 100, time.Time{} 163 castTestBuff(t, world, 1, 1, 27169) 164 before := len(conn.packets) 165 player.nanoRechargeUntil = time.Time{} 166 err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 26360})[20:]) 167 168 if err != nil || len(player.activeNanos) != 1 || player.activeNanos[0].program != 27169 || 169 player.effectiveAbility(16) != 22 || player.usedNCU() != 3 || player.nano != 56 || world.clients[1].nanoCast != nil || len(conn.packets) != before+1 { 170 t.Fatal("same-strain replacement retained an old bonus, stacked or charged a rejected downgrade", err) 171 } 172 if restored := newTestInventoryServer(t).characters[1]; len(restored.activeNanos) != 1 || restored.activeNanos[0].program != 27169 || restored.effectiveAbility(16) != 22 { 173 t.Fatal("replacement did not persist atomically") 174 } 175 } 176 177 func TestNanoBuffRemovalRejectsForeignAndMalformedRequests(t *testing.T) { 178 for _, scenario := range []string{"valid", "foreign", "wrong identity", "wrong program", "extra arguments", "loading", "stale"} { 179 t.Run(scenario, func(t *testing.T) { 180 world, conn := newNanoBuffWorld(t) 181 player, client := world.characters[1], world.clients[1] 182 castTestBuff(t, world, 1, 1, 27169) 183 owner, fields := uint32(1), [5]uint32{65, 0, 0, 53019, 27169} 184 switch scenario { 185 case "foreign": 186 owner = 2 187 case "wrong identity": 188 fields[3] = 1040005 189 case "wrong program": 190 fields[4] = 26360 191 case "extra arguments": 192 fields[1] = 1 193 case "loading": 194 client.loading = true 195 case "stale": 196 world.clients[1] = &worldClient{id: 1} 197 } 198 before := len(conn.packets) 199 200 for range 2 { 201 if err := world.handleNanoRemoval(client, characterActionPacket(owner, fields)[20:]); err != nil { 202 t.Fatal(err) 203 } 204 } 205 206 if scenario == "valid" { 207 if len(player.activeNanos) != 0 || player.effectiveAbility(16) != 10 || len(conn.packets) != before+1 || len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 208 t.Fatal("cancel did not remove the contribution exactly once") 209 } 210 } else if len(player.activeNanos) != 1 || player.effectiveAbility(16) != 22 || len(conn.packets) != before { 211 t.Fatal("invalid cancellation changed state") 212 } 213 }) 214 } 215 } 216 217 func TestNanoBuffFailedEffectCommitRefundsAndFailedRemovalRetries(t *testing.T) { 218 for _, operation := range []string{"add", "cancel", "expire"} { 219 t.Run(operation, func(t *testing.T) { 220 world, conn := newNanoBuffWorld(t) 221 player, client := world.characters[1], world.clients[1] 222 request := characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:] 223 if err := world.handleNanoCast(client, request); err != nil { 224 t.Fatal(err) 225 } 226 at := client.nanoCast.readyAt.Add(time.Nanosecond) 227 statement := "CREATE TRIGGER fail_buff BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'test add'); END" 228 if operation != "add" { 229 world.advanceNanos(at) 230 at = player.activeNanos[0].expiresAt 231 statement = "CREATE TRIGGER fail_buff BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'test remove'); END" 232 } 233 if _, err := world.db.Exec(statement); err != nil { 234 t.Fatal(err) 235 } 236 before := len(conn.packets) 237 238 var err error 239 if operation == "cancel" { 240 err = world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 27169})[20:]) 241 } else { 242 world.advanceNanos(at) 243 } 244 245 if operation == "add" { 246 if player.nano != 100 || player.nanoReserved != 0 || len(player.activeNanos) != 0 || client.nanoCast != nil || len(conn.packets) != before+2 || 247 len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 248 t.Fatal("failed effect was published or cost was not refunded") 249 } 250 } else { 251 if operation == "cancel" && err == nil || len(player.activeNanos) != 1 || player.usedNCU() != 3 || len(conn.packets) != before || len(newTestInventoryServer(t).characters[1].activeNanos) != 1 { 252 t.Fatal("failed removal was published or changed durable state", err) 253 } 254 if _, err := world.db.Exec("DROP TRIGGER fail_buff"); err != nil { 255 t.Fatal(err) 256 } 257 world.advanceNanos(at.Add(time.Second)) 258 if len(player.activeNanos) != 0 || len(conn.packets) != before+1 { 259 t.Fatal("expiration did not retry the durable removal") 260 } 261 } 262 }) 263 } 264 } 265 266 func TestNanoBuffTimersCheckpointAndPauseWithoutRoutineActorWrites(t *testing.T) { 267 world, _ := newNanoBuffWorld(t) 268 player := world.characters[1] 269 at := castTestBuff(t, world, 1, 1, 27169) 270 auditCheckpointWrites(t, world) 271 if _, err := world.db.Exec("CREATE TRIGGER audit_buff AFTER UPDATE ON active_nanos BEGIN INSERT INTO checkpoint_writes VALUES ('active_nanos','UPDATE'); END"); err != nil { 272 t.Fatal(err) 273 } 274 player.nextPositionSave = at.Add(20 * time.Second) 275 276 for second := 1; second < 20; second++ { 277 world.advanceNanos(at.Add(time.Duration(second) * time.Second)) 278 if err := world.flushPositions(at.Add(time.Duration(second) * time.Second)); err != nil { 279 t.Fatal(err) 280 } 281 } 282 if writes := checkpointWrites(t, world); len(writes) != 0 { 283 t.Fatal("buff ticks wrote to SQLite", writes) 284 } 285 if err := world.flushPositions(at.Add(20 * time.Second)); err != nil { 286 t.Fatal(err) 287 } 288 if err := world.pauseActiveNanos(player, at.Add(time.Minute)); err != nil { 289 t.Fatal(err) 290 } 291 restored := newTestInventoryServer(t).characters[1] 292 world.advanceNanos(at.Add(24 * time.Hour)) 293 294 if restored.activeNanos[0].remaining != 19*time.Minute || !restored.activeNanos[0].expiresAt.IsZero() || len(player.activeNanos) != 1 || 295 player.activeNanos[0].remaining != 19*time.Minute || player.effectiveAbility(16) != 22 || 296 checkpointWrites(t, world)["characters/UPDATE"] != 0 || checkpointWrites(t, world)["active_nanos/UPDATE"] != 2 { 297 t.Fatal("offline timer or checkpoint cadence changed the wrong state", restored.activeNanos, checkpointWrites(t, world)) 298 } 299 } 300 301 func TestNanoBuffCompletionRejectsNewStrongerEffectAndRefunds(t *testing.T) { 302 world, conn := newNanoBuffWorld(t) 303 player, client := world.characters[1], world.clients[1] 304 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 26360})[20:]); err != nil { 305 t.Fatal(err) 306 } 307 deadline := client.nanoCast.readyAt 308 // Another caster's stronger contribution arrives while this cast is pending. 309 player.activeNanos = []activeNano{{program: 27169, caster: 2, remaining: time.Minute, expiresAt: deadline.Add(time.Minute)}} 310 if err := world.saveActiveNanos(player, deadline); err != nil { 311 t.Fatal(err) 312 } 313 conn.packets = nil 314 315 world.advanceNanos(deadline.Add(time.Nanosecond)) 316 world.advanceNanos(deadline.Add(time.Second)) 317 318 if client.nanoCast != nil || player.nano != 100 || player.nanoReserved != 0 || player.effectiveAbility(16) != 22 || 319 len(player.activeNanos) != 1 || player.activeNanos[0].caster != 2 || len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 { 320 t.Fatal("late weaker effect overwrote the stronger buff or lost its refund") 321 } 322 } 323 324 func TestNanoBuffStrengthTricklesIntoSkillsWithoutChangingBase(t *testing.T) { 325 world, _ := newNanoBuffWorld(t) 326 player := world.characters[1] 327 before := player.effectiveSkill(142, 5, [6]uint32{60, 0, 40, 0, 0, 0}) 328 329 castTestBuff(t, world, 1, 1, 27169) 330 after := player.effectiveSkill(142, 5, [6]uint32{60, 0, 40, 0, 0, 0}) 331 if err := world.handleNanoRemoval(world.clients[1], characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 27169})[20:]); err != nil { 332 t.Fatal(err) 333 } 334 335 if before != 7 || after != 9 || player.baseStats().Abilities.Strength != 10 || player.effectiveSkill(142, 5, [6]uint32{60, 0, 40, 0, 0, 0}) != 7 { 336 t.Fatal("temporary ability changed the base stat or failed to affect trickle-down", before, after) 337 } 338 } 339 340 func TestNanoBuffReadyBroadcastReplaysOnceToObservers(t *testing.T) { 341 world, conn := newNanoBuffWorld(t) 342 castTestBuff(t, world, 1, 1, 27169) 343 observer := &combatTestConn{} 344 world.characters[2] = &character{id: 2, playfield: 950, health: 1} 345 world.clients[1].players = map[uint32]bool{2: true} 346 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, target: true} 347 world.clients[1].loading = true 348 conn.packets = nil 349 350 for range 2 { 351 if err := world.handleCharacterReady(world.clients[1]); err != nil { 352 t.Fatal(err) 353 } 354 } 355 356 var adds int 357 for _, packet := range observer.packets { 358 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 { 359 adds++ 360 } 361 } 362 if adds != 1 || len(conn.packets) != 2 || world.characters[1].effectiveAbility(16) != 22 || world.characters[1].usedNCU() != 3 { 363 t.Fatal("repeated ready stacked a contribution or replayed it to its owner", adds, len(conn.packets)) 364 } 365 } 366 367 func TestNanoBuffFailedCommitPublishesNoEffectAndRefunds(t *testing.T) { 368 world, conn := newNanoBuffWorld(t) 369 client, player := world.clients[1], world.characters[1] 370 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:]); err != nil { 371 t.Fatal(err) 372 } 373 at := client.nanoCast.readyAt.Add(time.Nanosecond) 374 if _, err := world.db.Exec(`CREATE TABLE pending_buff (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); 375 CREATE TRIGGER fail_buff AFTER INSERT ON active_nanos BEGIN INSERT INTO pending_buff VALUES (999999); END`); err != nil { 376 t.Fatal(err) 377 } 378 conn.packets = nil 379 380 world.advanceNanos(at) 381 382 if player.nano != 100 || player.nanoReserved != 0 || !player.nanoRechargeUntil.IsZero() || len(player.activeNanos) != 0 || len(conn.packets) != 2 || 383 binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 || len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 384 t.Fatal("failed COMMIT charged a cast or published an effect") 385 } 386 }