'use strict'; const NS_PER_MS = BigInt(1_000_000); class TimerEngine { constructor(config, io, roomId) { this.config = config; this.io = io; this.roomId = roomId; this.status = 'idle'; this.phase = 'set'; this.currentLap = 1; this.currentExercise = 1; this.currentSet = 1; this.phaseEndTime = BigInt(0); this.sessionEndTime = BigInt(0); this.elapsedSeconds = 0; this._interval = null; this._lastEmittedSecond = -1; } start() { this.status = 'running'; const now = process.hrtime.bigint(); this.phaseEndTime = now + BigInt(this.config.setTime) * NS_PER_MS * BigInt(1000); this.sessionEndTime = now + BigInt(this.config.totalTime) * NS_PER_MS * BigInt(1000); this.io.to(this.roomId).emit('timer:state', this._snapshot()); this._interval = setInterval(() => this._tick(), 100); } stop() { this._clearInterval(); this.status = 'stopped'; this.io.to(this.roomId).emit('timer:stopped', { sessionId: this.roomId }); } pause() { if (this.status !== 'running') return; const now = process.hrtime.bigint(); this._pausedPhaseRemainingMs = Math.max(0, Number((this.phaseEndTime - now) / NS_PER_MS)); this._pausedTotalRemainingMs = Math.max(0, Number((this.sessionEndTime - now) / NS_PER_MS)); this._clearInterval(); this.status = 'paused'; this.io.to(this.roomId).emit('timer:paused', this._snapshot()); } resume() { if (this.status !== 'paused') return; const now = process.hrtime.bigint(); this.phaseEndTime = now + BigInt(Math.round(this._pausedPhaseRemainingMs)) * NS_PER_MS; this.sessionEndTime = now + BigInt(Math.round(this._pausedTotalRemainingMs)) * NS_PER_MS; this.status = 'running'; this._lastEmittedSecond = -1; this._interval = setInterval(() => this._tick(), 100); this.io.to(this.roomId).emit('timer:state', this._snapshot()); } restartSet() { const now = process.hrtime.bigint(); this.phase = 'set'; this._lastEmittedSecond = -1; this.phaseEndTime = now + BigInt(this.config.setTime) * NS_PER_MS * BigInt(1000); if (this.status === 'paused') { this._pausedPhaseRemainingMs = this.config.setTime * 1000; this.io.to(this.roomId).emit('timer:paused', this._snapshot()); } else { if (this.status !== 'running') { this.status = 'running'; this._interval = setInterval(() => this._tick(), 100); } this.io.to(this.roomId).emit('timer:phase-change', this._snapshot()); } } restartLap() { const { setTime, restTime, moveTime, setCount, exerciseCount, lapCount } = this.config; const now = process.hrtime.bigint(); this.currentSet = 1; this.currentExercise = 1; this.phase = 'set'; this._lastEmittedSecond = -1; this.phaseEndTime = now + BigInt(setTime) * NS_PER_MS * BigInt(1000); const remainingLaps = lapCount - this.currentLap + 1; const lapSec = exerciseCount * (setCount * setTime + Math.max(0, setCount - 1) * restTime) + Math.max(0, exerciseCount - 1) * moveTime; const totalRemainingMs = remainingLaps * lapSec * 1000; this.sessionEndTime = now + BigInt(Math.round(totalRemainingMs)) * NS_PER_MS; if (this.status === 'paused') { this._pausedPhaseRemainingMs = setTime * 1000; this._pausedTotalRemainingMs = totalRemainingMs; this.io.to(this.roomId).emit('timer:paused', this._snapshot()); } else { if (this.status !== 'running') { this.status = 'running'; this._interval = setInterval(() => this._tick(), 100); } this.io.to(this.roomId).emit('timer:state', this._snapshot()); } } getState() { return this._snapshot(); } _phaseTotalMs() { const { setTime, restTime, moveTime } = this.config; if (this.phase === 'set') return setTime * 1000; if (this.phase === 'rest') return restTime * 1000; return moveTime * 1000; } _snapshot() { let phaseRemainingMs, totalRemainingMs; if (this.status === 'paused') { phaseRemainingMs = this._pausedPhaseRemainingMs ?? 0; totalRemainingMs = this._pausedTotalRemainingMs ?? 0; } else { const now = process.hrtime.bigint(); phaseRemainingMs = Number((this.phaseEndTime - now) / NS_PER_MS); totalRemainingMs = Number((this.sessionEndTime - now) / NS_PER_MS); } return { status: this.status, phase: this.phase, currentLap: this.currentLap, currentExercise:this.currentExercise, currentSet: this.currentSet, lapCount: this.config.lapCount, exerciseCount: this.config.exerciseCount, setCount: this.config.setCount, phaseRemainingMs: Math.max(0, phaseRemainingMs), totalRemainingMs: Math.max(0, totalRemainingMs), phaseTotalMs: this._phaseTotalMs(), setTimeMs: this.config.setTime * 1000, restTimeMs: this.config.restTime * 1000, moveTimeMs: this.config.moveTime * 1000, phaseEndTime: Number(this.phaseEndTime / NS_PER_MS), // ms epoch }; } _tick() { if (this.status !== 'running') return; const now = process.hrtime.bigint(); const phaseRemainingMs = Number((this.phaseEndTime - now) / NS_PER_MS); const totalRemainingMs = Number((this.sessionEndTime - now) / NS_PER_MS); const currentSecond = Math.ceil(phaseRemainingMs / 1000); if (currentSecond !== this._lastEmittedSecond) { this._lastEmittedSecond = currentSecond; this.io.to(this.roomId).emit('timer:tick', { phaseRemainingMs: Math.max(0, phaseRemainingMs), totalRemainingMs: Math.max(0, totalRemainingMs), phaseTotalMs: this._phaseTotalMs(), setTimeMs: this.config.setTime * 1000, restTimeMs: this.config.restTime * 1000, moveTimeMs: this.config.moveTime * 1000, phase: this.phase, currentLap: this.currentLap, currentExercise: this.currentExercise, currentSet: this.currentSet, lapCount: this.config.lapCount, exerciseCount: this.config.exerciseCount, setCount: this.config.setCount, }); } if (phaseRemainingMs <= 0) { this._advancePhase(); } } _advancePhase() { const { setTime, restTime, moveTime, setCount, exerciseCount, lapCount } = this.config; const now = process.hrtime.bigint(); // State machine if (this.phase === 'set') { if (restTime > 0) { this.phase = 'rest'; this.phaseEndTime = now + BigInt(restTime) * NS_PER_MS * BigInt(1000); this.io.to(this.roomId).emit('timer:phase-change', this._snapshot()); return; } // fall through to move/next-set logic } if (this.phase === 'rest' || (this.phase === 'set' && restTime === 0)) { // Check if this was the last set const isLastSet = this.currentSet >= setCount; const isLastExercise = this.currentExercise >= exerciseCount; const isLastLap = this.currentLap >= lapCount; if (isLastSet && isLastExercise && isLastLap) { this._complete(); return; } if (moveTime > 0 && !isLastSet) { this.phase = 'move'; this.phaseEndTime = now + BigInt(moveTime) * NS_PER_MS * BigInt(1000); this.io.to(this.roomId).emit('timer:phase-change', this._snapshot()); return; } // Advance counters this._advance(); return; } if (this.phase === 'move') { this._advance(); } } _advance() { const { setCount, exerciseCount, lapCount, setTime } = this.config; const now = process.hrtime.bigint(); if (this.currentSet < setCount) { this.currentSet++; } else if (this.currentExercise < exerciseCount) { this.currentSet = 1; this.currentExercise++; } else if (this.currentLap < lapCount) { this.currentSet = 1; this.currentExercise = 1; this.currentLap++; } else { this._complete(); return; } this.phase = 'set'; this.phaseEndTime = now + BigInt(setTime) * NS_PER_MS * BigInt(1000); this._lastEmittedSecond = -1; this.io.to(this.roomId).emit('timer:phase-change', this._snapshot()); } _complete() { this._clearInterval(); this.status = 'complete'; this.io.to(this.roomId).emit('timer:complete', { sessionId: this.roomId }); } _clearInterval() { if (this._interval) { clearInterval(this._interval); this._interval = null; } } } module.exports = TimerEngine;