aorecomp

Anarchy Online classic in the browser

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


server/resurrection_test.go (6753B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"encoding/binary"
      6 	"slices"
      7 	"sync"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func resurrectionRequest(owner uint32) []byte {
     13 	body := binary.BigEndian.AppendUint32(nil, 152)
     14 	body = append(body, make([]byte, 16)...)
     15 	return n3Packet(0x5e477770, 50000, owner, body)
     16 }
     17 
     18 func TestResurrectionNativeRequestRedirectsAndPersistsBeforeReconnect(t *testing.T) {
     19 	useNPCData(t, testNPCData())
     20 	useResources(t, assets.Files)
     21 	world := newTestServer(t)
     22 	player := world.newCharacter()
     23 	if err := world.saveUser(player); err != nil {
     24 		t.Fatal(err)
     25 	}
     26 	item := ownedItem{ID: 17, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1}
     27 	if err := world.saveInventory(player, playerInventory{LastID: 17, Items: []ownedItem{item}}); err != nil {
     28 		t.Fatal(err)
     29 	}
     30 	startTestService(t, world)
     31 	connection := connectZoningClient(t, world, true)
     32 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     33 		state.characters[1].health = 0
     34 		state.characters[1].nano = 0
     35 		state.characters[1].returnDoor = &doorOrigin{705, 1}
     36 		return nil
     37 	}))
     38 
     39 	if _, err := connection.Write(targetPacket(resurrectionRequest(1))); err != nil {
     40 		t.Fatal(err)
     41 	}
     42 	for _, key := range []uint32{0x445f2a0b, 0x43197d22, 0x3c} {
     43 		packet, err := readPacket(connection)
     44 		if err != nil {
     45 			t.Fatal(err)
     46 		}
     47 		if binary.BigEndian.Uint32(packet[16:20]) != key {
     48 			t.Fatalf("wrong resurrection sequence: %x", packet)
     49 		}
     50 		if key == 0x445f2a0b && (len(packet) != 37 || binary.BigEndian.Uint32(packet[29:33]) != 1 || binary.BigEndian.Uint32(packet[33:37]) != 1) {
     51 			t.Fatalf("wrong native resurrection resources: %x", packet)
     52 		}
     53 	}
     54 	restarted := newTestInventoryServer(t)
     55 	connection.Close()
     56 	connection = connectZoningClient(t, world, true)
     57 	movement := testPlayerData(t, world, 1).character()
     58 	movement.position[0]++
     59 	connection.Write(targetPacket(npcMovementPacket(movement, 2)))
     60 	ping := systemPacket(1, nil)
     61 	binary.BigEndian.PutUint16(ping[2:4], 11)
     62 	connection.Write(ping)
     63 	if _, err := readPacket(connection); err != nil {
     64 		t.Fatal(err)
     65 	}
     66 
     67 	arrived := restarted.characters[1]
     68 	if arrived.playfield != 952 || arrived.position != [3]float32{42.737694, 14.75, 70.091897} || arrived.returnDoor == nil || *arrived.returnDoor != (doorOrigin{3100, 3876588572}) || !slices.Equal(arrived.inventory.Items, []ownedItem{item}) {
     69 		t.Fatalf("resurrection arrival or belongings did not persist: %+v", arrived)
     70 	}
     71 	var playable bool
     72 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     73 		player, client := state.characters[1], state.clients[1]
     74 		playable = player.health == 1 && player.nano == 1 && player.position == [3]float32{43.625, 14.75, 70} && client.ready && !client.loading
     75 		return nil
     76 	}))
     77 	if !playable {
     78 		t.Fatal("resurrected character cannot play")
     79 	}
     80 }
     81 
     82 func TestResurrectionOnlyOnceAndClearsCombatForBothObservers(t *testing.T) {
     83 	t.Chdir(t.TempDir())
     84 	world, conn := newCombatWorld(t)
     85 	loadResurrectionLocations(t, world)
     86 	player := world.characters[1]
     87 	if err := world.saveUser(player); err != nil {
     88 		t.Fatal(err)
     89 	}
     90 	observer := &combatTestConn{}
     91 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     92 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true}
     93 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
     94 	world.advanceCombat(player.nextAttack)
     95 	player.health = 0
     96 	player.rechargeUntil = time.Now().Add(time.Hour)
     97 	client := world.clients[1]
     98 	conn.packets, observer.packets = nil, nil
     99 
    100 	shutdown := startTestService(t, world)
    101 	owner, err := world.world.owner(1)
    102 	if err != nil {
    103 		t.Fatal(err)
    104 	}
    105 
    106 	var requests sync.WaitGroup
    107 	for range 8 {
    108 		requests.Go(func() {
    109 			if err := owner.call(zonePacket{client: client, body: resurrectionRequest(1)[20:]}); err != nil {
    110 				t.Error(err)
    111 			}
    112 		})
    113 	}
    114 	requests.Wait()
    115 
    116 	var revived, cleared bool
    117 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    118 		player := state.characters[1]
    119 		revived = player.health == 1 && player.nano == 1 && player.fighting == 0 && player.nextAttack.IsZero() && player.rechargeUntil.IsZero()
    120 		return nil
    121 	}))
    122 	if err := owner.call(zoneTestCommand(func(state *server) error {
    123 		cleared = state.characters[1000000].fighting == 0 && len(state.fighting) == 0 && state.clients[1] == nil && !client.ready
    124 		return nil
    125 	})); err != nil {
    126 		t.Fatal(err)
    127 	}
    128 	if err := shutdown(); err != nil {
    129 		t.Fatal(err)
    130 	}
    131 	if !revived || !cleared {
    132 		t.Fatal("resurrection retained combat or a departing session")
    133 	}
    134 	for _, packets := range [][][]byte{conn.packets, observer.packets} {
    135 		count := 0
    136 		for _, packet := range packets {
    137 			if binary.BigEndian.Uint32(packet[16:20]) == 0x445f2a0b {
    138 				count++
    139 			}
    140 		}
    141 		if count != 1 {
    142 			t.Fatalf("observer received %d resurrection packets: %x", count, packets)
    143 		}
    144 	}
    145 	if binary.BigEndian.Uint32(observer.packets[len(observer.packets)-1][16:20]) != 0x36510078 ||
    146 		binary.BigEndian.Uint32(conn.packets[len(conn.packets)-2][16:20]) != 0x43197d22 {
    147 		t.Fatal("observers did not remove the departing character after resurrection")
    148 	}
    149 }
    150 
    151 func TestResurrectionRejectsInvalidRequests(t *testing.T) {
    152 	for _, scenario := range []string{"alive", "loading", "unready", "replaced", "wrong owner", "wrong type"} {
    153 		t.Run(scenario, func(t *testing.T) {
    154 			world, conn := newCombatWorld(t)
    155 			client := world.clients[1]
    156 			world.characters[1].health = 0
    157 			body := resurrectionRequest(1)[20:]
    158 			switch scenario {
    159 			case "alive":
    160 				world.characters[1].health = 50
    161 			case "loading":
    162 				client.loading = true
    163 			case "unready":
    164 				client.ready = false
    165 			case "replaced":
    166 				world.clients[1] = &worldClient{id: 1}
    167 			case "wrong owner":
    168 				binary.BigEndian.PutUint32(body[8:12], 2)
    169 			case "wrong type":
    170 				binary.BigEndian.PutUint32(body[4:8], 51050)
    171 			}
    172 
    173 			if err := world.handleResurrection(client, body); err != nil {
    174 				t.Fatal(err)
    175 			}
    176 
    177 			if len(conn.packets) != 0 || world.characters[1].playfield != 950 || client != world.clients[1] && scenario != "replaced" {
    178 				t.Fatal("invalid request changed the character or session")
    179 			}
    180 		})
    181 	}
    182 }
    183 
    184 func TestResurrectionSaveFailureLeavesDeadCharacterAndSessionUnchanged(t *testing.T) {
    185 	t.Chdir(t.TempDir())
    186 	world, conn := newCombatWorld(t)
    187 	loadResurrectionLocations(t, world)
    188 	failDatabaseWrites(t, world)
    189 	world.characters[1].health, world.characters[1].level = 0, 15
    190 	client := world.clients[1]
    191 
    192 	err := world.handleResurrection(client, resurrectionRequest(1)[20:])
    193 
    194 	if err == nil || len(conn.packets) != 0 || world.characters[1].health != 0 || world.characters[1].resurrectionShock != 0 || world.characters[1].playfield != 950 || world.clients[1] != client || !client.ready {
    195 		t.Fatal("failed save partially resurrected the player")
    196 	}
    197 }