优化自动钓鱼

This commit is contained in:
2026-04-25 12:04:19 +08:00
parent 0623120c00
commit d46fe85c70
@@ -450,11 +450,81 @@
_autoFishCdCountdown = null;
}
const btn = document.getElementById('fishing-btn');
btn.textContent = '🎣 钓鱼';
btn.disabled = false;
if (btn) {
btn.textContent = '🎣 钓鱼';
btn.disabled = false;
}
fishingTimer = null;
fishingReelTimeout = null;
removeBobber();
}
/**
* 检查并自动开启钓鱼
*/
function checkAndAutoStartFishing() {
@if(auth()->check())
@php
$autoFishingMinutesLeft = app(\App\Services\ShopService::class)->getActiveAutoFishingMinutesLeft(auth()->user());
$cdKey = "fishing:cd:" . auth()->id();
$cooldown = \Illuminate\Support\Facades\Redis::ttl($cdKey);
$cooldown = $cooldown > 0 ? $cooldown : 0;
@endphp
const minutesLeft = {{ $autoFishingMinutesLeft }};
const initialCooldown = {{ $cooldown }};
if (minutesLeft > 0 && !_autoFishing) {
_autoFishing = true;
if (initialCooldown > 0) {
// 如果还在冷却中,恢复冷却倒计时和停止按钮
console.log(`检测到自动钓鱼卡有效,恢复钓鱼状态,剩余冷却 ${initialCooldown}s`);
const btn = document.getElementById('fishing-btn');
if (btn) {
btn.disabled = true;
btn.textContent = `⏳ 冷却 ${initialCooldown}s`;
btn.onclick = null;
}
_showAutoFishStopBtn(initialCooldown);
let remaining = initialCooldown;
_autoFishCdCountdown = setInterval(() => {
remaining--;
const b = document.getElementById('fishing-btn');
if (b) b.textContent = `⏳ 冷却 ${remaining}s`;
if (remaining <= 0) {
clearInterval(_autoFishCdCountdown);
_autoFishCdCountdown = null;
}
}, 1000);
_autoFishCdTimer = setTimeout(() => {
_autoFishCdTimer = null;
_hideAutoFishStopBtn();
if (_autoFishing) startFishing();
}, initialCooldown * 1000);
} else {
// 无冷却,直接开始
console.log('检测到自动钓鱼卡有效,自动抛竿');
startFishing();
}
}
@endif
}
document.addEventListener('DOMContentLoaded', () => {
// 页面初始化时检查
checkAndAutoStartFishing();
// 监听全员清屏事件(清屏后由于 DOM 可能会导致部分提示被清,确保自动钓鱼能继续,不过由于上面已是闭环,这里只是做个保险调用)
// 主要是如果用户在开着自动钓鱼,清屏不会终止 JS 变量,所以不需要完全重置。
// 但如果要求“全员清屏后也能自动开启”(指可能因为某种原因刷新了聊天流),我们可以额外保障一下。
window.addEventListener('chat:screen-cleared', () => {
if (!_autoFishing) {
checkAndAutoStartFishing();
}
});
});
</script>