五子棋胜利发送系统公告,金币流水增加前缀
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据对局模式和获胜方计算奖励金币。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user