feat(chat): 完善五子棋功能,包含AI对战、PvP邀请、断线重连及界面美化
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:五子棋对局结束广播事件
|
||||
*
|
||||
* 对局结束(胜负/平局/认输/超时)时广播两个频道:
|
||||
* 1. 私有对局频道:通知双方结算并关闭棋盘
|
||||
* 2. 房间公共频道:广播战报消息
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\GomokuGame;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class GomokuFinishedEvent implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* @param GomokuGame $game 当前对局
|
||||
* @param string $winnerName 胜者用户名(平局时为空字符串)
|
||||
* @param string $loserName 败者用户名(平局时为空字符串)
|
||||
* @param string $reason 结束原因:win | draw | resign | timeout
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly GomokuGame $game,
|
||||
public readonly string $winnerName,
|
||||
public readonly string $loserName,
|
||||
public readonly string $reason,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 同时广播至对局私有频道 + 房间公共频道。
|
||||
*
|
||||
* @return array<\Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [
|
||||
new PrivateChannel("gomoku.{$this->game->id}"),
|
||||
new PresenceChannel("room.{$this->game->room_id}"),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播事件名(前端监听 .gomoku.finished)。
|
||||
*/
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'gomoku.finished';
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播数据。
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'game_id' => $this->game->id,
|
||||
'winner' => $this->game->winner,
|
||||
'winner_name' => $this->winnerName,
|
||||
'loser_name' => $this->loserName,
|
||||
'reason' => $this->reason,
|
||||
'reward_gold' => $this->game->reward_gold,
|
||||
'mode' => $this->game->mode,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:五子棋落子广播事件
|
||||
*
|
||||
* 每次玩家(或 AI)落子后通过私有对局频道广播,
|
||||
* 双方前端实时更新棋盘显示并切换行棋方。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\GomokuGame;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class GomokuMovedEvent implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* @param GomokuGame $game 当前对局
|
||||
* @param int $row 落子行(0-14)
|
||||
* @param int $col 落子列(0-14)
|
||||
* @param int $color 落子颜色(1=黑 2=白)
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly GomokuGame $game,
|
||||
public readonly int $row,
|
||||
public readonly int $col,
|
||||
public readonly int $color,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 广播至对局私有频道(仅双方可见)。
|
||||
*
|
||||
* @return array<\Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel("gomoku.{$this->game->id}")];
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播事件名(前端监听 .gomoku.moved)。
|
||||
*/
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'gomoku.moved';
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播数据。
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'game_id' => $this->game->id,
|
||||
'row' => $this->row,
|
||||
'col' => $this->col,
|
||||
'color' => $this->color,
|
||||
'current_turn' => $this->game->current_turn,
|
||||
'board' => $this->game->board,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user