feat(baccarat): 实现百家乐实时下注人数统计功能

- 新增 BaccaratPoolUpdated 事件,用于通过 WebSocket 广播实时下注数据更新
- 增加数据库迁移以在 baccarat_rounds 表中添加对应的下注人数统计字段
- 更新 BaccaratRound 模型以及 BaccaratController,支持实时下注统计更新与 WebSocket 事件分发
- 更新前端 chat.js 以及 baccarat-panel.blade.php,利用 Alpine.js 和 Echo 接收事件并动态渲染 "大"、"小"、"豹子" 的实时下注计数
This commit is contained in:
2026-03-28 17:02:10 +08:00
parent a68e82107e
commit 8fcccf72a5
6 changed files with 174 additions and 11 deletions
+66
View File
@@ -0,0 +1,66 @@
<?php
/**
* 文件功能:百家乐押注人数实时广播事件
*
* 当有用户成功下注时,向房间内所有用户广播最新的
* 各选项下注总人次,供前端实时更新面板。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Events;
use App\Models\BaccaratRound;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class BaccaratPoolUpdated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* @param BaccaratRound $round 本局信息
*/
public function __construct(
public readonly BaccaratRound $round,
) {}
/**
* 广播至房间公共频道。
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [new PresenceChannel('room.1')];
}
/**
* 广播事件名(前端监听 .baccarat.pool_updated)。
*/
public function broadcastAs(): string
{
return 'baccarat.pool_updated';
}
/**
* 广播数据。
*
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return [
'round_id' => $this->round->id,
'bet_count_big' => $this->round->bet_count_big,
'bet_count_small' => $this->round->bet_count_small,
'bet_count_triple' => $this->round->bet_count_triple,
];
}
}
@@ -56,6 +56,9 @@ class BaccaratController extends Controller
'total_bet_big' => $round->total_bet_big,
'total_bet_small' => $round->total_bet_small,
'total_bet_triple' => $round->total_bet_triple,
'bet_count_big' => $round->bet_count_big,
'bet_count_small' => $round->bet_count_small,
'bet_count_triple' => $round->bet_count_triple,
'my_bet' => $myBet ? [
'bet_type' => $myBet->bet_type,
'amount' => $myBet->amount,
@@ -139,9 +142,14 @@ class BaccaratController extends Controller
// 更新局次汇总统计
$field = 'total_bet_'.$data['bet_type'];
$countField = 'bet_count_'.$data['bet_type'];
$round->increment($field, $data['amount']);
$round->increment($countField);
$round->increment('bet_count');
// 广播各选项的最新押注人数
event(new \App\Events\BaccaratPoolUpdated($round));
$betLabel = match ($data['bet_type']) {
'big' => '大', 'small' => '小', default => '豹子'
};
+4
View File
@@ -24,6 +24,7 @@ class BaccaratRound extends Model
'bet_opens_at', 'bet_closes_at', 'settled_at',
'total_bet_big', 'total_bet_small', 'total_bet_triple',
'total_payout', 'bet_count',
'bet_count_big', 'bet_count_small', 'bet_count_triple',
];
/**
@@ -44,6 +45,9 @@ class BaccaratRound extends Model
'total_bet_triple' => 'integer',
'total_payout' => 'integer',
'bet_count' => 'integer',
'bet_count_big' => 'integer',
'bet_count_small' => 'integer',
'bet_count_triple' => 'integer',
];
}