2026-03-26 11:49:36 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:钓鱼核心业务逻辑
|
|
|
|
|
|
*
|
|
|
|
|
|
* 提取自 FishingController,供玩家请求和 AI 挂机脚本复用。
|
|
|
|
|
|
*
|
2026-04-11 12:56:22 +08:00
|
|
|
|
* 修改说明:
|
|
|
|
|
|
* - 保留 fishing_events.message 原始文案
|
|
|
|
|
|
* - 如果会员有加成,则在原文案后追加:
|
|
|
|
|
|
* (会员加成:+经验X,+金币Y)
|
2026-03-26 11:49:36 +08:00
|
|
|
|
*
|
2026-04-11 12:56:22 +08:00
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
* @version 1.2.0
|
2026-03-26 11:49:36 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Enums\CurrencySource;
|
|
|
|
|
|
use App\Events\MessageSent;
|
|
|
|
|
|
use App\Models\FishingEvent;
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
|
|
|
|
class FishingService
|
|
|
|
|
|
{
|
|
|
|
|
|
public function __construct(
|
2026-04-11 13:45:10 +08:00
|
|
|
|
private readonly ChatStateService $chatState,
|
|
|
|
|
|
private readonly VipService $vipService,
|
2026-03-26 11:49:36 +08:00
|
|
|
|
private readonly UserCurrencyService $currencyService,
|
2026-04-11 13:45:10 +08:00
|
|
|
|
private readonly ShopService $shopService,
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2026-03-26 11:49:36 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理收竿逻辑:计算结果、发放积分并全服广播。
|
|
|
|
|
|
*
|
2026-04-11 13:45:10 +08:00
|
|
|
|
* @param User $user 收竿的用户实体
|
|
|
|
|
|
* @param int $roomId 所在房间 ID
|
|
|
|
|
|
* @param bool $isAi 是否为 AI 调用(用于影响文案或标签)
|
2026-03-26 11:49:36 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function processCatch(User $user, int $roomId, bool $isAi = false): array
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. 随机决定钓鱼结果
|
|
|
|
|
|
$result = $this->randomFishResult();
|
|
|
|
|
|
|
2026-04-11 12:56:22 +08:00
|
|
|
|
// 2. 获取会员倍率
|
2026-03-26 11:49:36 +08:00
|
|
|
|
$expMul = $this->vipService->getExpMultiplier($user);
|
|
|
|
|
|
$jjbMul = $this->vipService->getJjbMultiplier($user);
|
|
|
|
|
|
|
2026-04-11 12:56:22 +08:00
|
|
|
|
// 3. 计算最终经验和金币
|
|
|
|
|
|
$finalExp = 0;
|
|
|
|
|
|
$finalJjb = 0;
|
|
|
|
|
|
|
2026-03-26 11:49:36 +08:00
|
|
|
|
if ($result['exp'] !== 0) {
|
2026-04-11 13:45:10 +08:00
|
|
|
|
// 当经验为 正数 则可使用会员翻倍,负数则不
|
|
|
|
|
|
$finalExp = $result['exp'] > 0 ? (int)round($result['exp'] * $expMul) : $result['exp'];
|
2026-04-11 12:56:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($result['jjb'] !== 0) {
|
2026-04-11 13:45:10 +08:00
|
|
|
|
$finalJjb = $result['jjb'] > 0 ? (int)round($result['jjb'] * $jjbMul) : $result['jjb'];
|
2026-04-11 12:56:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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. 生成最终展示文案
|
2026-04-11 14:13:52 +08:00
|
|
|
|
$finalMessage = $this->buildFinalMessage($result['message'], $bonusExp, $bonusJjb, $user);
|
2026-04-11 12:56:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 6. 更新经验
|
|
|
|
|
|
if ($finalExp !== 0) {
|
2026-04-11 13:45:10 +08:00
|
|
|
|
$this->currencyService->change($user, 'exp', $finalExp, CurrencySource::FISHING_GAIN, "钓鱼收竿:{$finalMessage}", $roomId,
|
2026-03-26 11:49:36 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 12:56:22 +08:00
|
|
|
|
// 7. 更新金币
|
|
|
|
|
|
if ($finalJjb !== 0) {
|
2026-04-11 13:45:10 +08:00
|
|
|
|
$this->currencyService->change($user, 'gold', $finalJjb, CurrencySource::FISHING_GAIN, "钓鱼收竿:{$finalMessage}", $roomId,
|
2026-03-26 11:49:36 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$user->refresh();
|
|
|
|
|
|
|
2026-04-11 12:56:22 +08:00
|
|
|
|
// 8. 广播钓鱼结果到聊天室
|
2026-03-26 11:49:36 +08:00
|
|
|
|
$promoTag = '';
|
2026-04-11 13:45:10 +08:00
|
|
|
|
if (!$isAi) {
|
2026-03-26 11:49:36 +08:00
|
|
|
|
$autoFishingMinutesLeft = $this->shopService->getActiveAutoFishingMinutesLeft($user);
|
|
|
|
|
|
$promoTag = $autoFishingMinutesLeft > 0
|
|
|
|
|
|
? ' <span onclick="window.openShopModal&&window.openShopModal()" '
|
2026-04-11 13:45:10 +08:00
|
|
|
|
. 'style="display:inline-block;margin-left:6px;padding:1px 7px;background:#e9e4f5;'
|
|
|
|
|
|
. 'color:#6d4fa8;border-radius:10px;font-size:10px;cursor:pointer;font-weight:bold;vertical-align:middle;'
|
|
|
|
|
|
. 'border:1px solid #d0c4ec;" title="点击购买自动钓鱼卡">🎣 自动钓鱼卡</span>'
|
2026-03-26 11:49:36 +08:00
|
|
|
|
: '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sysMsg = [
|
|
|
|
|
|
'id' => $this->chatState->nextMessageId($roomId),
|
|
|
|
|
|
'room_id' => $roomId,
|
|
|
|
|
|
'from_user' => '钓鱼播报',
|
|
|
|
|
|
'to_user' => '大家',
|
2026-04-11 12:56:22 +08:00
|
|
|
|
'content' => "{$result['emoji']} 【{$user->username}】{$finalMessage}{$promoTag}",
|
2026-03-26 11:49:36 +08:00
|
|
|
|
'is_secret' => false,
|
2026-04-11 12:56:22 +08:00
|
|
|
|
'font_color' => ($result['exp'] < 0 || $result['jjb'] < 0) ? '#dc2626' : '#16a34a',
|
2026-03-26 11:49:36 +08:00
|
|
|
|
'action' => '',
|
|
|
|
|
|
'sent_at' => now()->toDateTimeString(),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
$this->chatState->pushMessage($roomId, $sysMsg);
|
|
|
|
|
|
broadcast(new MessageSent($roomId, $sysMsg));
|
|
|
|
|
|
// 发送完需持久化,不过 controller 里并未直接看到 SaveMessageJob, 但 AIheartbeat 里有。
|
|
|
|
|
|
// 这里就先维持原样,只 broadcast 和 pushMessage
|
|
|
|
|
|
|
2026-04-11 12:56:22 +08:00
|
|
|
|
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)
|
|
|
|
|
|
*/
|
2026-04-11 14:13:52 +08:00
|
|
|
|
private function buildFinalMessage(string $baseMessage, int $bonusExp, int $bonusJjb, $user): string
|
2026-04-11 12:56:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
$bonusParts = [];
|
|
|
|
|
|
|
|
|
|
|
|
if ($bonusExp > 0) {
|
|
|
|
|
|
$bonusParts[] = "+经验{$bonusExp}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($bonusJjb > 0) {
|
|
|
|
|
|
$bonusParts[] = "+金币{$bonusJjb}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (empty($bonusParts)) {
|
|
|
|
|
|
return $baseMessage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 14:22:22 +08:00
|
|
|
|
return $baseMessage . '(' . $user->vipName() . '追加:' . implode(',', $bonusParts) . ')';
|
2026-03-26 11:49:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 随机钓鱼结果(从数据库 fishing_events 加权随机抽取)
|
|
|
|
|
|
*
|
|
|
|
|
|
* 若数据库中无激活事件,回退到兜底结果。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array{emoji: string, message: string, exp: int, jjb: int}
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function randomFishResult(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$event = FishingEvent::rollOne();
|
|
|
|
|
|
|
2026-04-11 13:45:10 +08:00
|
|
|
|
if (!$event) {
|
2026-03-26 11:49:36 +08:00
|
|
|
|
return [
|
|
|
|
|
|
'emoji' => '🐟',
|
|
|
|
|
|
'message' => '钓到一条小鱼,获得金币10',
|
|
|
|
|
|
'exp' => 0,
|
|
|
|
|
|
'jjb' => 10,
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'emoji' => $event->emoji,
|
|
|
|
|
|
'message' => $event->message,
|
2026-04-11 13:45:10 +08:00
|
|
|
|
'exp' => (int)$event->exp,
|
|
|
|
|
|
'jjb' => (int)$event->jjb,
|
2026-03-26 11:49:36 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|