aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/fast_attack_test.go (10411B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"reflect"
      8 	"sync"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func fastAttackRequest(owner, target uint32) []byte {
     14 	var body []byte
     15 	for _, value := range []uint32{50000, target, 147} {
     16 		body = binary.BigEndian.AppendUint32(body, value)
     17 	}
     18 	return n3Packet(0x51492120, 50000, owner, body)
     19 }
     20 
     21 func newFastAttackWorld(t *testing.T) (*server, *combatTestConn) {
     22 	t.Helper()
     23 	world, conn := newEquipmentWorld(t)
     24 	world.nextNPC = 1000001
     25 	world.characters[1000000].health = 1000
     26 	if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 6)[20:]); err != nil {
     27 		t.Fatal(err)
     28 	}
     29 	world.advanceEquipment(world.clients[1].equipment.readyAt)
     30 	conn.packets = nil
     31 	return world, conn
     32 }
     33 
     34 func TestFastAttackPublishesWeaponDamageAndPersistsItsOwnLock(t *testing.T) {
     35 	world, owner := newFastAttackWorld(t)
     36 	player, target := world.characters[1], world.characters[1000000]
     37 	player.stats.Melee.OneHandEdged = 400
     38 	observer := &combatTestConn{}
     39 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     40 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, target: true, npcs: map[uint32]bool{1000000: true}}
     41 	inventory := player.inventory
     42 	inventory.Items = append([]ownedItem(nil), player.inventory.Items...)
     43 	started := time.Now()
     44 
     45 	err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, 1000000)[20:])
     46 	restored := newTestInventoryServer(t)
     47 
     48 	if err != nil || target.health < 960 || target.health > 990 || target.fighting != 1 ||
     49 		target.damageContributors[damageContributor{player: 1}] != 1000-target.health ||
     50 		!reflect.DeepEqual(player.inventory, inventory) ||
     51 		!reflect.DeepEqual(restored.characters[1].inventory.Items, inventory.Items) ||
     52 		!restored.characters[1].skillLocks[147].Equal(player.skillLocks[147]) ||
     53 		player.skillLocks[147].Before(started.Add(19*time.Second)) ||
     54 		player.skillLocks[147].After(time.Now().Add(19*time.Second)) || !player.skillLocks[150].IsZero() {
     55 		t.Fatalf("Fast Attack damage, inventory or durable lock mismatch: HP=%d locks=%v, %v", target.health, player.skillLocks, err)
     56 	}
     57 	if len(owner.packets) != 3 || len(observer.packets) != 2 ||
     58 		binary.BigEndian.Uint32(owner.packets[0][16:20]) != 0x754f1115 ||
     59 		!bytes.Equal(owner.packets[0][16:], observer.packets[0][16:]) ||
     60 		binary.BigEndian.Uint32(owner.packets[0][29:33]) != 6 ||
     61 		binary.BigEndian.Uint32(owner.packets[0][33:37]) != 1000-target.health ||
     62 		binary.BigEndian.Uint32(owner.packets[0][37:41]) != ^uint32(0) ||
     63 		binary.BigEndian.Uint32(owner.packets[0][45:49]) != target.id ||
     64 		binary.BigEndian.Uint32(owner.packets[0][49:53]) != 147 ||
     65 		binary.BigEndian.Uint32(owner.packets[1][29:33]) != 170 ||
     66 		binary.BigEndian.Uint32(owner.packets[1][41:45]) != 147 ||
     67 		binary.BigEndian.Uint32(owner.packets[1][45:49]) != 19 {
     68 		t.Fatalf("Fast Attack native result/lock mismatch: %x", owner.packets)
     69 	}
     70 }
     71 
     72 func TestFastAttackRejectsInvalidRequestsWithoutDamageOrLocks(t *testing.T) {
     73 	for _, scenario := range []string{"unready", "loading", "replaced", "old client", "dead owner", "wrong owner", "wrong owner type", "wrong target type", "unsupported skill", "unknown target", "player target", "dead target", "hidden target", "other zone", "out of melee range", "sitting", "driving", "equip pending", "reloading", "locked", "unarmed", "stored axe", "pistol", "briefcase", "unsupported quality"} {
     74 		t.Run(scenario, func(t *testing.T) {
     75 			world, conn := newFastAttackWorld(t)
     76 			player, target, client := world.characters[1], world.characters[1000000], world.clients[1]
     77 			request := fastAttackRequest(1, 1000000)[20:]
     78 			switch scenario {
     79 			case "unready":
     80 				client.ready = false
     81 			case "loading":
     82 				client.loading = true
     83 			case "replaced":
     84 				world.clients[1] = &worldClient{id: 1}
     85 			case "old client":
     86 				client.target = false
     87 			case "dead owner":
     88 				player.health = 0
     89 			case "wrong owner":
     90 				binary.BigEndian.PutUint32(request[8:12], 2)
     91 			case "wrong owner type":
     92 				binary.BigEndian.PutUint32(request[4:8], 51018)
     93 			case "wrong target type":
     94 				binary.BigEndian.PutUint32(request[16:20], 51018)
     95 			case "unsupported skill":
     96 				binary.BigEndian.PutUint32(request[24:28], 143)
     97 			case "unknown target":
     98 				binary.BigEndian.PutUint32(request[20:24], 2000000)
     99 			case "player target":
    100 				target.npc = false
    101 			case "dead target":
    102 				target.health = 0
    103 			case "hidden target":
    104 				delete(client.npcs, 1000000)
    105 			case "other zone":
    106 				target.playfield = 705
    107 			case "out of melee range":
    108 				target.position[0] = 20
    109 			case "sitting":
    110 				player.sitting = true
    111 			case "driving":
    112 				player.inventory.Items = append(player.inventory.Items, ownedItem{ID: 3, Type: 51022, Template: 117294, Quality: 15, Slot: 1, Count: 1})
    113 			case "equip pending":
    114 				client.equipment = &equipmentMove{}
    115 			case "reloading":
    116 				client.reloadUntil = time.Now().Add(time.Hour)
    117 			case "locked":
    118 				player.skillLocks = map[uint32]time.Time{147: time.Now().Add(time.Hour)}
    119 			case "unarmed":
    120 				player.inventory.Items = nil
    121 			case "stored axe":
    122 				player.inventory.Items[0].Slot = 64
    123 			case "pistol":
    124 				player.inventory.Items[0].Template, player.inventory.Items[0].Ammo = 123742, 3
    125 			case "briefcase":
    126 				player.inventory.Items[0].Template, player.inventory.Items[0].Quality = 205736, 100
    127 			case "unsupported quality":
    128 				player.inventory.Items[0].Quality = 25
    129 			}
    130 			deadline := player.skillLocks[147]
    131 			health := target.health
    132 
    133 			err := world.handleWeaponSpecial(client, request)
    134 
    135 			if err != nil || target.health != health || player.skillLocks[147] != deadline || len(conn.packets) != 0 {
    136 				t.Fatalf("invalid Fast Attack changed state: HP=%d locks=%v, %v", target.health, player.skillLocks, err)
    137 			}
    138 		})
    139 	}
    140 }
    141 
    142 func TestFastAttackConcurrentRequestsApplyOnceWithoutResettingAttackOrCast(t *testing.T) {
    143 	world, conn := newFastAttackWorld(t)
    144 	player, client := world.characters[1], world.clients[1]
    145 	world.handleCombat(client, attackPacket(1, 1000000)[20:])
    146 	world.advanceCombat(player.nextAttack)
    147 	attack, recharge, health := player.nextAttack, player.rechargeUntil, world.characters[1000000].health
    148 	cast := &nanoCast{target: 1000000, readyAt: time.Now().Add(time.Second)}
    149 	client.nanoCast = cast
    150 	conn.packets = nil
    151 	var calls sync.WaitGroup
    152 
    153 	zone, stop := startTestZone(t, world)
    154 
    155 	for range 8 {
    156 		calls.Go(func() {
    157 			if err := zone.call(zonePacket{client, fastAttackRequest(1, 1000000)[20:]}); err != nil {
    158 				t.Error(err)
    159 			}
    160 		})
    161 	}
    162 	calls.Wait()
    163 	if err := stop(); err != nil {
    164 		t.Fatal(err)
    165 	}
    166 	count := 0
    167 	for _, packet := range conn.packets {
    168 		if binary.BigEndian.Uint32(packet[16:20]) == 0x754f1115 {
    169 			count++
    170 		}
    171 	}
    172 
    173 	if count != 1 || world.characters[1000000].health >= health || player.nextAttack != attack ||
    174 		player.rechargeUntil != recharge || client.nanoCast != cast {
    175 		t.Fatal("Fast Attack duplicated its hit or reset normal attack/cast state")
    176 	}
    177 }
    178 
    179 func TestFastAttackFailedOutcomePublishesNothingAndRollsBackItsLock(t *testing.T) {
    180 	for _, statement := range []string{
    181 		"CREATE TRIGGER fail_special BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(FAIL, 'test lock failure'); END",
    182 		"CREATE TRIGGER fail_special BEFORE UPDATE OF health ON characters WHEN NEW.id=1000000 BEGIN SELECT RAISE(FAIL, 'test health failure'); END",
    183 		"CREATE TRIGGER fail_special BEFORE UPDATE ON npcs BEGIN SELECT RAISE(FAIL, 'test NPC failure'); END",
    184 		"CREATE TRIGGER fail_special BEFORE INSERT ON npc_damage_contributors BEGIN SELECT RAISE(FAIL, 'test contribution failure'); END",
    185 	} {
    186 		if !t.Run(statement, func(t *testing.T) {
    187 			world, conn := newFastAttackWorld(t)
    188 			if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    189 				t.Fatal(err)
    190 			}
    191 			conn.packets = nil
    192 			if _, err := world.db.Exec(statement); err != nil {
    193 				t.Fatal(err)
    194 			}
    195 
    196 			err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, 1000000)[20:])
    197 			restored := newTestInventoryServer(t)
    198 
    199 			if err == nil || world.characters[1000000].health != 1000 || len(conn.packets) != 0 ||
    200 				len(world.characters[1].skillLocks) != 0 || len(restored.characters[1].skillLocks) != 0 {
    201 				t.Fatal("failed Fast Attack changed durable/live outcome or published feedback")
    202 			}
    203 		}) {
    204 			return
    205 		}
    206 	}
    207 }
    208 
    209 func TestFastAttackKillAndCooldownRecoverTogether(t *testing.T) {
    210 	world, conn := newFastAttackWorld(t)
    211 	target := world.characters[1000000]
    212 	target.health, target.monsterData, target.level = 1, 17655, 1
    213 	target.combatProfile = gamedata.NPCCombatProfiles[17655][1]
    214 
    215 	err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, 1000000)[20:])
    216 	restored := newTestInventoryServer(t)
    217 	corpse := restored.characters[1000000]
    218 	deadline := restored.characters[1].skillLocks[147]
    219 
    220 	if err != nil || corpse.health != 0 || corpse.corpse == nil || corpse.corpse.looter != 1 ||
    221 		corpse.corpse.expiresAt.IsZero() || len(restored.npcRespawns) != 1 ||
    222 		!deadline.Equal(world.characters[1].skillLocks[147]) ||
    223 		binary.BigEndian.Uint32(conn.packets[0][33:37]) != 1 ||
    224 		binary.BigEndian.Uint32(conn.packets[0][53:57]) != 4 {
    225 		t.Fatal("Fast Attack kill/corpse/lock did not recover atomically", err)
    226 	}
    227 }
    228 
    229 func TestFastAttackStoredLockRejectsAfterRestartAndExpiresOnce(t *testing.T) {
    230 	world, _ := newFastAttackWorld(t)
    231 	if err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, 1000000)[20:]); err != nil {
    232 		t.Fatal(err)
    233 	}
    234 	restored := newTestInventoryServer(t)
    235 	conn := &combatTestConn{}
    236 	restored.clients = map[uint32]*worldClient{1: {id: 1, ready: true, target: true, conn: conn, npcs: map[uint32]bool{1000000: true}}}
    237 	deadline, health := restored.characters[1].skillLocks[147], restored.characters[1000000].health
    238 
    239 	err := restored.handleWeaponSpecial(restored.clients[1], fastAttackRequest(1, 1000000)[20:])
    240 	restored.advanceItemEffects(deadline.Add(-time.Nanosecond))
    241 	if len(conn.packets) != 0 || restored.characters[1000000].health != health {
    242 		t.Fatal("restarted Fast Attack bypassed its persisted lock")
    243 	}
    244 	restored.advanceItemEffects(deadline)
    245 	restored.advanceItemEffects(deadline.Add(time.Second))
    246 
    247 	if err != nil || len(conn.packets) != 1 || !restored.characters[1].skillLocks[147].IsZero() ||
    248 		binary.BigEndian.Uint32(conn.packets[0][29:33]) != 164 || binary.BigEndian.Uint32(conn.packets[0][45:49]) != 147 {
    249 		t.Fatal("Fast Attack lock did not expire exactly once", err)
    250 	}
    251 }