fixup! feat: implement speed controls

This commit is contained in:
Carlos Valente
2025-03-02 14:45:32 +01:00
parent ae1639802c
commit d0027d4016
4 changed files with 25 additions and 36 deletions
@@ -130,7 +130,6 @@ export const useTimerSpeed = createSelector((state: RuntimeStore) => ({
})); }));
export const setTimerSpeed = { export const setTimerSpeed = {
calculateSpeed: () => socketSendJson('calculate-speed'),
getSpeed: () => socketSendJson('get-speed'), getSpeed: () => socketSendJson('get-speed'),
setSpeed: (speed: number) => socketSendJson('set-speed', speed), setSpeed: (speed: number) => socketSendJson('set-speed', speed),
}; };
@@ -260,10 +260,6 @@ const actionHandlers: Record<string, ActionHandler> = {
throw new Error('No matching method provided'); throw new Error('No matching method provided');
}, },
/* Speed */ /* Speed */
'calculate-speed': () => {
const factor = runtimeService.calculateSpeed();
return { payload: factor };
},
'get-speed': () => { 'get-speed': () => {
const factor = runtimeService.getSpeed(); const factor = runtimeService.getSpeed();
return { payload: factor }; return { payload: factor };
@@ -667,14 +667,6 @@ class RuntimeService {
} }
} }
/**
* Utility calculates the speed factor necessary to finish on time
* @returns {number} speed factor needed to meet schedule
*/
public calculateSpeed(): number {
return runtimeState.calculateSpeed();
}
/** /**
* @returns {number} speed factor currently applied * @returns {number} speed factor currently applied
*/ */
@@ -688,19 +680,28 @@ class RuntimeService {
* @returns {number} applied speed factor * @returns {number} applied speed factor
*/ */
public setSpeed(speed: number): number { public setSpeed(speed: number): number {
// TODO: validate state if (speed < 0.5 || speed > 2.0) {
// TODO: validate value logger.warning(LogOrigin.Server, `Speed out of bounds: ${speed}`);
runtimeState.setSpeed(speed); return;
return runtimeState.getSpeed(); }
const currentState = runtimeState.getState();
if (currentState.eventNow === null) {
logger.warning(LogOrigin.Server, 'No event running to set speed to');
}
const newSpeed = runtimeState.setSpeed(speed);
logger.info(LogOrigin.Server, `Speed set to ${newSpeed}`);
return newSpeed;
} }
/** /**
* Resets the speed of the current timer * Resets the speed of the current timer
* @returns {number} applied speed factor
*/ */
public resetSpeed(): number { public resetSpeed(): number {
runtimeState.resetSpeed(); const speed = runtimeState.resetSpeed();
return runtimeState.getSpeed(); logger.info(LogOrigin.Server, `Speed set to ${speed}`);
return speed;
} }
} }
+9 -16
View File
@@ -350,33 +350,26 @@ export function updateAll(rundown: OntimeRundown) {
loadBlock(rundown); loadBlock(rundown);
} }
export function setSpeed(speed: number) { export function setSpeed(speed: number): number {
const remainingMs = runtimeState.timer.current;
const adjustedRemainingTimeMs = remainingMs / speed;
const newFinishTimeMs = runtimeState.clock + adjustedRemainingTimeMs;
runtimeState.timer.speed = speed; runtimeState.timer.speed = speed;
runtimeState.timer.expectedFinish = newFinishTimeMs;
return runtimeState.timer.speed;
} }
export function resetSpeed() { export function resetSpeed() {
runtimeState.timer.speed = 1.0; runtimeState.timer.speed = 1.0;
return runtimeState.timer.speed;
} }
export function getSpeed() { export function getSpeed() {
return runtimeState.timer.speed; return runtimeState.timer.speed;
} }
export function calculateSpeed() {
// TODO: what should this return if no timer is running?
if (runtimeState.eventNow !== null) {
const timeToDesiredFinish = runtimeState.eventNow.timeEnd - runtimeState.clock;
const timeToActualFinish = runtimeState.timer.expectedFinish - runtimeState.clock;
const factor = 1 / (timeToDesiredFinish / timeToActualFinish);
// TODO: this can produce negative speeds (i.e. the desired finish time is in the past)
// TODO: should there be some clamping or rounding on this number?
return factor;
}
return 1.0;
}
export function start(state: RuntimeState = runtimeState): boolean { export function start(state: RuntimeState = runtimeState): boolean {
if (state.eventNow === null) { if (state.eventNow === null) {
return false; return false;