server/brawl_test.go (10925B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "reflect" 7 "testing" 8 "time" 9 ) 10 11 func brawlRequest(owner, target uint32) []byte { 12 request := flingShotRequest(owner, target) 13 binary.BigEndian.PutUint32(request[len(request)-4:], 142) 14 return request 15 } 16 17 func TestBrawlUsesEffectiveSkillAndOwnItemWithOrWithoutWeapon(t *testing.T) { 18 for _, scenario := range []struct { 19 skill uint16 20 damage uint32 21 }{{0, 0}, {3, 2}, {20, 11}, {21, 11}, {1000, 1750}, {1001, 1751}} { 22 for _, armed := range []bool{false, true} { 23 t.Run(fmt.Sprint(scenario.skill, "/", armed), func(t *testing.T) { 24 world, _ := newBrawlWorld(t) 25 player, target := world.characters[1], world.characters[1000000] 26 player.stats.Body.Brawling, player.stats.Body.MartialArts = scenario.skill, 1000 27 target.health = 10000 28 world.combatRoll = landedCombatRoll 29 if armed { 30 player.inventory.Items = []ownedItem{{ID: 1, Type: 51018, Template: 129026, Quality: 10, Count: 1, Slot: 6}} 31 } 32 33 err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]) 34 35 if err != nil || target.health != 10000-scenario.damage || len(target.activeNanos) != 0 || player.skillLocks[142].IsZero() { 36 t.Fatal("Brawl used a held weapon/MA damage or wrong skill endpoint", target.health, err) 37 } 38 }) 39 } 40 } 41 } 42 43 func TestBrawlMissCanStunAndDoesNotSpendAmmunitionOrRepeat(t *testing.T) { 44 world, _ := newBrawlWorld(t) 45 player, target := world.characters[1], world.characters[1000000] 46 world.combatRoll = func(limit uint32) uint32 { 47 if limit == 10000 { 48 return 9999 49 } 50 return 0 51 } 52 target.stats.Speed.EvadeClose = 10000 53 before := player.inventory 54 if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil { 55 t.Fatal(err) 56 } 57 attack, recharge := player.nextAttack, player.rechargeUntil 58 59 for range 2 { 60 if err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]); err != nil { 61 t.Fatal(err) 62 } 63 } 64 65 if target.health != 1000 || len(target.activeNanos) != 1 || player.skillLocks[142].IsZero() || 66 player.nextAttack != attack || player.rechargeUntil != recharge || !reflect.DeepEqual(player.inventory, before) { 67 t.Fatal("Brawl miss, duplicate, inventory or ordinary attack clock changed") 68 } 69 } 70 71 func TestBrawlStunStopsPursuitAndAttacksUntilExpiry(t *testing.T) { 72 world, _ := newBrawlWorld(t) 73 player, target := world.characters[1], world.characters[1000000] 74 target.motion = &npcMotion{moving: true, home: target.position, destination: player.position} 75 target.running = true 76 if err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]); err != nil { 77 t.Fatal(err) 78 } 79 deadline := target.activeNanos[0].expiresAt 80 health := player.health 81 target.nextAttack = deadline.Add(-time.Second) 82 83 world.advanceCombat(deadline.Add(-time.Nanosecond)) 84 85 if player.health != health || target.motion.moving || target.running || !target.movementRestricted() { 86 t.Fatal("stunned creature moved or retaliated", player.health, target.motion) 87 } 88 world.advanceNanos(deadline) 89 world.advanceCombat(deadline) 90 if target.movementRestricted() || len(target.activeNanos) != 0 || player.health >= health { 91 t.Fatal("expired stun did not restore retaliation", player.health, health, target.activeNanos) 92 } 93 if cold := newTestInventoryServer(t); cold.characters[target.id].movementRestricted() { 94 t.Fatal("expired stun returned on restart") 95 } 96 } 97 98 func TestBrawlStunChanceUsesQualityAndPreservesIndependentRoot(t *testing.T) { 99 for _, scenario := range []struct { 100 skill uint16 101 roll uint32 102 stunned bool 103 }{{20, 9, true}, {20, 10, false}, {1000, 19, true}, {1000, 20, false}} { 104 t.Run(fmt.Sprint(scenario.skill, "/", scenario.roll), func(t *testing.T) { 105 world, _ := newBrawlWorld(t) 106 player, target := world.characters[1], world.characters[1000000] 107 player.stats.Body.Brawling = scenario.skill 108 target.stats.Speed.EvadeClose = 10000 109 target.activeNanos = []activeNano{{program: 42110, caster: player.id, expiresAt: time.Now().Add(time.Minute)}} 110 world.combatRoll = func(limit uint32) uint32 { 111 if limit == 10000 { 112 return 9999 113 } 114 return scenario.roll 115 } 116 117 err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]) 118 119 if err != nil || target.brawlStunned() != scenario.stunned || !target.movementRestricted() || target.health != 1000 { 120 t.Fatal("wrong stun chance or unrelated root lost", target.activeNanos, err) 121 } 122 world.advanceNanos(time.Now().Add(3 * time.Second)) 123 if target.brawlStunned() || !target.movementRestricted() || len(target.activeNanos) != 1 || target.activeNanos[0].program != 42110 { 124 t.Fatal("stun expiry cleared another effect") 125 } 126 }) 127 } 128 } 129 130 func TestBrawlRefreshesExistingStunWithoutDuplicates(t *testing.T) { 131 world, _ := newBrawlWorld(t) 132 target := world.characters[1000000] 133 previous := time.Now().Add(time.Second) 134 target.activeNanos = []activeNano{{program: 125772, caster: 1, expiresAt: previous}} 135 if err := world.flushWorld(); err != nil { 136 t.Fatal(err) 137 } 138 139 err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]) 140 cold := newTestInventoryServer(t) 141 142 if err != nil || len(target.activeNanos) != 1 || !target.activeNanos[0].expiresAt.After(previous) || 143 len(cold.characters[target.id].activeNanos) != 1 || !cold.characters[target.id].brawlStunned() { 144 t.Fatal("stun refresh duplicated or failed to persist the new duration", target.activeNanos, err) 145 } 146 } 147 148 func TestBrawlFailedOutcomeRollsBackDamageStunLockAndRewards(t *testing.T) { 149 for index, failure := range []string{ 150 "BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(IGNORE); END", 151 "BEFORE UPDATE OF health ON characters WHEN NEW.id=1000000 BEGIN SELECT RAISE(FAIL,'target'); END", 152 "BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END", 153 "BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'loot'); END", 154 "BEFORE UPDATE OF experience ON characters WHEN NEW.id=1 BEGIN SELECT RAISE(IGNORE); END", 155 } { 156 t.Run(fmt.Sprint(index), func(t *testing.T) { 157 world, output := newBrawlWorld(t) 158 player, target := world.characters[1], world.characters[1000000] 159 if index >= 3 { 160 target.health = 1 161 } 162 before := target.health 163 if err := world.flushWorld(); err != nil { 164 t.Fatal(err) 165 } 166 if _, err := world.db.Exec("CREATE TRIGGER fail_brawl " + failure); err != nil { 167 t.Fatal(err) 168 } 169 170 err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]) 171 cold := newTestInventoryServer(t) 172 173 if err == nil || target.health != before || len(target.activeNanos) != 0 || target.corpse != nil || 174 !player.skillLocks[142].IsZero() || player.experience != 0 || len(output.packets) != 0 || 175 cold.characters[target.id].health != before || len(cold.characters[target.id].activeNanos) != 0 || 176 !cold.characters[1].skillLocks[142].IsZero() || cold.characters[1].experience != 0 { 177 t.Fatal("failed Brawl retained a partial outcome", err) 178 } 179 if _, err := world.db.Exec("DROP TRIGGER fail_brawl"); err != nil { 180 t.Fatal(err) 181 } 182 if err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]); err != nil { 183 t.Fatal(err) 184 } 185 cold = newTestInventoryServer(t) 186 if index >= 3 { 187 if target.health != 0 || target.corpse == nil || len(target.corpse.items) != 1 || player.experience != 100 || 188 cold.characters[1].experience != 100 || cold.characters[target.id].corpse == nil || len(target.activeNanos) != 0 { 189 t.Fatal("retried Brawl kill did not commit once") 190 } 191 } else if target.health != 976 || !target.movementRestricted() || !cold.characters[target.id].movementRestricted() { 192 t.Fatal("retried Brawl did not commit damage and stun") 193 } 194 }) 195 } 196 } 197 198 func newBrawlWorld(t *testing.T) (*server, *combatTestConn) { 199 t.Helper() 200 world, output := newKillExperienceWorld(t, "unarmed") 201 player := world.characters[1] 202 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 203 player.stats.Body.Brawling = 20 204 world.characters[1000000].health = 1000 205 world.combatRoll = func(uint32) uint32 { return 0 } 206 if err := world.saveUser(player); err != nil { 207 t.Fatal(err) 208 } 209 if err := world.flushWorld(); err != nil { 210 t.Fatal(err) 211 } 212 return world, output 213 } 214 215 func TestBrawlCommitsOwnDamageStunAndCooldown(t *testing.T) { 216 world, output := newBrawlWorld(t) 217 player, target := world.characters[1], world.characters[1000000] 218 before := time.Now() 219 220 err := world.handleWeaponSpecial(world.clients[1], brawlRequest(1, target.id)[20:]) 221 cold := newTestInventoryServer(t) 222 223 if err != nil || target.health != 976 || len(target.activeNanos) != 1 || target.activeNanos[0].program != 125772 || 224 !target.movementRestricted() || player.skillLocks[142].Before(before.Add(15*time.Second)) || 225 player.skillLocks[142].After(time.Now().Add(15*time.Second)) { 226 t.Fatal("Brawl omitted its own damage, stun or cooldown", target.health, target.activeNanos, player.skillLocks, err) 227 } 228 if cold.characters[target.id].health != 976 || !cold.characters[target.id].movementRestricted() || 229 !cold.characters[1].skillLocks[142].Equal(player.skillLocks[142]) { 230 t.Fatal("Brawl outcome did not recover atomically") 231 } 232 if binary.BigEndian.Uint32(output.packets[0][16:20]) != 0x754f1115 || 233 binary.BigEndian.Uint32(output.packets[0][29:33]) != 0 || binary.BigEndian.Uint32(output.packets[0][49:53]) != 142 { 234 t.Fatal("unarmed Brawl result did not identify slot zero and skill 142") 235 } 236 } 237 238 func TestBrawlRejectsInvalidUseWithoutOutcome(t *testing.T) { 239 for _, scenario := range []string{"ranged", "range", "reload", "equipping", "locked", "sitting", "hidden", "unready", "owner", "dead"} { 240 t.Run(scenario, func(t *testing.T) { 241 world, output := newBrawlWorld(t) 242 player, client := world.characters[1], world.clients[1] 243 request := brawlRequest(1, 1000000)[20:] 244 switch scenario { 245 case "ranged": 246 player.inventory.Items = []ownedItem{{ID: 1, Type: 51018, Template: 123742, Quality: 1, Count: 1, Slot: 6}} 247 case "range": 248 world.characters[1000000].position[0] = 10 249 case "reload": 250 client.reloadUntil = time.Now().Add(time.Minute) 251 case "equipping": 252 client.equipment = &equipmentMove{} 253 case "locked": 254 player.setSkillLock(142, time.Now().Add(time.Minute)) 255 case "sitting": 256 player.sitting = true 257 case "hidden": 258 delete(client.npcs, 1000000) 259 case "unready": 260 client.ready = false 261 case "owner": 262 binary.BigEndian.PutUint32(request[8:], 2) 263 case "dead": 264 player.health = 0 265 } 266 before, deadline := player.inventory, player.skillLocks[142] 267 268 err := world.handleWeaponSpecial(client, request) 269 270 if err != nil || world.characters[1000000].health != 1000 || len(world.characters[1000000].activeNanos) != 0 || 271 !player.skillLocks[142].Equal(deadline) || !reflect.DeepEqual(player.inventory, before) || len(output.packets) != 0 { 272 t.Fatal("rejected Brawl changed state or feedback", err) 273 } 274 }) 275 } 276 }