diff --git a/app/Services/FishingService.php b/app/Services/FishingService.php index 7f3c933..97bf79d 100644 --- a/app/Services/FishingService.php +++ b/app/Services/FishingService.php @@ -5,9 +5,13 @@ * * 提取自 FishingController,供玩家请求和 AI 挂机脚本复用。 * - * @author ChatRoom Laravel + * 修改说明: + * - 保留 fishing_events.message 原始文案 + * - 如果会员有加成,则在原文案后追加: + * (会员加成:+经验X,+金币Y) * - * @version 1.0.0 + * @author ChatRoom Laravel + * @version 1.2.0 */ namespace App\Services; @@ -38,29 +42,72 @@ class FishingService // 1. 随机决定钓鱼结果 $result = $this->randomFishResult(); - // 2. 更新经验和金币 + // 2. 获取会员倍率 $expMul = $this->vipService->getExpMultiplier($user); $jjbMul = $this->vipService->getJjbMultiplier($user); + // 3. 计算最终经验和金币 + $finalExp = 0; + $finalJjb = 0; + if ($result['exp'] !== 0) { - $finalExp = $result['exp'] > 0 ? (int) round($result['exp'] * $expMul) : $result['exp']; - $this->currencyService->change( - $user, 'exp', $finalExp, CurrencySource::FISHING_GAIN, - "钓鱼收竿:{$result['message']}", $roomId, - ); + $finalExp = $result['exp'] > 0 + ? (int) round($result['exp'] * $expMul) + : $result['exp']; } if ($result['jjb'] !== 0) { - $finalJjb = $result['jjb'] > 0 ? (int) round($result['jjb'] * $jjbMul) : $result['jjb']; + $finalJjb = $result['jjb'] > 0 + ? (int) round($result['jjb'] * $jjbMul) + : $result['jjb']; + } + + // 4. 计算会员额外加成部分 + $bonusExp = 0; + $bonusJjb = 0; + + if ($result['exp'] > 0 && $finalExp > $result['exp']) { + $bonusExp = $finalExp - $result['exp']; + } + + if ($result['jjb'] > 0 && $finalJjb > $result['jjb']) { + $bonusJjb = $finalJjb - $result['jjb']; + } + + // 5. 生成最终展示文案 + $finalMessage = $this->buildFinalMessage( + $result['message'], + $bonusExp, + $bonusJjb + ); + + // 6. 更新经验 + if ($finalExp !== 0) { $this->currencyService->change( - $user, 'gold', $finalJjb, CurrencySource::FISHING_GAIN, - "钓鱼收竿:{$result['message']}", $roomId, + $user, + 'exp', + $finalExp, + CurrencySource::FISHING_GAIN, + "钓鱼收竿:{$finalMessage}", + $roomId, + ); + } + + // 7. 更新金币 + if ($finalJjb !== 0) { + $this->currencyService->change( + $user, + 'gold', + $finalJjb, + CurrencySource::FISHING_GAIN, + "钓鱼收竿:{$finalMessage}", + $roomId, ); } $user->refresh(); - // 3. 广播钓鱼结果到聊天室 + // 8. 广播钓鱼结果到聊天室 $promoTag = ''; if (! $isAi) { $autoFishingMinutesLeft = $this->shopService->getActiveAutoFishingMinutesLeft($user); @@ -77,9 +124,9 @@ class FishingService 'room_id' => $roomId, 'from_user' => '钓鱼播报', 'to_user' => '大家', - 'content' => "{$result['emoji']} 【{$user->username}】{$result['message']}{$promoTag}", + 'content' => "{$result['emoji']} 【{$user->username}】{$finalMessage}{$promoTag}", 'is_secret' => false, - 'font_color' => $result['exp'] >= 0 ? '#16a34a' : '#dc2626', + 'font_color' => ($result['exp'] < 0 || $result['jjb'] < 0) ? '#dc2626' : '#16a34a', 'action' => '', 'sent_at' => now()->toDateTimeString(), ]; @@ -89,7 +136,42 @@ class FishingService // 发送完需持久化,不过 controller 里并未直接看到 SaveMessageJob, 但 AIheartbeat 里有。 // 这里就先维持原样,只 broadcast 和 pushMessage - return $result; + return [ + 'emoji' => $result['emoji'], + 'message' => $finalMessage, + 'exp' => $finalExp, + 'jjb' => $finalJjb, + 'base_exp' => $result['exp'], + 'base_jjb' => $result['jjb'], + 'bonus_exp' => $bonusExp, + 'bonus_jjb' => $bonusJjb, + ]; + } + + /** + * 生成最终展示文案 + * + * 示例: + * - 钓到一条金鱼,获得经验10,金币20 + * - 钓到一条金鱼,获得经验10,金币20(会员加成:+经验10,+金币20) + */ + private function buildFinalMessage(string $baseMessage, int $bonusExp, int $bonusJjb): string + { + $bonusParts = []; + + if ($bonusExp > 0) { + $bonusParts[] = "+经验{$bonusExp}"; + } + + if ($bonusJjb > 0) { + $bonusParts[] = "+金币{$bonusJjb}"; + } + + if (empty($bonusParts)) { + return $baseMessage; + } + + return $baseMessage . '(会员加成:' . implode(',', $bonusParts) . ')'; } /** @@ -115,8 +197,8 @@ class FishingService return [ 'emoji' => $event->emoji, 'message' => $event->message, - 'exp' => $event->exp, - 'jjb' => $event->jjb, + 'exp' => (int) $event->exp, + 'jjb' => (int) $event->jjb, ]; } }