json(['status' => 'error', 'message' => '请先登录'], 401); } // 检查钓鱼全局开关 if (! GameConfig::isEnabled('fishing')) { return response()->json(['status' => 'error', 'message' => '钓鱼功能暂未开放。'], 403); } if (! $this->roomScopeService->isRoomAllowedForGame('fishing', $id)) { return response()->json(['status' => 'error', 'message' => '当前房间未开启钓鱼小游戏。'], 403); } // 1. 检查冷却时间(Redis TTL) $cooldownKey = "fishing:cd:{$user->id}"; if (Redis::exists($cooldownKey)) { $ttl = Redis::ttl($cooldownKey); return response()->json([ 'status' => 'error', 'message' => "钓鱼冷却中,还需等待 {$ttl} 秒。", 'cooldown' => $ttl, ], 429); } $tokenKey = "fishing:token:{$user->id}"; if (Redis::exists($tokenKey)) { $activeSessionResponse = $this->restoreActiveFishingSessionResponse($user, $tokenKey); if ($activeSessionResponse) { return $activeSessionResponse; } } // 2. 检查金币是否足够 $cost = (int) (GameConfig::param('fishing', 'fishing_cost') ?? Sysparam::getValue('fishing_cost', '5')); if (($user->jjb ?? 0) < $cost) { return response()->json([ 'status' => 'error', 'message' => "金币不足!钓鱼需要 {$cost} 金币,您当前只有 {$user->jjb} 金币。", ], 422); } // 3. 生成一次性 token,存入 Redis(TTL = 等待时间 + 收竿窗口 + 缓冲) $waitMin = (int) (GameConfig::param('fishing', 'fishing_wait_min') ?? Sysparam::getValue('fishing_wait_min', '8')); $waitMax = (int) (GameConfig::param('fishing', 'fishing_wait_max') ?? Sysparam::getValue('fishing_wait_max', '15')); $waitTime = rand($waitMin, $waitMax); $token = Str::random(32); $tokenTtl = $waitTime + 13; $tokenPayload = json_encode([ 'token' => $token, 'cast_at' => time(), 'wait_time' => $waitTime, ]); // 原子占用本次抛竿 token,避免多标签页自动钓鱼互相覆盖令牌。 $reserved = Redis::command('set', [$tokenKey, $tokenPayload, 'EX', $tokenTtl, 'NX']); if (! $reserved) { $activeSessionResponse = $this->restoreActiveFishingSessionResponse($user, $tokenKey); if ($activeSessionResponse) { return $activeSessionResponse; } return response()->json([ 'status' => 'error', 'message' => '钓鱼状态同步中,请稍后重试。', 'retry_after' => 3, ], 409); } try { // token 占用成功后才扣金币,确保重复抛竿不会多扣费用。 $this->currencyService->change( $user, 'gold', -$cost, CurrencySource::FISHING_COST, "钓鱼抛竿消耗 {$cost} 金币", $id, ); $user->refresh(); } catch (\Throwable $exception) { // 金币扣除失败时释放 token,避免用户被短时间卡在未收竿状态。 Redis::del($tokenKey); throw $exception; } // 4. 生成随机浮漂坐标(百分比,避开边缘) $bobberX = rand(15, 85); // 左右 15%~85% $bobberY = rand(20, 65); // 上下 20%~65% // 5. 检查是否持有有效自动钓鱼卡 $autoFishingMinutes = $this->shopService->getActiveAutoFishingMinutesLeft($user); return response()->json([ 'status' => 'success', 'message' => "已花费 {$cost} 金币,鱼竿已抛出!等待鱼儿上钩...", 'wait_time' => $waitTime, 'bobber_x' => $bobberX, 'bobber_y' => $bobberY, 'token' => $token, 'auto_fishing' => $autoFishingMinutes > 0, 'auto_fishing_minutes_left' => $autoFishingMinutes, 'cost' => $cost, 'jjb' => $user->jjb, ]); } /** * 恢复已有钓鱼会话,避免刷新页面后丢失前端内存里的收竿令牌。 */ private function restoreActiveFishingSessionResponse(User $user, string $tokenKey): ?JsonResponse { $stored = json_decode((string) Redis::get($tokenKey), true); if (! is_array($stored) || empty($stored['token'])) { Redis::del($tokenKey); return null; } $elapsed = time() - (int) ($stored['cast_at'] ?? 0); $waitTime = max(0, (int) ($stored['wait_time'] ?? 0) - $elapsed); $autoFishingMinutes = $this->shopService->getActiveAutoFishingMinutesLeft($user); return response()->json([ 'status' => 'success', 'message' => '已恢复正在进行的钓鱼,请等待本次收竿。', 'wait_time' => $waitTime, 'bobber_x' => rand(15, 85), 'bobber_y' => rand(20, 65), 'token' => (string) $stored['token'], 'auto_fishing' => $autoFishingMinutes > 0, 'auto_fishing_minutes_left' => $autoFishingMinutes, 'cost' => 0, 'jjb' => $user->jjb, 'restored' => true, ]); } /** * 收竿 — 验证浮漂 token,随机计算钓鱼结果,更新经验/金币,广播到聊天室。 * * 必须携带 token(从抛竿接口获取),否则判定为非法收竿。 * * @param int $id 房间ID */ public function reel(Request $request, int $id): JsonResponse { $user = Auth::user(); if (! $user) { return response()->json(['status' => 'error', 'message' => '请先登录'], 401); } if (! $this->roomScopeService->isRoomAllowedForGame('fishing', $id)) { return response()->json(['status' => 'error', 'message' => '当前房间未开启钓鱼小游戏。'], 403); } // 1. 验证 token + 服务端时间校验(防止前端篡改 wait_time 跳过等待) $tokenKey = "fishing:token:{$user->id}"; $storedJson = Redis::get($tokenKey); $clientToken = $request->input('token', ''); if (! $storedJson) { return response()->json([ 'status' => 'error', 'message' => '鱼儿跑了!浮漂已超时,请重新抛竿。', ], 422); } $stored = json_decode($storedJson, true); // 校验 token 一致性 if (($stored['token'] ?? '') !== $clientToken) { return response()->json([ 'status' => 'error', 'message' => '令牌无效,请重新抛竿。', ], 422); } // 校验服务端时间:距抛竿必须已过 wait_time 秒(允许 ±1s 误差) $elapsed = time() - (int) ($stored['cast_at'] ?? 0); $required = (int) ($stored['wait_time'] ?? 0); if ($elapsed < $required - 1) { return response()->json([ 'status' => 'error', 'message' => '鱼还没上钩,别急!', ], 422); } // 清除 token(一次性) Redis::del($tokenKey); // 2. 设置冷却时间 $cooldown = (int) (GameConfig::param('fishing', 'fishing_cooldown') ?? Sysparam::getValue('fishing_cooldown', '300')); Redis::setex("fishing:cd:{$user->id}", $cooldown, time()); // 3. 随机决定钓鱼结果并广播(直接调用服务) $result = $this->fishingService->processCatch($user, $id, false); return response()->json([ 'status' => 'success', 'result' => $result, 'exp_num' => $user->exp_num, 'jjb' => $user->jjb, 'cooldown_seconds' => $cooldown, // 前端自动钓鱼卡循环等待用 ]); } }