68 lines
1.6 KiB
PHP
68 lines
1.6 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 文件功能:五子棋对战邀请广播事件
|
|||
|
|
*
|
|||
|
|
* 玩家发起对战邀请时广播至房间 Presence 频道,
|
|||
|
|
* 前端在聊天消息流中渲染「接受挑战」按钮。
|
|||
|
|
*
|
|||
|
|
* @author ChatRoom Laravel
|
|||
|
|
*
|
|||
|
|
* @version 1.0.0
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
namespace App\Events;
|
|||
|
|
|
|||
|
|
use App\Models\GomokuGame;
|
|||
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|||
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
|||
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|||
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|||
|
|
use Illuminate\Queue\SerializesModels;
|
|||
|
|
|
|||
|
|
class GomokuInviteEvent implements ShouldBroadcastNow
|
|||
|
|
{
|
|||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @param GomokuGame $game 对局记录
|
|||
|
|
* @param string $inviterName 发起者用户名
|
|||
|
|
*/
|
|||
|
|
public function __construct(
|
|||
|
|
public readonly GomokuGame $game,
|
|||
|
|
public readonly string $inviterName,
|
|||
|
|
) {}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播至对应房间频道。
|
|||
|
|
*
|
|||
|
|
* @return array<\Illuminate\Broadcasting\Channel>
|
|||
|
|
*/
|
|||
|
|
public function broadcastOn(): array
|
|||
|
|
{
|
|||
|
|
return [new PresenceChannel("room.{$this->game->room_id}")];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播事件名(前端监听 .gomoku.invite)。
|
|||
|
|
*/
|
|||
|
|
public function broadcastAs(): string
|
|||
|
|
{
|
|||
|
|
return 'gomoku.invite';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播数据。
|
|||
|
|
*
|
|||
|
|
* @return array<string, mixed>
|
|||
|
|
*/
|
|||
|
|
public function broadcastWith(): array
|
|||
|
|
{
|
|||
|
|
return [
|
|||
|
|
'game_id' => $this->game->id,
|
|||
|
|
'inviter_name' => $this->inviterName,
|
|||
|
|
'expires_at' => $this->game->invite_expires_at?->toIso8601String(),
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
}
|