mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 19:52:09 +00:00
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Enums\RoomStateChange;
|
|
use Illuminate\Broadcasting\Channel;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class RoomStateChanged implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* @param array<string, mixed>|null $state
|
|
*/
|
|
public function __construct(
|
|
public readonly string $roomId,
|
|
public readonly int $version,
|
|
public readonly RoomStateChange $change,
|
|
public readonly ?array $state = null,
|
|
public readonly ?int $affectedPlayerId = null,
|
|
) {}
|
|
|
|
public function broadcastOn(): Channel
|
|
{
|
|
return new Channel("rooms.{$this->roomId}");
|
|
}
|
|
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'room.state.changed';
|
|
}
|
|
|
|
/**
|
|
* @return array{version: int, change: string, state: array<string, mixed>|null, affected_player_id?: int}
|
|
*/
|
|
public function broadcastWith(): array
|
|
{
|
|
$payload = [
|
|
'version' => $this->version,
|
|
'change' => $this->change->value,
|
|
'state' => $this->state,
|
|
];
|
|
|
|
if ($this->affectedPlayerId !== null) {
|
|
$payload['affected_player_id'] = $this->affectedPlayerId;
|
|
}
|
|
|
|
return $payload;
|
|
}
|
|
}
|