Files
vitfit-r2/V2/app/modules/timers/timer-engine.js
T
2026-06-13 08:03:45 -04:00

170 lines
4.8 KiB
JavaScript

'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 });
}
getState() {
return this._snapshot();
}
_snapshot() {
const now = process.hrtime.bigint();
const phaseRemainingMs = Number((this.phaseEndTime - now) / NS_PER_MS);
const 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),
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),
phase: this.phase,
currentLap: this.currentLap,
currentExercise: this.currentExercise,
currentSet: this.currentSet,
});
}
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;