server/ability_nano_test.go (10433B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "slices" 7 "testing" 8 "time" 9 ) 10 11 func TestAbilityNanoFamiliesApplyAndRemoveOneContribution(t *testing.T) { 12 for _, nano := range []struct{ program, stat, health, points uint32 }{ 13 {26342, 17, 37, 620}, {27086, 19, 37, 620}, {27134, 21, 37, 626}, 14 {27154, 20, 37, 620}, {27168, 18, 46, 620}, {27169, 16, 37, 620}, 15 } { 16 t.Run(fmt.Sprint(nano.program), func(t *testing.T) { 17 world, _ := newNanoBuffWorld(t) 18 player := world.characters[1] 19 player.inventory.LearnedNanos = []uint32{nano.program} 20 21 castTestBuff(t, world, 1, 1, nano.program) 22 restored := newTestInventoryServer(t).characters[1] 23 24 if player.effectiveAbility(nano.stat) != 22 || player.usedNCU() != 3 || player.maximumHealth() != nano.health || player.maximumNano() != nano.points || 25 restored.effectiveAbility(nano.stat) != 22 || restored.maximumHealth() != nano.health || restored.maximumNano() != nano.points || player.health != 37 || player.nano != 56 { 26 t.Fatal("ability changed the wrong contribution, maximum or current resource", player.maximumHealth(), player.maximumNano()) 27 } 28 if err := world.handleNanoRemoval(world.clients[1], characterActionPacket(1, [5]uint32{65, 0, 0, 53019, nano.program})[20:]); err != nil { 29 t.Fatal(err) 30 } 31 if player.effectiveAbility(nano.stat) != 10 || player.maximumHealth() != 37 || player.maximumNano() != 620 || player.usedNCU() != 0 { 32 t.Fatal("removal retained a derived contribution") 33 } 34 }) 35 } 36 } 37 38 func TestAbilityNanoNCUExactFitRefreshAndReplacement(t *testing.T) { 39 world, conn := newNanoBuffWorld(t) 40 player, client := world.characters[1], world.clients[1] 41 player.inventory.LearnedNanos = []uint32{26342, 26360, 27086, 27169} 42 player.nano = 300 43 for _, program := range []uint32{26342, 27086, 26360} { 44 player.nanoRechargeUntil = time.Time{} 45 castTestBuff(t, world, 1, 1, program) 46 } 47 before, packets := player.nano, len(conn.packets) 48 player.nanoRechargeUntil = time.Time{} 49 50 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:]) 51 52 if err != nil || player.usedNCU() != 8 || player.maximumNCU() != 8 || player.nano != before || client.nanoCast != nil || len(conn.packets) != packets+1 || 53 player.effectiveAbility(16) != 16 || player.effectiveAbility(17) != 22 || player.effectiveAbility(19) != 22 { 54 t.Fatal("over-capacity replacement was charged or installed", err) 55 } 56 castTestBuff(t, world, 1, 1, 26342) 57 if player.usedNCU() != 8 || len(player.activeNanos) != 3 || player.nano != before-44 { 58 t.Fatal("refresh at capacity stacked or required the full cost twice") 59 } 60 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 27086})[20:]); err != nil { 61 t.Fatal(err) 62 } 63 player.nanoRechargeUntil = time.Time{} 64 castTestBuff(t, world, 1, 1, 27169) 65 if player.usedNCU() != 6 || player.effectiveAbility(16) != 22 || len(player.activeNanos) != 2 || newTestInventoryServer(t).characters[1].usedNCU() != 6 { 66 t.Fatal("freeing NCU did not allow one durable same-line upgrade") 67 } 68 } 69 70 func TestAbilityNanoNCUMemoryAllowsPreviouslyRejectedProgram(t *testing.T) { 71 world, _ := newNanoBuffWorld(t) 72 player, client := world.characters[1], world.clients[1] 73 player.nano = 300 74 player.inventory.LearnedNanos = []uint32{26342, 27086, 27169} 75 for _, program := range []uint32{26342, 27086} { 76 player.nanoRechargeUntil = time.Time{} 77 castTestBuff(t, world, 1, 1, program) 78 } 79 player.nanoRechargeUntil = time.Time{} 80 request := characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:] 81 if err := world.handleNanoCast(client, request); err != nil || client.nanoCast != nil || player.nano != 212 || player.usedNCU() != 6 { 82 t.Fatal("third 3-NCU program fitted into 8 NCU", err) 83 } 84 for _, template := range []uint32{36783, 36779} { 85 if granted, err := world.grantItem(player, template); err != nil || !granted { 86 t.Fatal(granted, err) 87 } 88 } 89 90 for _, move := range [][2]uint32{{64, 7}, {65, 9}} { 91 if err := world.handleInventory(client, inventoryMovePacket(1, move[0], move[1])[20:]); err != nil || client.equipment == nil { 92 t.Fatal("belt or memory was not admitted", err) 93 } 94 world.advanceEquipment(client.equipment.readyAt.Add(time.Nanosecond)) 95 } 96 castTestBuff(t, world, 1, 1, 27169) 97 restored := newTestInventoryServer(t).characters[1] 98 99 if player.maximumNCU() != 10 || player.usedNCU() != 9 || player.nano != 168 || restored.maximumNCU() != 10 || restored.usedNCU() != 9 || 100 player.effectiveAbility(16) != 22 || !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.Template == 36779 && item.Slot == 9 }) { 101 t.Fatal("NCU memory did not allow the previously rejected program") 102 } 103 } 104 105 func TestAbilityNanoResourceClampAndRemovalCommitTogether(t *testing.T) { 106 for _, program := range []uint32{27168, 27134} { 107 t.Run(fmt.Sprint(program), func(t *testing.T) { 108 world, conn := newNanoBuffWorld(t) 109 player := world.characters[1] 110 player.inventory.LearnedNanos = []uint32{program} 111 castTestBuff(t, world, 1, 1, program) 112 player.health, player.nano = player.maximumHealth(), player.maximumNano() 113 if err := world.savePosition(player); err != nil { 114 t.Fatal(err) 115 } 116 if _, err := world.db.Exec("CREATE TRIGGER fail_clamp BEFORE UPDATE OF health,nano ON characters BEGIN SELECT RAISE(FAIL, 'test clamp'); END"); err != nil { 117 t.Fatal(err) 118 } 119 conn.packets = nil 120 request := characterActionPacket(1, [5]uint32{65, 0, 0, 53019, program})[20:] 121 122 err := world.handleNanoRemoval(world.clients[1], request) 123 124 restored := newTestInventoryServer(t).characters[1] 125 if err == nil || len(conn.packets) != 0 || len(player.activeNanos) != 1 || len(restored.activeNanos) != 1 || 126 restored.health != player.health || restored.nano != player.nano { 127 t.Fatalf("failed clamp state: error=%v packets=%d effects=%d/%d resources=%d,%d/%d,%d", err, len(conn.packets), len(player.activeNanos), len(restored.activeNanos), player.health, player.nano, restored.health, restored.nano) 128 } 129 if _, err := world.db.Exec("DROP TRIGGER fail_clamp"); err != nil { 130 t.Fatal(err) 131 } 132 if err := world.handleNanoRemoval(world.clients[1], request); err != nil { 133 t.Fatal(err) 134 } 135 restored = newTestInventoryServer(t).characters[1] 136 if len(restored.activeNanos) != 0 || player.health != 37 || player.nano != 620 || restored.health != 37 || restored.nano != 620 || player.savedVitals != player.vitals() { 137 t.Fatal("removal and resource clamp did not persist together") 138 } 139 maximum := uint32(0) 140 for _, packet := range conn.packets { 141 if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e { 142 stat := binary.BigEndian.Uint32(packet[29:33]) 143 if program == 27168 && stat == 1 || program == 27134 && stat == 221 { 144 maximum = binary.BigEndian.Uint32(packet[33:37]) 145 } 146 } 147 } 148 if program == 27168 && maximum != 37 || program == 27134 && maximum != 620 { 149 t.Fatal("native cached maximum was not corrected", maximum) 150 } 151 }) 152 } 153 } 154 155 func TestAbilityNanoExpiryClampsFullResources(t *testing.T) { 156 world, _ := newNanoBuffWorld(t) 157 player := world.characters[1] 158 player.nano = 300 159 player.inventory.LearnedNanos = []uint32{27168, 27134} 160 castTestBuff(t, world, 1, 1, 27168) 161 player.nanoRechargeUntil = time.Time{} 162 castTestBuff(t, world, 1, 1, 27134) 163 player.health, player.nano = 46, 626 164 if err := world.savePosition(player); err != nil { 165 t.Fatal(err) 166 } 167 168 world.advanceNanos(time.Now().Add(time.Hour)) 169 restored := newTestInventoryServer(t).characters[1] 170 171 if len(player.activeNanos) != 0 || player.health != 37 || player.nano != 620 || len(restored.activeNanos) != 0 || restored.health != 37 || restored.nano != 620 { 172 t.Fatal("expired ability buffs retained their maxima or over-cap resources") 173 } 174 } 175 176 func TestAbilityNanoAnchorsWoundedRecipientsHeldMovement(t *testing.T) { 177 world, _ := newNanoBuffWorld(t) 178 player := world.characters[1] 179 player.inventory.LearnedNanos = []uint32{27168} 180 recipient := *player 181 recipient.id, recipient.name, recipient.health, recipient.running = 2, "Recipient", 4, true 182 recipient.position, recipient.rotation = [3]float32{0, 0, 3}, [4]float32{0, 0, 0, 1} 183 world.characters[2], world.nextCharacter = &recipient, 2 184 world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true} 185 world.characters[2].movement = heldPlayerMovement(world.characters[2], 1, time.Now()) 186 if err := world.saveUser(&recipient); err != nil { 187 t.Fatal(err) 188 } 189 if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, 2, 53019, 27168})[20:]); err != nil { 190 t.Fatal(err) 191 } 192 at := world.clients[1].nanoCast.readyAt.Add(time.Nanosecond) 193 world.characters[2].movement.updatedAt = at.Add(-time.Second) 194 195 world.advanceNanos(at) 196 position := predictedPosition(&recipient, at.Add(time.Second)) 197 198 if recipient.maximumHealth() != 46 || world.characters[2].movement.updatedAt != at || recipient.position[2] < 6.661 || recipient.position[2] > 6.663 || 199 position[2] < 9.900 || position[2] > 9.903 { 200 t.Fatal("ability buff repriced the earlier wounded movement segment", recipient.position, position) 201 } 202 } 203 204 func TestAbilityNanoCompletionRefundsWhenRecipientNCUFills(t *testing.T) { 205 world, _ := newNanoBuffWorld(t) 206 recipient := *world.characters[1] 207 recipient.id, recipient.name = 2, "Recipient" 208 recipient.inventory.LearnedNanos = []uint32{26342, 27086} 209 world.characters[2], world.nextCharacter = &recipient, 2 210 world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true} 211 if err := world.saveUser(&recipient); err != nil { 212 t.Fatal(err) 213 } 214 castTestBuff(t, world, 2, 2, 26342) 215 recipient.nanoRechargeUntil, recipient.aggDef = time.Time{}, 100 216 client := world.clients[1] 217 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 2, 53019, 27169})[20:]); err != nil { 218 t.Fatal(err) 219 } 220 deadline := client.nanoCast.readyAt 221 222 castTestBuff(t, world, 2, 2, 27086) 223 world.advanceNanos(deadline.Add(time.Nanosecond)) 224 world.advanceNanos(deadline.Add(time.Second)) 225 restored := newTestInventoryServer(t) 226 227 if client.nanoCast != nil || world.characters[1].nano != 100 || restored.characters[1].nano != 100 || 228 recipient.usedNCU() != 6 || len(recipient.activeNanos) != 2 || recipient.effectiveAbility(16) != 10 || restored.characters[2].usedNCU() != 6 { 229 t.Fatal("cast completed beyond NCU capacity or failed to refund once") 230 } 231 }