From 106dc7f852a741e4d6b3e05df1f448ca8b118f27 Mon Sep 17 00:00:00 2001 From: lkddi Date: Thu, 12 Mar 2026 15:59:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=94=E5=AD=90=E6=A3=8B=E8=83=9C=E5=88=A9?= =?UTF-8?q?=E5=8F=91=E9=80=81=E7=B3=BB=E7=BB=9F=E5=85=AC=E5=91=8A=EF=BC=8C?= =?UTF-8?q?=E9=87=91=E5=B8=81=E6=B5=81=E6=B0=B4=E5=A2=9E=E5=8A=A0=E5=89=8D?= =?UTF-8?q?=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/GomokuController.php | 46 +++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/GomokuController.php b/app/Http/Controllers/GomokuController.php index 499f7f6..fba2dce 100644 --- a/app/Http/Controllers/GomokuController.php +++ b/app/Http/Controllers/GomokuController.php @@ -404,6 +404,13 @@ class GomokuController extends Controller $winnerName = $winnerUser?->username ?? ''; $loserName = $loserUser?->username ?? ''; + // 将英文 reason 转为友好的中文后缀 + $reasonText = match ($reason) { + 'resign' => '(认输)', + 'timeout' => '(超时)', + default => '', + }; + // 发放 PvP 胜利奖励给获胜玩家 if ($winnerUser && $rewardGold > 0) { $this->currency->change( @@ -411,7 +418,7 @@ class GomokuController extends Controller 'gold', $rewardGold, CurrencySource::GOMOKU_WIN, - "五子棋对战击败 {$loserName}", + "五子棋:击败 {$loserName}{$reasonText}", ); } } else { @@ -426,7 +433,7 @@ class GomokuController extends Controller 'gold', $rewardGold, CurrencySource::GOMOKU_WIN, - "五子棋击败 AI(难度{$game->ai_level})", + "五子棋:击败 AI(难度{$game->ai_level})", ); } } else { @@ -446,9 +453,16 @@ class GomokuController extends Controller 'finished_at' => now(), ]); - // 广播对局结束事件 + // 广播对局结束事件给参与对局的双方 broadcast(new GomokuFinishedEvent($game->fresh(), $winnerName, $loserName, $reason)); + // 有胜负,并且非 AI 获胜,发送系统房间广播 + if ($winnerColor !== 0 && $winnerName && $winnerName !== "AI(难度{$game->ai_level})") { + $msgType = $game->mode === 'pvp' ? '玩家对战' : "人机对战(难度{$game->ai_level})"; + $text = "🎉 棋神降临!恭喜玩家【{$winnerName}】在五子棋{$msgType}中击败了【{$loserName}】,赢得了 {$rewardGold} 金币奖励!"; + $this->broadcastSystemMessage($game->room_id, $text); + } + return response()->json([ 'ok' => true, 'finished' => true, @@ -459,6 +473,32 @@ class GomokuController extends Controller ]); } + /** + * 发送系统房间广播。 + * + * @param int $roomId 房间ID + * @param string $content 广播内容 + */ + private function broadcastSystemMessage(int $roomId, string $content): void + { + $chatState = app(\App\Services\ChatStateService::class); + $messageData = [ + 'id' => $chatState->nextMessageId($roomId), + 'room_id' => $roomId, + 'from_user' => '系统广播', + 'to_user' => '大家', + 'content' => $content, + 'is_secret' => false, + 'font_color' => '#d97706', // 琥珀橙色 + 'action' => '大声宣告', + 'sent_at' => now()->toDateTimeString(), + ]; + + $chatState->pushMessage($roomId, $messageData); + broadcast(new \App\Events\MessageSent($roomId, $messageData)); + \App\Jobs\SaveMessageJob::dispatch($messageData); + } + /** * 根据对局模式和获胜方计算奖励金币。 *