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(),