Spaces:
Sleeping
Sleeping
| // @ts-nocheck | |
| import { captureModeSuffix, effectiveScanMs, formatCountdown, formatScanInterval } from "./config.js"; | |
| import { state } from "./state.js"; | |
| export function updateSmartDelayUi() { | |
| const delayHost = document.getElementById('smart-delay-seconds'); | |
| const card = delayHost?.closest?.('.smart-delay-card'); | |
| if (!delayHost) return; | |
| const seconds = Math.round((state.smartDelayMs || 0) / 1000); | |
| delayHost.textContent = `+${seconds}s`; | |
| card?.classList.toggle('is-delaying', seconds > 0); | |
| } | |
| export function updateCountdown() { | |
| const countdownHost = document.getElementById('next-shot-countdown'); | |
| const dotHost = document.getElementById('next-shot-dot'); | |
| const sessionHost = document.getElementById('live-session-duration'); | |
| updateSmartDelayUi(); | |
| if (sessionHost) { | |
| const pausedMs = state.sessionPausedMs || 0; | |
| const inPauseMs = state.loopPaused && state.sessionPauseStartedAt ? Math.max(0, Date.now() - state.sessionPauseStartedAt) : 0; | |
| const elapsed = state.active && state.sessionStartedAt ? Math.floor((Date.now() - state.sessionStartedAt - pausedMs - inPauseMs) / 1000) : 0; | |
| sessionHost.textContent = formatCountdown(elapsed); | |
| sessionHost.classList.toggle('is-live', Boolean(state.active && state.sessionStartedAt)); | |
| } | |
| if (!countdownHost) return; | |
| if (!state.active || !state.nextScanAt) { | |
| countdownHost.textContent = state.active && state.loopPaused ? 'PAUSED' : '--:--'; | |
| dotHost?.classList.remove('is-live'); | |
| return; | |
| } | |
| const seconds = Math.ceil((state.nextScanAt - Date.now()) / 1000); | |
| countdownHost.textContent = formatCountdown(seconds); | |
| dotHost?.classList.add('is-live'); | |
| } | |
| export function startCountdownTimer() { | |
| if (state.countdownTimer) window.clearInterval(state.countdownTimer); | |
| state.countdownTimer = window.setInterval(updateCountdown, 1000); | |
| updateCountdown(); | |
| } | |
| export function stopCountdownTimer() { | |
| if (state.countdownTimer) { | |
| window.clearInterval(state.countdownTimer); | |
| state.countdownTimer = null; | |
| } | |
| state.nextScanAt = null; | |
| updateCountdown(); | |
| } | |
| export function scheduleNextScan(delayMs = null) { | |
| if (!state.active) return; | |
| if (state.loopPaused) { | |
| state.nextScanAt = null; | |
| updateCountdown(); | |
| return; | |
| } | |
| if (state.scanTimer) { | |
| window.clearTimeout(state.scanTimer); | |
| state.scanTimer = null; | |
| } | |
| const totalDelay = Math.max(1000, delayMs ?? (effectiveScanMs() + (state.smartDelayMs || 0))); | |
| state.nextScanAt = Date.now() + totalDelay; | |
| state.scanTimer = window.setTimeout(() => state.captureAndAnalyze?.(), totalDelay); | |
| updateCountdown(); | |
| } | |
| export function clearScheduledScan() { | |
| if (state.scanTimer) { | |
| window.clearTimeout(state.scanTimer); | |
| state.scanTimer = null; | |
| } | |
| state.nextScanAt = null; | |
| updateCountdown(); | |
| } | |
| export function readLatestAnalysisProject() { | |
| const resultHost = document.getElementById('important-response'); | |
| const raw = (resultHost?.querySelector?.('textarea, input')?.value || resultHost?.textContent || '').trim(); | |
| if (!raw) return ''; | |
| try { | |
| const parsed = JSON.parse(raw); | |
| return String(parsed.track || parsed.matchName || parsed.matchId || '').trim(); | |
| } catch {} | |
| const quoted = raw.match(/["']track["']\\s*:\\s*["']([^"']+)["']/i); | |
| if (quoted) return quoted[1].trim(); | |
| const label = raw.match(/\\btrack\\b\\s*[:\\n]\\s*([^,\\n{}]+)/i); | |
| return label ? label[1].trim().replace(/^["']|["']$/g, '') : ''; | |
| } | |
| export function updateSmartDelayFromLatestResult() { | |
| const projectName = readLatestAnalysisProject(); | |
| if (!projectName || /^unknown$/i.test(projectName)) { | |
| state.lastProjectName = ''; | |
| state.sameProjectStreak = 0; | |
| state.smartDelayMs = 0; | |
| updateSmartDelayUi(); | |
| return; | |
| } | |
| if (projectName === state.lastProjectName) state.sameProjectStreak += 1; | |
| else { state.lastProjectName = projectName; state.sameProjectStreak = 1; } | |
| if (state.sameProjectStreak < 3) state.smartDelayMs = 0; | |
| else state.smartDelayMs = Math.min(300, 15 * (2 ** Math.min(5, state.sameProjectStreak - 3))) * 1000; | |
| updateSmartDelayUi(); | |
| } | |