server/nano_health_payment_test.go (11842B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "slices" 6 "testing" 7 "time" 8 ) 9 10 func newHealthPaymentWorld(t *testing.T) *server { 11 t.Helper() 12 world, _ := newTeamWorld(t) 13 for _, player := range world.characters { 14 player.profession, player.level = 7, 10 15 player.stats.Body.BodyDevelopment = 55 16 player.stats.NanoAiding.BiologicalMetamorphosis = 10 17 player.stats.NanoAiding.TimeAndSpace = 9 18 player.health, player.nano = 100, 30 19 player.inventory.LearnedNanos = []uint32{43374} 20 if err := world.saveUser(player); err != nil { 21 t.Fatal(err) 22 } 23 if err := world.saveInventory(player, player.inventory); err != nil { 24 t.Fatal(err) 25 } 26 } 27 formTestTeam(t, world, 1, 2) 28 return world 29 } 30 31 func TestHealthPaymentStarterCrystalHealsAndSchedulesDebt(t *testing.T) { 32 world, _ := newNanoCastingWorld(t) 33 player, client := world.characters[1], world.clients[1] 34 player.profession = 7 35 player.stats.NanoAiding.BiologicalMetamorphosis = 10 36 player.stats.NanoAiding.TimeAndSpace = 9 37 player.health, player.nano, player.inventory.LearnedNanos = 20, 30, nil 38 if err := world.saveUser(player); err != nil { 39 t.Fatal(err) 40 } 41 if granted, err := world.grantItem(player, 43378); err != nil || !granted { 42 t.Fatal(granted, err) 43 } 44 45 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 46 t.Fatal(err) 47 } 48 if !slices.Equal(player.inventory.LearnedNanos, []uint32{43374}) { 49 t.Fatal("issued Weak Delayed Health Payment crystal was not learned") 50 } 51 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43374})[20:]); err != nil || client.nanoCast == nil { 52 t.Fatal("starter team heal was not admitted", err) 53 } 54 completed := client.nanoCast.readyAt.Add(time.Nanosecond) 55 world.advanceNanos(completed) 56 57 if player.health < 31 || player.health > 35 || player.nano != 21 || len(player.activeNanos) != 1 || 58 player.activeNanos[0].program != 43372 || player.activeNanos[0].ticksLeft != 5 || player.usedNCU() != 0 { 59 t.Fatal("team heal or caster debt changed", player.health, player.nano, player.activeNanos) 60 } 61 } 62 63 func TestHealthPaymentHealsOnlyEligibleTeamMembers(t *testing.T) { 64 for _, scenario := range []string{"nearby", "other faction", "far", "vertical", "zone", "dead", "offline", "loading"} { 65 t.Run(scenario, func(t *testing.T) { 66 world := newHealthPaymentWorld(t) 67 patient := world.characters[2] 68 switch scenario { 69 case "other faction": 70 patient.appearance = 0x515 71 case "far": 72 patient.position[0] += 16 73 case "vertical": 74 patient.position[1] += 16 75 case "zone": 76 patient.playfield++ 77 case "dead": 78 patient.health = 0 79 case "offline": 80 delete(world.clients, 2) 81 case "loading": 82 world.clients[2].loading = true 83 } 84 before := patient.health 85 86 // A selected nonmember cannot redirect the self-scoped team formula. 87 castTestBuff(t, world, 1, 3, 43374) 88 89 if scenario == "nearby" || scenario == "other faction" { 90 if patient.health < 111 || patient.health > 115 { 91 t.Fatal("eligible teammate did not receive the authored heal", patient.health) 92 } 93 } else if patient.health != before { 94 t.Fatal("ineligible teammate was healed", patient.health) 95 } 96 if world.characters[3].health != 100 || len(patient.activeNanos) != 0 || world.characters[1].health < 111 || world.characters[1].health > 115 { 97 t.Fatal("team heal selected a bystander, omitted caster or charged teammate") 98 } 99 }) 100 } 101 } 102 103 func TestHealthPaymentDebtTicksAndBlocksRecastUntilExpiry(t *testing.T) { 104 world := newHealthPaymentWorld(t) 105 player, client := world.characters[1], world.clients[1] 106 at := castTestBuff(t, world, 1, 1, 43374) 107 health := player.health 108 109 world.advanceNanos(at.Add(2*time.Second - time.Nanosecond)) 110 if player.health != health { 111 t.Fatal("debt ticked before the authored interval") 112 } 113 world.advanceNanos(at.Add(2 * time.Second)) 114 cold := newTestInventoryServer(t).characters[1] 115 if player.health != health-10 || cold.health != health || cold.activeNanos[0].ticksLeft != 5 { 116 t.Fatal("debt tick did not stay in memory until its checkpoint", player.health, cold.activeNanos) 117 } 118 world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 43372})[20:]) 119 world.advanceNanos(at.Add(10 * time.Second)) 120 player.nanoRechargeUntil = time.Time{} 121 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 43374})[20:]) 122 123 if player.health != health-50 || player.nano != 21 || len(player.activeNanos) != 1 || player.activeNanos[0].ticksLeft != 0 || client.nanoCast != nil { 124 t.Fatal("debt was removable, repeated ticks or allowed early recast") 125 } 126 world.advanceNanos(at.Add(15*time.Second - time.Nanosecond)) 127 if len(player.activeNanos) != 1 { 128 t.Fatal("recast restriction expired before 15 seconds") 129 } 130 world.advanceNanos(at.Add(15 * time.Second)) 131 cold = newTestInventoryServer(t).characters[1] 132 if cold.health != health-50 || len(cold.activeNanos) != 0 { 133 t.Fatal("debt expiry did not persist accumulated damage with effect removal", cold.health, cold.activeNanos) 134 } 135 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 43374})[20:]) 136 if len(player.activeNanos) != 0 || client.nanoCast == nil { 137 t.Fatal("debt did not expire or a new cast stayed blocked") 138 } 139 } 140 141 func TestHealthPaymentDebtDeathPersistsWithoutRewards(t *testing.T) { 142 world := newHealthPaymentWorld(t) 143 player := world.characters[1] 144 player.health = 1 145 if err := world.savePosition(player); err != nil { 146 t.Fatal(err) 147 } 148 at := castTestBuff(t, world, 1, 1, 43374) 149 150 world.advanceNanos(at.Add(4 * time.Second)) 151 cold := newTestInventoryServer(t).characters[1] 152 153 if player.health != 0 || cold.health != 0 || player.experience != 0 || cold.experience != 0 || 154 len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || world.characters[2].health < 111 { 155 t.Fatal("self-damage death lost debt, granted XP or undid the team heal") 156 } 157 } 158 159 func TestHealthPaymentDoesNotRequireFreeNCU(t *testing.T) { 160 world := newHealthPaymentWorld(t) 161 player := world.characters[1] 162 // Pre-existing buffs occupy the ordinary eight NCU slots. 163 player.activeNanos = []activeNano{ 164 {program: 26342, caster: 1, remaining: time.Minute}, 165 {program: 27086, caster: 1, remaining: time.Minute}, 166 {program: 26360, caster: 1, remaining: time.Minute}, 167 } 168 if player.usedNCU() != 8 || player.maximumNCU() != 8 { 169 t.Fatal("fixture has free NCU") 170 } 171 172 castTestBuff(t, world, 1, 1, 43374) 173 174 if player.health < 111 || player.nano != 21 || len(player.activeNanos) != 4 || player.usedNCU() != 8 { 175 t.Fatal("instant team heal required free NCU") 176 } 177 } 178 179 func TestHealthPaymentOutcomeFailureIsAtomic(t *testing.T) { 180 for _, failure := range []string{"debt", "ignored debt", "member", "ignored member", "commit"} { 181 t.Run(failure, func(t *testing.T) { 182 world := newHealthPaymentWorld(t) 183 player, client := world.characters[1], world.clients[1] 184 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 43374})[20:]) 185 at := client.nanoCast.readyAt.Add(time.Nanosecond) 186 statement := "CREATE TRIGGER fail_payment BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'debt'); END" 187 switch failure { 188 case "ignored debt": 189 statement = "CREATE TRIGGER fail_payment BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END" 190 case "member": 191 statement = "CREATE TRIGGER fail_payment BEFORE UPDATE OF health ON characters WHEN NEW.id=2 BEGIN SELECT RAISE(FAIL, 'member'); END" 192 case "ignored member": 193 statement = "CREATE TRIGGER fail_payment BEFORE UPDATE OF health ON characters WHEN NEW.id=2 BEGIN SELECT RAISE(IGNORE); END" 194 case "commit": 195 statement = "CREATE TABLE invalid_payment (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_payment AFTER UPDATE OF health ON characters WHEN NEW.id=2 BEGIN INSERT INTO invalid_payment VALUES (999999); END" 196 } 197 if _, err := world.db.Exec(statement); err != nil { 198 t.Fatal(err) 199 } 200 201 world.advanceNanos(at) 202 cold := newTestInventoryServer(t) 203 204 if player.health != 100 || world.characters[2].health != 100 || player.nano != 30 || player.nanoReserved != 0 || 205 len(player.activeNanos) != 0 || cold.characters[1].health != 100 || cold.characters[2].health != 100 || cold.characters[1].nano != 30 { 206 t.Fatal("failed team cast published a partial outcome") 207 } 208 if _, err := world.db.Exec("DROP TRIGGER fail_payment"); err != nil { 209 t.Fatal(err) 210 } 211 castTestBuff(t, world, 1, 1, 43374) 212 if player.health < 111 || world.characters[2].health < 111 || player.nano != 21 || len(player.activeNanos) != 1 { 213 t.Fatal("retry failed or charged twice") 214 } 215 }) 216 } 217 } 218 219 func TestHealthPaymentRejectsIncompleteLinkedEffects(t *testing.T) { 220 for _, program := range []uint32{43374, 43373, 43372} { 221 original := gamedata.NanoTemplates[program] 222 clone := *original 223 clone.Sections = slices.Clone(original.Sections) 224 index := 2 225 if program == 43373 { 226 index = 0 227 } 228 functions := slices.Clone(*clone.Sections[index].Functions) 229 functions[0].TickCount++ 230 clone.Sections[index].Functions = &functions 231 gamedata.NanoTemplates[program] = &clone 232 233 admitted := nanoCrystalProgram(43378) 234 235 gamedata.NanoTemplates[program] = original 236 if admitted != 0 { 237 t.Fatal("changed linked contract admitted a partial formula", program) 238 } 239 } 240 } 241 242 func TestHealthPaymentFailedPersistenceRetainsDebtProgressAndRetries(t *testing.T) { 243 for _, failure := range []string{"health", "ignored health", "ticks", "ignored ticks", "death"} { 244 t.Run(failure, func(t *testing.T) { 245 world := newHealthPaymentWorld(t) 246 player := world.characters[1] 247 at := castTestBuff(t, world, 1, 1, 43374).Add(2 * time.Second) 248 if failure == "death" { 249 player.health = 1 250 if err := world.savePosition(player); err != nil { 251 t.Fatal(err) 252 } 253 } 254 before := player.health 255 statement := "CREATE TRIGGER fail_debt BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(FAIL, 'health'); END" 256 switch failure { 257 case "ignored health": 258 statement = "CREATE TRIGGER fail_debt BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(IGNORE); END" 259 case "ticks": 260 statement = "CREATE TRIGGER fail_debt BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(FAIL, 'ticks'); END" 261 case "ignored ticks": 262 statement = "CREATE TRIGGER fail_debt BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(IGNORE); END" 263 } 264 if _, err := world.db.Exec(statement); err != nil { 265 t.Fatal(err) 266 } 267 268 world.advanceNanos(at) 269 if failure != "death" { 270 player.nextPositionSave = at 271 if err := world.flushPositions(at); err == nil { 272 t.Fatal("failed checkpoint accepted debt progress") 273 } 274 } 275 cold := newTestInventoryServer(t).characters[1] 276 277 wantHealth, wantTicks := before, uint32(5) 278 if failure != "death" { 279 wantHealth, wantTicks = before-10, 4 280 } 281 if player.health != wantHealth || player.activeNanos[0].ticksLeft != wantTicks || cold.health != before || cold.activeNanos[0].ticksLeft != 5 { 282 t.Fatal("failed persistence changed the saved debt or lost live progress") 283 } 284 if _, err := world.db.Exec("DROP TRIGGER fail_debt"); err != nil { 285 t.Fatal(err) 286 } 287 world.advanceNanos(at) 288 world.advanceNanos(at) 289 if failure != "death" { 290 if err := world.flushPositions(at); err != nil { 291 t.Fatal(err) 292 } 293 cold = newTestInventoryServer(t).characters[1] 294 if cold.health != before-10 || cold.activeNanos[0].ticksLeft != 4 { 295 t.Fatal("checkpoint retry did not persist exactly one debt tick") 296 } 297 } 298 if failure == "death" { 299 if player.health != 0 || len(player.activeNanos) != 0 { 300 t.Fatal("lethal debt retry did not settle death") 301 } 302 } else if player.health != before-10 || player.activeNanos[0].ticksLeft != 4 { 303 t.Fatal("debt retry lost or duplicated a tick") 304 } 305 }) 306 } 307 }