*/ public function broadcastOn(): array { return [new PresenceChannel('room.'.$this->race->room_id)]; } /** * 广播事件名(前端监听 .horse.settled)。 */ public function broadcastAs(): string { return 'horse.settled'; } /** * 广播数据。 * * @return array */ public function broadcastWith(): array { $config = GameConfig::forGame('horse_racing')?->params ?? []; $houseTake = (int) ($config['house_take_percent'] ?? 5); $seedPool = (int) ($config['seed_pool'] ?? 0); // 统计各马匹总下注,为前端还原个人分奖金额提供基础参数。 $horsePools = HorseBet::query() ->where('race_id', $this->race->id) ->groupBy('horse_id') ->selectRaw('horse_id, SUM(amount) as pool') ->pluck('pool', 'horse_id') ->map(fn ($pool) => (int) $pool) ->toArray(); $winnerPool = (int) ($horsePools[$this->race->winner_horse_id] ?? 0); $distributablePool = (int) round( HorseRace::calcDistributablePool($horsePools, $houseTake, $seedPool, $winnerPool) ); // 找出获胜马匹的名称 $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, 'room_id' => (int) $this->race->room_id, 'winner_horse_id' => $this->race->winner_horse_id, 'winner_name' => $winnerName, 'total_pool' => (int) $this->race->total_pool, 'winner_pool' => $winnerPool, 'distributable_pool' => $distributablePool, 'settled_at' => $this->race->settled_at?->toIso8601String(), ]; } }