78 lines
2.0 KiB
PHP
78 lines
2.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:赛马结算广播事件
|
|
*
|
|
* 跑马结束後广播赛果(获胜马匹、赔付金额等)给房间所有用户,
|
|
* 前端收到后展示结算面板并更新中奖信息。
|
|
*
|
|
* @author ChatRoom Laravel
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
namespace App\Events;
|
|
|
|
use App\Models\HorseRace;
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class HorseRaceSettled implements ShouldBroadcastNow
|
|
{
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
/**
|
|
* @param HorseRace $race 已结算的场次
|
|
*/
|
|
public function __construct(
|
|
public readonly HorseRace $race,
|
|
) {}
|
|
|
|
/**
|
|
* 广播至房间公共频道。
|
|
*
|
|
* @return array<\Illuminate\Broadcasting\Channel>
|
|
*/
|
|
public function broadcastOn(): array
|
|
{
|
|
return [new PresenceChannel('room.1')];
|
|
}
|
|
|
|
/**
|
|
* 广播事件名(前端监听 .horse.settled)。
|
|
*/
|
|
public function broadcastAs(): string
|
|
{
|
|
return 'horse.settled';
|
|
}
|
|
|
|
/**
|
|
* 广播数据。
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function broadcastWith(): array
|
|
{
|
|
// 找出获胜马匹的名称
|
|
$horses = $this->race->horses ?? [];
|
|
$winnerName = '未知';
|
|
foreach ($horses as $horse) {
|
|
if (($horse['id'] ?? 0) === $this->race->winner_horse_id) {
|
|
$winnerName = ($horse['emoji'] ?? '').' '.($horse['name'] ?? '');
|
|
break;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'race_id' => $this->race->id,
|
|
'winner_horse_id' => $this->race->winner_horse_id,
|
|
'winner_name' => $winnerName,
|
|
'total_pool' => (int) $this->race->total_pool,
|
|
'settled_at' => $this->race->settled_at?->toIso8601String(),
|
|
];
|
|
}
|
|
}
|