65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Game\Room;
|
|
use Illuminate\Broadcasting\Channel;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
final class GameSnapshot implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithSockets;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* @param array<string, mixed> $players
|
|
* @param array<int, mixed> $entities
|
|
* @param array<int, mixed> $events
|
|
*/
|
|
public function __construct(
|
|
public Room $room,
|
|
public array $players,
|
|
public array $entities,
|
|
public array $events,
|
|
) {}
|
|
|
|
public function broadcastOn(): Channel
|
|
{
|
|
return new Channel('game.'.$this->room->id);
|
|
}
|
|
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'snapshot';
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function broadcastWith(): array
|
|
{
|
|
return [
|
|
'state' => $this->room->state,
|
|
'elapsed' => round($this->room->elapsed, 2),
|
|
'seq' => $this->room->seq,
|
|
'players' => $this->players,
|
|
'entities' => $this->entities,
|
|
'events' => $this->events,
|
|
'msgCap' => $this->messageSizeCap(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Largest snapshot the WebSocket layer will deliver in one frame; used by
|
|
* the client debug HUD to show broadcast headroom against the Reverb cap.
|
|
*/
|
|
private function messageSizeCap(): int
|
|
{
|
|
return (int) config('reverb.apps.apps.0.max_message_size', 20_000);
|
|
}
|
|
}
|