Timer
This commit is contained in:
@@ -37,14 +37,94 @@ class TimerEngine {
|
||||
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() {
|
||||
const now = process.hrtime.bigint();
|
||||
const phaseRemainingMs = Number((this.phaseEndTime - now) / NS_PER_MS);
|
||||
const totalRemainingMs = Number((this.sessionEndTime - now) / NS_PER_MS);
|
||||
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,
|
||||
@@ -56,6 +136,10 @@ class TimerEngine {
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -73,10 +157,17 @@ class TimerEngine {
|
||||
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,
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user