server/player_interest_test.go (6268B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "testing" 6 "time" 7 ) 8 9 func joinInterestPlayer(t *testing.T, world *server, id uint32, x float32) (*worldClient, *combatInterestConn) { 10 t.Helper() 11 player := world.characters[id] 12 if player == nil { 13 player = world.newCharacter() 14 if player.id != id { 15 t.Fatal("unexpected fixture identity", player.id) 16 } 17 } 18 player.playfield, player.position, player.rotation = 950, [3]float32{x, 14, 56}, [4]float32{0, 0, 0, 1} 19 if err := world.saveUser(player); err != nil { 20 t.Fatal(err) 21 } 22 output := &combatInterestConn{} 23 client := &worldClient{conn: output, loading: true} 24 if err := world.joinWorldClient(client, id); err != nil { 25 t.Fatal(err) 26 } 27 if err := world.handleCharacterReady(client); err != nil { 28 t.Fatal(err) 29 } 30 return client, output 31 } 32 33 func actorPacketCount(output *combatInterestConn, opcode, actor uint32) int { 34 count := 0 35 for _, packet := range output.packets { 36 if binary.BigEndian.Uint16(packet[2:4]) == 10 && binary.BigEndian.Uint32(packet[20:24]) == opcode && binary.BigEndian.Uint32(packet[28:32]) == actor { 37 count++ 38 } 39 } 40 return count 41 } 42 43 func TestPlayerInterestBoundaryAndDistantBroadcasts(t *testing.T) { 44 world := newCheckpointWorld(t) 45 first, output := joinInterestPlayer(t, world, 1, 500) 46 second, _ := joinInterestPlayer(t, world, 2, 681) 47 if first.players[2] || second.players[1] || actorPacketCount(output, 0x271b3a6b, 2) != 0 { 48 t.Fatal("distant player was included in login") 49 } 50 output.packets = nil 51 52 world.characters[2].position[0] = 680 53 world.advanceActions(time.Now()) 54 if !first.players[2] || !second.players[1] || actorPacketCount(output, 0x271b3a6b, 2) != 1 { 55 t.Fatal("player did not enter at 180 metres") 56 } 57 world.characters[2].position[0] = 700 58 world.advanceActions(time.Now()) 59 if !first.players[2] || actorPacketCount(output, 0x36510078, 2) != 0 { 60 t.Fatal("boundary margin removed a visible player") 61 } 62 world.characters[2].position[0] = 701 63 world.advanceActions(time.Now()) 64 if first.players[2] || second.players[1] || actorPacketCount(output, 0x36510078, 2) != 1 { 65 t.Fatal("player did not leave beyond 200 metres") 66 } 67 output.packets = nil 68 if err := world.handlePosture(second, characterActionPacket(2, [5]uint32{86})[20:]); err != nil { 69 t.Fatal(err) 70 } 71 second.regenerationAt = time.Now().Add(-2 * time.Second) 72 world.advanceRegeneration(time.Now()) 73 if actorPacketCount(output, 0x5e477770, 2) != 0 || actorPacketCount(output, 0x3710256c, 2) != 0 { 74 t.Fatal("distant posture or health reached an observer", output.packets) 75 } 76 77 world.characters[2].position[0] = 680 78 world.advanceActions(time.Now()) 79 if actorPacketCount(output, 0x271b3a6b, 2) != 1 || actorPacketCount(output, 0x570c2039, 2) != 1 { 80 t.Fatal("returning player did not receive a complete fresh actor") 81 } 82 world.leaveWorldClient(second) 83 if first.players[2] || actorPacketCount(output, 0x36510078, 2) != 1 { 84 t.Fatal("departure retained a visible actor") 85 } 86 } 87 88 func TestPlayerInterestReplaysFollowAfterBothSnapshots(t *testing.T) { 89 world := newCheckpointWorld(t) 90 follower, _ := joinInterestPlayer(t, world, 1, 590) 91 _, _ = joinInterestPlayer(t, world, 2, 670) 92 observer, output := joinInterestPlayer(t, world, 3, 500) 93 follower.target = true 94 if err := world.handlePlayerFollow(follower, playerFollowRequest(2)); err != nil { 95 t.Fatal(err) 96 } 97 output.packets = nil 98 99 world.characters[2].position[0] = 701 100 world.advanceActions(time.Now()) 101 if observer.players[2] || !follower.players[2] || world.characters[1].followTarget != 2 { 102 t.Fatal("observer visibility changed the owner's follow") 103 } 104 stop, removed := -1, -1 105 for index, packet := range output.packets { 106 if binary.BigEndian.Uint32(packet[20:24]) == 0x260f3671 && binary.BigEndian.Uint32(packet[28:32]) == 1 && binary.BigEndian.Uint32(packet[40:44]) == 0 { 107 stop = index 108 } 109 if binary.BigEndian.Uint32(packet[20:24]) == 0x36510078 && binary.BigEndian.Uint32(packet[28:32]) == 2 { 110 removed = index 111 } 112 } 113 if stop < 0 || removed <= stop { 114 t.Fatal("follow target disappeared before its observer controller stopped", stop, removed) 115 } 116 output.packets = nil 117 world.characters[2].position[0] = 680 118 world.advanceActions(time.Now()) 119 spawn, follow := -1, -1 120 for index, packet := range output.packets { 121 if binary.BigEndian.Uint32(packet[20:24]) == 0x271b3a6b && binary.BigEndian.Uint32(packet[28:32]) == 2 { 122 spawn = index 123 } 124 if binary.BigEndian.Uint32(packet[20:24]) == 0x260f3671 && binary.BigEndian.Uint32(packet[28:32]) == 1 && binary.BigEndian.Uint32(packet[40:44]) == 2 { 125 follow = index 126 } 127 } 128 if spawn < 0 || follow <= spawn { 129 t.Fatal("follow did not resume after its target snapshot", spawn, follow) 130 } 131 } 132 133 func TestPlayerInterestPetFollowWaitsForActorSnapshots(t *testing.T) { 134 for _, reentry := range []bool{false, true} { 135 t.Run(map[bool]string{false: "login", true: "reentry"}[reentry], func(t *testing.T) { 136 world, _ := newPetSummonWorld(t) 137 client := world.clients[1] 138 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]); err != nil { 139 t.Fatal(err) 140 } 141 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 142 if err := world.handlePetCommand(client, petFollowRequest(1)[20:]); err != nil { 143 t.Fatal(err) 144 } 145 world.nextCharacter = 1 146 observer, output := joinInterestPlayer(t, world, 2, 50) 147 if reentry { 148 world.characters[1].position[0] = 251 149 world.advanceActions(time.Now()) 150 if observer.players[1] || !observer.npcs[1000001] { 151 t.Fatal("fixture did not retain the pet while its owner left") 152 } 153 output.packets = nil 154 world.characters[1].position[0] = 0 155 world.advanceActions(time.Now()) 156 } 157 158 ownerSeen, petSeen, followed := false, reentry, 0 159 for _, packet := range output.packets { 160 key, id := binary.BigEndian.Uint32(packet[20:24]), binary.BigEndian.Uint32(packet[28:32]) 161 if key == 0x271b3a6b { 162 ownerSeen = ownerSeen || id == 1 163 petSeen = petSeen || id == 1000001 164 } 165 if key == 0x260f3671 && id == 1000001 { 166 followed++ 167 if !ownerSeen || !petSeen { 168 t.Fatal("pet controller preceded a required actor snapshot") 169 } 170 } 171 } 172 if followed != 1 { 173 t.Fatal("pet controller was omitted or replayed twice", followed) 174 } 175 }) 176 } 177 }