From 20d5d8b12927321a6ee2ebfb0a2cb6ac7f76c8c5 Mon Sep 17 00:00:00 2001 From: arc-alex Date: Mon, 27 Nov 2023 17:42:42 +0100 Subject: [PATCH] move skip logic to timerUtils --- apps/server/src/config/config.js | 2 +- apps/server/src/services/TimerService.ts | 9 +- .../src/services/__tests__/rollUtils.test.ts | 88 ------------------- .../src/services/__tests__/timerUtils.test.ts | 24 ++++- apps/server/src/services/rollUtils.ts | 12 +-- apps/server/src/services/timerUtils.ts | 13 +++ 6 files changed, 45 insertions(+), 103 deletions(-) diff --git a/apps/server/src/config/config.js b/apps/server/src/config/config.js index 42ec30e52..158838f2d 100644 --- a/apps/server/src/config/config.js +++ b/apps/server/src/config/config.js @@ -9,5 +9,5 @@ export const config = { filename: 'override.css', }, restoreFile: 'ontime.restore', - rollSkipLimit: 3 * 32, + timeSkipLimit: 3 * 32, }; diff --git a/apps/server/src/services/TimerService.ts b/apps/server/src/services/TimerService.ts index 64c8ae577..47d096bb0 100644 --- a/apps/server/src/services/TimerService.ts +++ b/apps/server/src/services/TimerService.ts @@ -5,7 +5,7 @@ import { eventStore } from '../stores/EventStore.js'; import { PlaybackService } from './PlaybackService.js'; import { updateRoll } from './rollUtils.js'; import { integrationService } from './integration-service/IntegrationService.js'; -import { getCurrent, getExpectedFinish } from './timerUtils.js'; +import { getCurrent, getExpectedFinish, skipedOutOfEvent } from './timerUtils.js'; import { clock } from './Clock.js'; import { logger } from '../classes/Logger.js'; import type { RestorePoint } from './RestoreService.js'; @@ -339,7 +339,6 @@ export class TimerService { this.timer.expectedFinish >= this.timer.startedAt ? this.timer.expectedFinish : this.timer.expectedFinish + dayInMs, - _startAt: this.timer.startedAt, clock: this.timer.clock, secondaryTimer: this.timer.secondaryTimer, secondaryTarget: this.secondaryTarget, @@ -405,7 +404,11 @@ export class TimerService { let shouldNotify = false; if (this.playback === Playback.Roll) { shouldNotify = true; - this.updateRoll(); + if (skipedOutOfEvent(previousTime, this.timer.clock, this.timer.startedAt, this.timer.finishedAt)) { + PlaybackService.roll(); + } else { + this.updateRoll(); + } } else if (this.timer.startedAt !== null) { // we only update timer if a timer has been started shouldNotify = true; diff --git a/apps/server/src/services/__tests__/rollUtils.test.ts b/apps/server/src/services/__tests__/rollUtils.test.ts index ced5f1adc..329ece996 100644 --- a/apps/server/src/services/__tests__/rollUtils.test.ts +++ b/apps/server/src/services/__tests__/rollUtils.test.ts @@ -2,7 +2,6 @@ import { OntimeEvent } from 'ontime-types'; import { dayInMs } from 'ontime-utils'; import { getRollTimers, normaliseEndTime, sortArrayByProperty, updateRoll } from '../rollUtils.js'; -import { config } from '../../config/config.js'; // test sortArrayByProperty() describe('sort simple arrays of objects', () => { @@ -563,7 +562,6 @@ describe('typical scenarios', () => { selectedEventId: '1', current: 10, _finishAt: 15, - _startAt: 9, clock: 11, secondaryTimer: null, secondaryTarget: null, @@ -586,72 +584,11 @@ describe('typical scenarios', () => { expect(updateRoll(timers)).toStrictEqual(expected); }); - it('dose not skip isFinished under limit', () => { - const timers = { - selectedEventId: '1', - current: 13, - _finishAt: 15, - _startAt: 9, - clock: 14, - secondaryTimer: null, - secondaryTarget: null, - }; - - const expected = { - updatedTimer: timers._finishAt - timers.clock, - updatedSecondaryTimer: null, - doRollLoad: false, - isFinished: false, - }; - - expect(updateRoll(timers)).toStrictEqual(expected); - - // test that it can jump time - timers._finishAt = 14; - timers.clock += config.rollSkipLimit - 1; - expected.updatedTimer = timers._finishAt - timers.clock; - expected.doRollLoad = true; - expected.isFinished = true; - - expect(updateRoll(timers)).toStrictEqual(expected); - }); - - it('dose skips isFinished over limit', () => { - const timers = { - selectedEventId: '1', - current: 13, - _finishAt: 15, - _startAt: 9, - clock: 14, - secondaryTimer: null, - secondaryTarget: null, - }; - - const expected = { - updatedTimer: timers._finishAt - timers.clock, - updatedSecondaryTimer: null, - doRollLoad: false, - isFinished: false, - }; - - expect(updateRoll(timers)).toStrictEqual(expected); - - // test that it can jump time - timers._finishAt = 14; - timers.clock += config.rollSkipLimit + 1; - expected.updatedTimer = timers._finishAt - timers.clock; - expected.doRollLoad = true; - expected.isFinished = false; - - expect(updateRoll(timers)).toStrictEqual(expected); - }); - it('it updates secondary timer', () => { const timers = { selectedEventId: null, current: null, _finishAt: null, - _startAt: null, clock: 11, secondaryTimer: 1, secondaryTarget: 15, @@ -672,7 +609,6 @@ describe('typical scenarios', () => { selectedEventId: '1', current: 10, _finishAt: 11, - _startAt: 9, clock: 12, secondaryTimer: null, secondaryTarget: null, @@ -693,7 +629,6 @@ describe('typical scenarios', () => { selectedEventId: null, current: null, _finishAt: null, - _startAt: null, clock: 16, secondaryTimer: 1, secondaryTarget: 15, @@ -714,7 +649,6 @@ describe('typical scenarios', () => { selectedEventId: null, current: null, _finishAt: null, - _startAt: null, clock: 15, secondaryTimer: 0, secondaryTarget: 15, @@ -735,7 +669,6 @@ describe('typical scenarios', () => { selectedEventId: '1', current: 25, _finishAt: 10 + dayInMs, - _startAt: 10, clock: dayInMs - 10, secondaryTimer: null, secondaryTarget: null, @@ -756,7 +689,6 @@ describe('typical scenarios', () => { selectedEventId: '1', current: dayInMs, _finishAt: 10 + dayInMs, - _startAt: 10, clock: 10, secondaryTimer: null, secondaryTarget: null, @@ -771,24 +703,4 @@ describe('typical scenarios', () => { expect(updateRoll(timers)).toStrictEqual(expected); }); - - it('rolls backwards', () => { - const timers = { - selectedEventId: '1', - current: 11, - _startAt: 10, - _finishAt: 15, - clock: 9, - secondaryTimer: null, - secondaryTarget: null, - }; - - const expected = { - updatedTimer: timers._finishAt - timers.clock, - updatedSecondaryTimer: null, - doRollLoad: true, - isFinished: false, - }; - expect(updateRoll(timers)).toStrictEqual(expected); - }); }); diff --git a/apps/server/src/services/__tests__/timerUtils.test.ts b/apps/server/src/services/__tests__/timerUtils.test.ts index 2a6e15c67..9930da561 100644 --- a/apps/server/src/services/__tests__/timerUtils.test.ts +++ b/apps/server/src/services/__tests__/timerUtils.test.ts @@ -1,7 +1,8 @@ import { dayInMs } from 'ontime-utils'; import { TimerType } from 'ontime-types'; -import { getCurrent, getExpectedFinish } from '../timerUtils.js'; +import { getCurrent, getExpectedFinish, skipedOutOfEvent } from '../timerUtils.js'; +import { config } from '../../config/config.js'; describe('getExpectedFinish()', () => { it('is null if we havent started', () => { @@ -354,3 +355,24 @@ describe('getExpectedFinish() and getCurrentTime() combined', () => { expect(current).toBe(8); }); }); + +describe('skipedOutOfEvent()', () => { + it('without added times, they combine to be duration', () => { + const startedAt = 1000; + const duration = 1000; + const pausedTime = 0; + const finishedAt = null; + const addedTime = 0; + const timerType = TimerType.CountDown; + const expectedFinish = startedAt + duration; + const previousTime = 1000; + let clock = previousTime; + expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false); + clock += config.timeSkipLimit + 10; + expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(false); + clock = expectedFinish + 1; + expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true); + clock = startedAt - config.timeSkipLimit - 1; + expect(skipedOutOfEvent(previousTime, clock, startedAt, expectedFinish)).toBe(true); + }); +}); diff --git a/apps/server/src/services/rollUtils.ts b/apps/server/src/services/rollUtils.ts index 3a5cd13c4..98a6feac2 100644 --- a/apps/server/src/services/rollUtils.ts +++ b/apps/server/src/services/rollUtils.ts @@ -1,6 +1,5 @@ import { OntimeEvent } from 'ontime-types'; import { dayInMs } from 'ontime-utils'; -import { config } from '../config/config.js'; /** * handle events that span over midnight @@ -154,7 +153,6 @@ type CurrentTimers = { selectedEventId: string | null; current: number | null; _finishAt: number | null; - _startAt: number | null; clock: number | null; secondaryTimer: number | null; secondaryTarget: number | null; @@ -166,7 +164,7 @@ type CurrentTimers = { * @returns {object} object with selection variables */ export const updateRoll = (currentTimers: CurrentTimers) => { - const { selectedEventId, current, _finishAt, _startAt, clock, secondaryTimer, secondaryTarget } = currentTimers; + const { selectedEventId, current, _finishAt, clock, secondaryTimer, secondaryTarget } = currentTimers; // timers let updatedTimer = current; @@ -185,15 +183,9 @@ export const updateRoll = (currentTimers: CurrentTimers) => { } if (updatedTimer < 0) { - const hasSkipped = Math.abs(updatedTimer) > config.rollSkipLimit; - if (!hasSkipped) { - isPrimaryFinished = true; - } + isPrimaryFinished = true; // we need a new event doRollLoad = true; - } else if (clock < _startAt) { - // we have rolled back befor this evet start so we need a new one - doRollLoad = true; } } else if (secondaryTimer >= 0) { // if secondaryTimer is running we are in waiting to roll diff --git a/apps/server/src/services/timerUtils.ts b/apps/server/src/services/timerUtils.ts index 8edad2a7f..5b402650c 100644 --- a/apps/server/src/services/timerUtils.ts +++ b/apps/server/src/services/timerUtils.ts @@ -1,5 +1,6 @@ import { MaybeNumber, TimerType } from 'ontime-types'; import { dayInMs } from 'ontime-utils'; +import { config } from '../config/config.js'; /** * Calculates expected finish time of a running timer @@ -64,3 +65,15 @@ export function getCurrent( } return startedAt + duration + addedTime + pausedTime - clock; } + +export function skipedOutOfEvent(previousTime: number, clock: number, startedAt: number, finishAt): boolean { + const skipTime = previousTime - clock; + const hasSkipped = Math.abs(skipTime) > config.timeSkipLimit; + if (hasSkipped) { + if (clock > finishAt || clock < startedAt) { + return true; + } + // otherwise we just skipped within the event + } + return false; +}