Skip to content

Commit

Permalink
Split TypeConverter broadcasting into multiple variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Jul 22, 2023
1 parent 2005f9a commit b84ca69
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/world/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,15 @@ class World implements ChunkManager{
private array $playerChunkListeners = [];

/**
* @var ClientboundPacket[][] chunkHash => [typeConverterHash => ClientboundPacket[]]
* @phpstan-var array<ChunkPosHash, array<TypeConverterHash, list<ClientboundPacket>>>
* @var ClientboundPacket[][] chunkHash => ClientboundPacket[]
* @phpstan-var array<ChunkPosHash, list<ClientboundPacket>>
*/
private array $packetBuffersByChunk = [];
/**
* @var \Closure[][] chunkHash => Closure[]
* @phpstan-var array<ChunkPosHash, list<\Closure(TypeConverter) : ClientboundPacket[]>>
*/
private array $packetBuffersByChunkTypeConverter = [];

/**
* @var float[] chunkHash => timestamp of request
Expand Down Expand Up @@ -835,25 +840,18 @@ public function broadcastPacketToViewersByTypeConverter(Vector3 $pos, \Closure $
new ParameterType('typeConverter', TypeConverter::class),
), $closure);

[$typeConverters, ] = TypeConverter::sortByConverter($this->getViewersForPosition($pos));

foreach($typeConverters as $typeConverter){
/** @var ClientboundPacket[] $packets */
$packets = $closure($typeConverter);
foreach($packets as $packet){
$this->broadcastPacketToPlayersUsingChunk($pos->getFloorX() >> Chunk::COORD_BIT_SIZE, $pos->getFloorZ() >> Chunk::COORD_BIT_SIZE, $packet, $typeConverter);
}
if(!isset($this->packetBuffersByChunkTypeConverter[$index = World::chunkHash($pos->getFloorX() >> Chunk::COORD_BIT_SIZE, $pos->getFloorZ() >> Chunk::COORD_BIT_SIZE)])){
$this->packetBuffersByChunkTypeConverter[$index] = [$closure];
}else{
$this->packetBuffersByChunkTypeConverter[$index][] = $closure;
}
}

private function broadcastPacketToPlayersUsingChunk(int $chunkX, int $chunkZ, ClientboundPacket $packet, ?TypeConverter $typeConverter = null) : void{
/** @phpstan-var int|null $typeConverterId */
$typeConverterId = $typeConverter === null ? null : spl_object_id($typeConverter);

if(!isset($this->packetBuffersByChunk[$index = World::chunkHash($chunkX, $chunkZ)][$typeConverterId])){
$this->packetBuffersByChunk[$index][$typeConverterId] = [$packet];
private function broadcastPacketToPlayersUsingChunk(int $chunkX, int $chunkZ, ClientboundPacket $packet) : void{
if(!isset($this->packetBuffersByChunk[$index = World::chunkHash($chunkX, $chunkZ)])){
$this->packetBuffersByChunk[$index] = [$packet];
}else{
$this->packetBuffersByChunk[$index][$typeConverterId][] = $packet;
$this->packetBuffersByChunk[$index][] = $packet;
}
}

Expand Down Expand Up @@ -1093,21 +1091,31 @@ protected function actuallyDoTick(int $currentTick) : void{
$this->checkSleep();
}

foreach($this->packetBuffersByChunk as $index => $entries){
foreach($this->packetBuffersByChunkTypeConverter as $index => $entries){
World::getXZ($index, $chunkX, $chunkZ);
$universalPackets = $entries[null] ?? [];
[$typeConverters, $converterRecipients] = TypeConverter::sortByConverter($this->getChunkPlayers($chunkX, $chunkZ));

foreach($typeConverters as $key => $typeConverter){
$packets = ($entries[$key] ?? []) + $universalPackets;
$packets = (array_map(fn(\Closure $closure) => $closure($typeConverter), $entries[$key] ?? [])) + ($this->packetBuffersByChunk[$index] ?? []);

Check failure on line 1099 in src/world/World.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Parameter #2 $array of function array_map expects array, array|(Closure) given.

Check failure on line 1099 in src/world/World.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Parameter #2 $array of function array_map expects array, array|(Closure) given.

if(count($packets) > 0){
NetworkBroadcastUtils::broadcastPackets($converterRecipients[$key], $packets);
}
}

unset($this->packetBuffersByChunk[$index]);
}

foreach($this->packetBuffersByChunk as $index => $entries){
World::getXZ($index, $chunkX, $chunkZ);
$chunkPlayers = $this->getChunkPlayers($chunkX, $chunkZ);
if(count($chunkPlayers) > 0){
NetworkBroadcastUtils::broadcastPackets($chunkPlayers, $entries);
}
}

$this->packetBuffersByChunk = [];
$this->packetBuffersByChunkTypeConverter = [];
}

public function checkSleep() : void{
Expand Down

0 comments on commit b84ca69

Please sign in to comment.