72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:赛马进行中实时进度广播事件
|
||
|
|
*
|
||
|
|
* 跑马过程中每隔1秒广播各马匹当前进度(0~100%),
|
||
|
|
* 前端据此实时更新赛道动画。
|
||
|
|
*
|
||
|
|
* @author ChatRoom Laravel
|
||
|
|
*
|
||
|
|
* @version 1.0.0
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace App\Events;
|
||
|
|
|
||
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
||
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
||
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
||
|
|
use Illuminate\Queue\SerializesModels;
|
||
|
|
|
||
|
|
class HorseRaceProgress implements ShouldBroadcastNow
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param int $raceId 场次 ID
|
||
|
|
* @param array<int, int> $positions 各马匹进度 [horse_id => progress(0~100)]
|
||
|
|
* @param bool $finished 是否已到终点
|
||
|
|
* @param int|null $leaderId 当前领跑马匹 ID
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly int $raceId,
|
||
|
|
public readonly array $positions,
|
||
|
|
public readonly bool $finished = false,
|
||
|
|
public readonly ?int $leaderId = null,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 广播至房间公共频道。
|
||
|
|
*
|
||
|
|
* @return array<\Illuminate\Broadcasting\Channel>
|
||
|
|
*/
|
||
|
|
public function broadcastOn(): array
|
||
|
|
{
|
||
|
|
return [new PresenceChannel('room.1')];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 广播事件名(前端监听 .horse.progress)。
|
||
|
|
*/
|
||
|
|
public function broadcastAs(): string
|
||
|
|
{
|
||
|
|
return 'horse.progress';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 广播数据。
|
||
|
|
*
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
public function broadcastWith(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'race_id' => $this->raceId,
|
||
|
|
'positions' => $this->positions,
|
||
|
|
'finished' => $this->finished,
|
||
|
|
'leader_id' => $this->leaderId,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|