From 97b8e6a29027cccafe1362ca0b717c8598494894 Mon Sep 17 00:00:00 2001 From: Leon Eckardt <36102288+ekordas@users.noreply.github.com> Date: Mon, 20 May 2024 22:28:23 +0200 Subject: [PATCH] fix #984 Expected End of timeline is not calculated correctly (#985) --- apps/server/src/services/__tests__/timerUtils.test.ts | 4 ++-- apps/server/src/services/timerUtils.ts | 6 +++--- apps/server/src/stores/__tests__/runtimeState.test.ts | 8 ++++---- apps/server/src/stores/runtimeState.ts | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/server/src/services/__tests__/timerUtils.test.ts b/apps/server/src/services/__tests__/timerUtils.test.ts index 7a83936ce..f4d3bae9f 100644 --- a/apps/server/src/services/__tests__/timerUtils.test.ts +++ b/apps/server/src/services/__tests__/timerUtils.test.ts @@ -1424,7 +1424,7 @@ describe('getRuntimeOffset()', () => { expect(offset).toBe(-50); }); - it('added time adds time ahead (positive offset)', () => { + it('added time subtracts time offset (positive offset)', () => { const state = { eventNow: { id: '1', @@ -1444,7 +1444,7 @@ describe('getRuntimeOffset()', () => { } as RuntimeState; const offset = getRuntimeOffset(state); - expect(offset).toBe(-40); + expect(offset).toBe(-60); }); it('considers running overtime (negative offset)', () => { diff --git a/apps/server/src/services/timerUtils.ts b/apps/server/src/services/timerUtils.ts index 96e635d97..57ce874cb 100644 --- a/apps/server/src/services/timerUtils.ts +++ b/apps/server/src/services/timerUtils.ts @@ -322,10 +322,10 @@ export function getRuntimeOffset(state: RuntimeState): MaybeNumber { const pausedTime = state._timer.pausedAt === null ? 0 : clock - state._timer.pausedAt; // startOffset - difference between scheduled start and actual start - // addedTime - time added by user (positive offset) + // addedTime - time added by user (negative offset) // pausedTime - time the playback was paused (negative offset) // overtime - how long the timer has been over-running (negative offset) - return startOffset + addedTime - pausedTime - overtime; + return startOffset - addedTime - pausedTime - overtime; } /** @@ -356,5 +356,5 @@ export function getExpectedEnd(state: RuntimeState): MaybeNumber { if (state.runtime.actualStart === null) { return null; } - return state.runtime.plannedEnd + state.runtime.offset + state._timer.totalDelay; + return state.runtime.plannedEnd - state.runtime.offset + state._timer.totalDelay; } diff --git a/apps/server/src/stores/__tests__/runtimeState.test.ts b/apps/server/src/stores/__tests__/runtimeState.test.ts index 58ecdef98..8cbe7bdf8 100644 --- a/apps/server/src/stores/__tests__/runtimeState.test.ts +++ b/apps/server/src/stores/__tests__/runtimeState.test.ts @@ -162,7 +162,7 @@ describe('mutation on runtimeState', () => { const firstStart = newState.clock; expect(newState.runtime.actualStart).toBe(newState.clock); expect(newState.runtime.offset).toBe(event1.timeStart - newState.clock); - expect(newState.runtime.expectedEnd).toBe(newState.runtime.offset + event2.timeEnd); + expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset); // 3. Next event load(event2, [event1, event2]); @@ -177,13 +177,13 @@ describe('mutation on runtimeState', () => { const delayBefore = event2.timeStart - newState.clock; expect(newState.runtime.offset).toBe(delayBefore); // finish is the difference between the runtime and the schedule - expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset); + expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset); // 4. Add time addTime(10); newState = getState(); - expect(newState.runtime.offset).toBe(delayBefore + 10); - expect(newState.runtime.expectedEnd).toBe(event2.timeEnd + newState.runtime.offset); + expect(newState.runtime.offset).toBe(delayBefore - 10); + expect(newState.runtime.expectedEnd).toBe(event2.timeEnd - newState.runtime.offset); // 5. Stop event stop(); diff --git a/apps/server/src/stores/runtimeState.ts b/apps/server/src/stores/runtimeState.ts index 24cd02d12..aadf93ad1 100644 --- a/apps/server/src/stores/runtimeState.ts +++ b/apps/server/src/stores/runtimeState.ts @@ -301,7 +301,7 @@ export function start(state: RuntimeState = runtimeState): boolean { } state.runtime.offset = getRuntimeOffset(state); - state.runtime.expectedEnd = state.runtime.plannedEnd + state.runtime.offset; + state.runtime.expectedEnd = state.runtime.plannedEnd - state.runtime.offset; return true; } @@ -389,6 +389,7 @@ export function update(): UpdateResult { // update offset runtimeState.runtime.offset = getRuntimeOffset(runtimeState); + runtimeState.runtime.expectedEnd = getExpectedEnd(runtimeState); return { hasTimerFinished,