功能:随机浮漂钓鱼防挂机 + 商店自动钓鱼卡

核心变更:
1. FishingController 重写
   - cast(): 生成随机浮漂坐标(x/y%) + 一次性 token
   - reel(): 必须携带 token 才能收竿(防脚本绕过)
   - 检测自动钓鱼卡剩余时间并返回给前端

2. 前端钓鱼逻辑重写
   - 抛竿后显示随机位置 🪝 浮漂动画(全屏飘动)
   - 鱼上钩时浮漂「下沉」动画,8秒内点击浮漂才能收竿
   - 超时未点击:鱼跑了,token 也失效
   - 持有自动钓鱼卡:自动点击,紫色提示剩余时间

3. 商店新增「🎣 自动钓鱼卡」分组
   - 3档:2h(800金)/8h(2500金)/24h(6000金)
   - 图标徽章显示剩余有效时间(紫色)
   - 购买后即时激活,无需手动操作

4. 数据库
   - shop_items.type 加 auto_fishing 枚举
   - shop_items.duration_minutes 新字段(分钟精度)
   - Seeder 写入 3 张卡数据

防挂机原理:按钮 → 浮漂随机位置,脚本无法固定坐标点击
This commit is contained in:
2026-03-01 16:19:45 +08:00
parent e0c15b437e
commit 63679a622f
9 changed files with 417 additions and 77 deletions

View File

@@ -2,12 +2,16 @@
/**
* 文件功能:钓鱼小游戏控制器
* 复刻原版 ASP 聊天室 diaoyu/ 目录下的钓鱼功能
* 简化掉鱼竿道具系统,用 Redis 控制冷却,随机奖惩经验/金币
*
* 新增随机浮漂点击防挂机机制:
* - 抛竿时生成一次性 token 和随机浮漂坐标(百分比),返回给前端
* - 前端显示漂浮动画,等待下沉后用户点击,将 token 随收竿请求提交
* - 若持有有效「自动钓鱼卡」,前端直接自动点击,无需手动操作
* - 服务端验证 token 有效性,防止脚本直接调用收竿接口
*
* @author ChatRoom Laravel
*
* @version 1.0.0
* @version 2.0.0
*/
namespace App\Http\Controllers;
@@ -16,12 +20,14 @@ use App\Enums\CurrencySource;
use App\Events\MessageSent;
use App\Models\Sysparam;
use App\Services\ChatStateService;
use App\Services\ShopService;
use App\Services\UserCurrencyService;
use App\Services\VipService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
class FishingController extends Controller
{
@@ -29,10 +35,17 @@ class FishingController extends Controller
private readonly ChatStateService $chatState,
private readonly VipService $vipService,
private readonly UserCurrencyService $currencyService,
private readonly ShopService $shopService,
) {}
/**
* 抛竿 检查冷却和金币,扣除金币,返回随机等待时间
* 抛竿 检查冷却和金币,扣除金币,生成浮漂 token 和随机坐标。
*
* 返回:
* wait_time 等待秒数(前端倒数后触发下沉动画)
* bobber_x/y 浮漂随机位置0-100 百分比)
* token 本次钓鱼唯一令牌(收竿时必须携带)
* auto_fishing 是否持有有效自动钓鱼卡(前端据此自动点击)
*
* @param int $id 房间ID
*/
@@ -64,36 +77,49 @@ class FishingController extends Controller
], 422);
}
// 3. 扣除金币(通过统一积分服务记录流水)
// 3. 扣除金币
$this->currencyService->change(
$user,
'gold',
-$cost,
$user, 'gold', -$cost,
CurrencySource::FISHING_COST,
"钓鱼抛竿消耗 {$cost} 金币",
$id,
);
$user->refresh(); // 刷新本地模型service 已原子更新)
$user->refresh();
// 4. 设置"正在钓鱼"标记防止重复抛竿30秒后自动过期
Redis::setex("fishing:active:{$user->id}", 30, time());
// 5. 计算随机等待时间
// 4. 生成一次性 token存入 RedisTTL = 等待时间 + 收竿窗口 + 缓冲
$waitMin = (int) Sysparam::getValue('fishing_wait_min', '8');
$waitMax = (int) Sysparam::getValue('fishing_wait_max', '15');
$waitTime = rand($waitMin, $waitMax);
$token = Str::random(32);
$tokenKey = "fishing:token:{$user->id}";
// token 有效期 = 等待时间 + 10秒点击窗口 + 5秒缓冲
Redis::setex($tokenKey, $waitTime + 15, $token);
// 5. 生成随机浮漂坐标(百分比,避开边缘)
$bobberX = rand(15, 85); // 左右 15%~85%
$bobberY = rand(20, 65); // 上下 20%~65%
// 6. 检查是否持有有效自动钓鱼卡
$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,
]);
}
/**
* 收竿 随机计算钓鱼结果,更新经验/金币,广播到聊天室
* 收竿 验证浮漂 token随机计算钓鱼结果,更新经验/金币,广播到聊天室
*
* 必须携带 token从抛竿接口获取否则判定为非法收竿。
*
* @param int $id 房间ID
*/
@@ -104,17 +130,20 @@ class FishingController extends Controller
return response()->json(['status' => 'error', 'message' => '请先登录'], 401);
}
// 1. 检查是否有"正在钓鱼"标记
$activeKey = "fishing:active:{$user->id}";
if (! Redis::exists($activeKey)) {
// 1. 验证 token防止脚本绕过浮漂直接收竿
$tokenKey = "fishing:token:{$user->id}";
$storedToken = Redis::get($tokenKey);
$clientToken = $request->input('token', '');
if (! $storedToken || $storedToken !== $clientToken) {
return response()->json([
'status' => 'error',
'message' => '您还没有抛竿,或者鱼已经跑了!',
'message' => '鱼儿跑了!浮漂已超时或令牌无效,请重新抛竿。',
], 422);
}
// 清除钓鱼标记
Redis::del($activeKey);
// 清除 token一次性
Redis::del($tokenKey);
// 2. 设置冷却时间
$cooldown = (int) Sysparam::getValue('fishing_cooldown', '300');
@@ -123,7 +152,7 @@ class FishingController extends Controller
// 3. 随机决定钓鱼结果
$result = $this->randomFishResult();
// 4. 通过统一积分服务更新经验和金币,写入流水
// 4. 通过统一积分服务更新经验和金币
$expMul = $this->vipService->getExpMultiplier($user);
$jjbMul = $this->vipService->getJjbMultiplier($user);
if ($result['exp'] !== 0) {
@@ -140,7 +169,7 @@ class FishingController extends Controller
"钓鱼收竿:{$result['message']}", $id,
);
}
$user->refresh(); // 刷新获取最新余额
$user->refresh();
// 5. 广播钓鱼结果到聊天室
$sysMsg = [
@@ -175,15 +204,6 @@ class FishingController extends Controller
{
$roll = rand(1, 100);
// 概率分布(总计 100%
// 1-15: 大鲨鱼 (+100exp, +20金)
// 16-30: 娃娃鱼 (+0exp, +30金)
// 31-50: 大草鱼 (+50exp)
// 51-70: 小鲤鱼 (+50exp, +10金)
// 71-85: 落水 (-50exp)
// 86-95: 被打 (-20exp, -3金)
// 96-100:大丰收 (+150exp, +50金)
return match (true) {
$roll <= 15 => [
'emoji' => '🦈',