60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 文件功能:猜成语答题结果广播事件
|
||
|
|
*
|
||
|
|
* 用户答对成语时广播,通知房间内所有用户结果。
|
||
|
|
*
|
||
|
|
* @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 IdiomGameAnswered implements ShouldBroadcastNow
|
||
|
|
{
|
||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param int $roomId 房间 ID
|
||
|
|
* @param int $roundId 游戏回合 ID
|
||
|
|
* @param string $answer 正确答案
|
||
|
|
* @param string $winnerUsername 答对的用户名
|
||
|
|
* @param int $rewardGold 获得的金币
|
||
|
|
* @param int $rewardExp 获得的经验
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public readonly int $roomId,
|
||
|
|
public readonly int $roundId,
|
||
|
|
public readonly string $answer,
|
||
|
|
public readonly string $winnerUsername,
|
||
|
|
public readonly int $rewardGold = 0,
|
||
|
|
public readonly int $rewardExp = 0,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public function broadcastOn(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
new PresenceChannel('room.'.$this->roomId),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function broadcastWith(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'round_id' => $this->roundId,
|
||
|
|
'answer' => $this->answer,
|
||
|
|
'winner_username' => $this->winnerUsername,
|
||
|
|
'reward_gold' => $this->rewardGold,
|
||
|
|
'reward_exp' => $this->rewardExp,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|