功能:随机浮漂钓鱼防挂机 + 商店自动钓鱼卡
核心变更: 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:
@@ -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,存入 Redis(TTL = 等待时间 + 收竿窗口 + 缓冲)
|
||||
$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' => '🦈',
|
||||
|
||||
@@ -42,6 +42,7 @@ class ShopController extends Controller
|
||||
'price' => $item->price,
|
||||
'type' => $item->type,
|
||||
'duration_days' => $item->duration_days,
|
||||
'duration_minutes' => $item->duration_minutes,
|
||||
'intimacy_bonus' => $item->intimacy_bonus,
|
||||
'charm_bonus' => $item->charm_bonus,
|
||||
]);
|
||||
@@ -61,7 +62,8 @@ class ShopController extends Controller
|
||||
'user_jjb' => $user->jjb ?? 0,
|
||||
'active_week_effect' => $this->shopService->getActiveWeekEffect($user),
|
||||
'has_rename_card' => $this->shopService->hasRenameCard($user),
|
||||
'ring_counts' => $ringCounts, // [item_id => qty]
|
||||
'ring_counts' => $ringCounts,
|
||||
'auto_fishing_minutes_left' => $this->shopService->getActiveAutoFishingMinutesLeft($user),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
+10
-1
@@ -17,7 +17,8 @@ class ShopItem extends Model
|
||||
|
||||
protected $fillable = [
|
||||
'name', 'slug', 'description', 'icon', 'price',
|
||||
'type', 'duration_days', 'sort_order', 'is_active',
|
||||
'type', 'duration_days', 'duration_minutes', 'sort_order', 'is_active',
|
||||
'intimacy_bonus', 'charm_bonus',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -32,6 +33,14 @@ class ShopItem extends Model
|
||||
return $this->hasMany(UserPurchase::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为自动钓鱼卡
|
||||
*/
|
||||
public function isAutoFishingCard(): bool
|
||||
{
|
||||
return $this->type === 'auto_fishing';
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为特效类商品(instant 或 duration,slug 以 once_ 或 week_ 开头)
|
||||
*/
|
||||
|
||||
@@ -33,6 +33,7 @@ class ShopService
|
||||
'duration' => $this->buyWeekCard($user, $item),
|
||||
'one_time' => $this->buyRenameCard($user, $item),
|
||||
'ring' => $this->buyRing($user, $item),
|
||||
'auto_fishing' => $this->buyAutoFishingCard($user, $item),
|
||||
default => ['ok' => false, 'message' => '未知商品类型'],
|
||||
};
|
||||
}
|
||||
@@ -245,4 +246,57 @@ class ShopService
|
||||
->whereHas('shopItem', fn ($q) => $q->where('slug', 'rename_card'))
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 购买自动钓鱼卡:手刺金币,写入 active 记录,到期时间 = 现在 + duration_minutes。
|
||||
*
|
||||
* @return array{ok:bool, message:string}
|
||||
*/
|
||||
public function buyAutoFishingCard(User $user, ShopItem $item): array
|
||||
{
|
||||
$minutes = (int) $item->duration_minutes;
|
||||
if ($minutes <= 0) {
|
||||
return ['ok' => false, 'message' => '该钓鱼卡配置异常,请联系管理员。'];
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($user, $item, $minutes): void {
|
||||
// 手刺金币
|
||||
$user->decrement('jjb', $item->price);
|
||||
// 写入背包(active,刻起计时)
|
||||
UserPurchase::create([
|
||||
'user_id' => $user->id,
|
||||
'shop_item_id' => $item->id,
|
||||
'status' => 'active',
|
||||
'price_paid' => $item->price,
|
||||
'expires_at' => Carbon::now()->addMinutes($minutes),
|
||||
]);
|
||||
});
|
||||
|
||||
$hours = round($minutes / 60, 1);
|
||||
|
||||
return [
|
||||
'ok' => true,
|
||||
'message' => "🎣 {$item->name}购买成功!{$hours}小时内鬼鱼自动收篼,尽情摆烂!",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户当前有效的自动钓鱼卡剩余分钟数(没有则返回 0)
|
||||
*/
|
||||
public function getActiveAutoFishingMinutesLeft(User $user): int
|
||||
{
|
||||
$purchase = UserPurchase::where('user_id', $user->id)
|
||||
->where('status', 'active')
|
||||
->whereNotNull('expires_at')
|
||||
->whereHas('shopItem', fn ($q) => $q->where('type', 'auto_fishing'))
|
||||
->where('expires_at', '>', Carbon::now())
|
||||
->orderByDesc('expires_at') // 取最晚过期的
|
||||
->first();
|
||||
|
||||
if (! $purchase) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) Carbon::now()->diffInMinutes($purchase->expires_at, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user