From dfa72781845b1b2cceddc9911fb17845c4c75b46 Mon Sep 17 00:00:00 2001 From: lkddi Date: Sun, 1 Mar 2026 20:54:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=98=9F=E6=B5=B7=E5=B0=8F?= =?UTF-8?q?=E5=8D=9A=E5=A3=AB=E9=9A=8F=E6=9C=BA=E4=BA=8B=E4=BB=B6=E9=87=91?= =?UTF-8?q?=E5=B8=81/=E7=BB=8F=E9=AA=8C=E4=B8=8D=E8=AE=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=EF=BC=9A=E6=94=B9=E8=B5=B0UserCurrencyService.change(?= =?UTF-8?q?)=EF=BC=8C=E6=96=B0=E5=A2=9ECurrencySource::AUTO=5FEVENT?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Enums/CurrencySource.php | 4 ++++ app/Http/Controllers/ChatController.php | 30 ++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/Enums/CurrencySource.php b/app/Enums/CurrencySource.php index 5ff6a2c..b841c71 100644 --- a/app/Enums/CurrencySource.php +++ b/app/Enums/CurrencySource.php @@ -78,6 +78,9 @@ enum CurrencySource: string /** 百家乐中奖赔付(收入金币,含本金返还) */ case BACCARAT_WIN = 'baccarat_win'; + /** 星海小博士随机事件(好运/坏运/经验/金币奖惩) */ + case AUTO_EVENT = 'auto_event'; + /** * 返回该来源的中文名称,用于后台统计展示。 */ @@ -103,6 +106,7 @@ enum CurrencySource: string self::HOLIDAY_BONUS => '节日福利', self::BACCARAT_BET => '百家乐下注', self::BACCARAT_WIN => '百家乐赢钱', + self::AUTO_EVENT => '随机事件(星海小博士)', }; } } diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index afe578d..173d232 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -469,14 +469,32 @@ class ChatController extends Controller if ($eventChance > 0 && rand(1, 100) <= $eventChance) { $autoEvent = Autoact::randomEvent(); if ($autoEvent) { - // 应用经验/金币变化(不低于 0) + // 经验变化:通过 UserCurrencyService 写日志 if ($autoEvent->exp_change !== 0) { - $user->exp_num = max(0, $user->exp_num + $autoEvent->exp_change); + $this->currencyService->change( + $user, + 'exp', + $autoEvent->exp_change, + CurrencySource::AUTO_EVENT, + "随机事件:{$autoEvent->text_body}", + $id, + ); } + + // 金币变化:通过 UserCurrencyService 写日志 if ($autoEvent->jjb_change !== 0) { - $user->jjb = max(0, ($user->jjb ?? 0) + $autoEvent->jjb_change); + $this->currencyService->change( + $user, + 'gold', + $autoEvent->jjb_change, + CurrencySource::AUTO_EVENT, + "随机事件:{$autoEvent->text_body}", + $id, + ); } - $user->save(); + + // 重新从数据库读取最新属性(service 已原子更新,需刷新本地对象) + $user->refresh(); // 重新计算等级(经验可能因事件而变化,但管理员不参与自动升降级) if ($user->user_level < $superLevel) { @@ -497,8 +515,8 @@ class ChatController extends Controller 'is_secret' => false, 'font_color' => match ($autoEvent->event_type) { 'good' => '#16a34a', // 绿色(好运) - 'bad' => '#dc2626', // 红色(坏运) - default => '#7c3aed', // 紫色(中性) + 'bad' => '#dc2626', // 红色(坏运) + default => '#7c3aed', // 紫色(中性) }, 'action' => '', 'sent_at' => now()->toDateTimeString(),