From 1f001cf1884465d7ac52a6ec71d9ec11b257481d Mon Sep 17 00:00:00 2001 From: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Date: Wed, 15 Jun 2022 20:37:21 +0200 Subject: [PATCH] Feat/153 (#157) * feat/153: always roll * feat/153: version bump --- .../src/classes/__tests__/classUtils.test.js | 81 +++++++---- server/src/classes/classUtils.js | 126 ++++++++++-------- server/src/package.json | 2 +- 3 files changed, 124 insertions(+), 85 deletions(-) diff --git a/server/src/classes/__tests__/classUtils.test.js b/server/src/classes/__tests__/classUtils.test.js index 42474b7c6..3cec2f6c8 100644 --- a/server/src/classes/__tests__/classUtils.test.js +++ b/server/src/classes/__tests__/classUtils.test.js @@ -1,8 +1,8 @@ import { DAY_TO_MS, getSelectionByRoll, - replacePlaceholder, normaliseEndTime, + replacePlaceholder, sortArrayByProperty, updateRoll, } from '../classUtils.js'; @@ -59,7 +59,7 @@ describe('test that roll loads selection in right order', () => { const eventlist = [ { id: 1, - timeStart: 0, + timeStart: 5, timeEnd: 10, isPublic: false, }, @@ -67,7 +67,7 @@ describe('test that roll loads selection in right order', () => { id: 2, timeStart: 10, timeEnd: 20, - isPublic: true, + isPublic: false, }, { id: 3, @@ -109,19 +109,35 @@ describe('test that roll loads selection in right order', () => { it('if timer is at 0', () => { const now = 0; + const expected = { + nowIndex: null, + nowId: null, + publicIndex: null, + nextIndex: 0, + publicNextIndex: 4, + timers: null, + timeToNext: 5, + }; + + const state = getSelectionByRoll(eventlist, now); + expect(state).toStrictEqual(expected); + }); + + it('if timer is at 5', () => { + const now = 5; const expected = { nowIndex: 0, nowId: 1, publicIndex: null, nextIndex: 1, - publicNextIndex: 1, + publicNextIndex: 4, timers: { _finishAt: 10, - _startedAt: 0, - current: 10, - duration: 10, + _startedAt: 5, + current: 5, + duration: 5, }, - timeToNext: 10, + timeToNext: 5, }; const state = getSelectionByRoll(eventlist, now); @@ -133,7 +149,7 @@ describe('test that roll loads selection in right order', () => { const expected = { nowIndex: 1, nowId: 2, - publicIndex: 1, + publicIndex: null, nextIndex: 2, publicNextIndex: 4, timers: { @@ -154,7 +170,7 @@ describe('test that roll loads selection in right order', () => { const expected = { nowIndex: 2, nowId: 3, - publicIndex: 1, + publicIndex: null, nextIndex: 3, publicNextIndex: 4, timers: { @@ -239,15 +255,38 @@ describe('test that roll loads selection in right order', () => { nowIndex: null, nowId: null, publicIndex: null, - nextIndex: null, - publicNextIndex: null, + nextIndex: 0, + publicNextIndex: 4, timers: null, - timeToNext: null, + timeToNext: DAY_TO_MS - now + eventlist[0].timeStart, }; const state = getSelectionByRoll(eventlist, now); expect(state).toStrictEqual(expected); }); + + it('handles rolls to next day with real values', () => { + const singleEventList = [ + { + id: 1, + timeStart: 36000000, // 10:00 + timeEnd: 39600000, // 11:00 + isPublic: true, + }, + ]; + const now = 64800000; // 18:00 + const expected = { + nowIndex: null, + nowId: null, + publicIndex: null, + nextIndex: 0, + publicNextIndex: 0, + timers: null, + timeToNext: DAY_TO_MS - now + singleEventList[0].timeStart, + }; + const state = getSelectionByRoll(singleEventList, now); + expect(state).toStrictEqual(expected); + }); }); // test getSelectionByRoll() @@ -372,22 +411,6 @@ describe('test that roll behaviour with overlapping times', () => { const state = getSelectionByRoll(eventlist, now); expect(state).toStrictEqual(expected); }); - - it('if timer is at 31', () => { - const now = 31; - const expected = { - nowIndex: null, - nowId: null, - publicIndex: null, - nextIndex: null, - publicNextIndex: null, - timers: null, - timeToNext: null, - }; - - const state = getSelectionByRoll(eventlist, now); - expect(state).toStrictEqual(expected); - }); }); // test replacePlaceholder() diff --git a/server/src/classes/classUtils.js b/server/src/classes/classUtils.js index cc2e91b51..ee9c0d47c 100644 --- a/server/src/classes/classUtils.js +++ b/server/src/classes/classUtils.js @@ -62,16 +62,8 @@ export const getSelectionByRoll = (arr, now) => { // current timer let timers = null; - // Order events by startTime - const orderedEvents = sortArrayByProperty(arr, 'timeStart'); - - // flags: select first event if several overlapping - let nowFound = false; - - // exit early if we are past the events - const lastEvent = orderedEvents[orderedEvents.length - 1]; - const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd); - if (now > lastNormalEnd) { + // exit early if there are no events + if (arr.length < 1) { return { nowIndex, nowId, @@ -83,59 +75,83 @@ export const getSelectionByRoll = (arr, now) => { }; } - // loop through events, look for where we should be - for (const e of orderedEvents) { - // When does the event end (handle midnight) - const normalEnd = normaliseEndTime(e.timeStart, e.timeEnd); + // Order events by startTime + const orderedEvents = sortArrayByProperty(arr, 'timeStart'); - if (normalEnd <= now) { - // event ran already + // preload first if we are past events + const lastEvent = orderedEvents[orderedEvents.length - 1]; + const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd); - // public event might not be the one running - if (e.isPublic && normalEnd > publicTime) { - publicTime = normalEnd; - publicIndex = arr.findIndex((a) => a.id === e.id); - } - } else if (normalEnd > now && now >= e.timeStart && !nowFound) { - // event is running + if (now > lastNormalEnd) { + nextIndex = 0; + timeToNext = orderedEvents[0].timeStart + DAY_TO_MS - now; - // it could also be public + // look for next public + for (const e of orderedEvents) { if (e.isPublic) { - publicTime = normalEnd; - publicIndex = arr.findIndex((a) => a.id === e.id); - } - - nowIndex = arr.findIndex((a) => a.id === e.id); - nowId = e.id; - - // set timers - timers = { - _startedAt: e.timeStart, - _finishAt: e.timeEnd, - duration: normalEnd - e.timeStart, - current: normalEnd - now, - }; - nowFound = true; - } else if (normalEnd > now) { - // event will run - - // no need to look after found first - if (nextIndex !== null && publicNextIndex !== null) continue; - - // look for next events - // check how far the start is from now - const wait = e.timeStart - now; - - if (nextIndex == null || wait < timeToNext) { - timeToNext = wait; - nextIndex = arr.findIndex((a) => a.id === e.id); - } - if ((publicNextIndex === null || wait < publicTimeToNext) && e.isPublic) { - publicTimeToNext = wait; publicNextIndex = arr.findIndex((a) => a.id === e.id); + break; + } + } + } else { + // flags: select first event if several overlapping + let nowFound = false; + + // loop through events, look for where we should be + for (const e of orderedEvents) { + // When does the event end (handle midnight) + const normalEnd = normaliseEndTime(e.timeStart, e.timeEnd); + + if (normalEnd <= now) { + // event ran already + + // public event might not be the one running + if (e.isPublic && normalEnd > publicTime) { + publicTime = normalEnd; + publicIndex = arr.findIndex((a) => a.id === e.id); + } + } else if (normalEnd > now && now >= e.timeStart && !nowFound) { + // event is running + + // it could also be public + if (e.isPublic) { + publicTime = normalEnd; + publicIndex = arr.findIndex((a) => a.id === e.id); + } + + nowIndex = arr.findIndex((a) => a.id === e.id); + nowId = e.id; + + // set timers + timers = { + _startedAt: e.timeStart, + _finishAt: e.timeEnd, + duration: normalEnd - e.timeStart, + current: normalEnd - now, + }; + nowFound = true; + } else if (normalEnd > now) { + // event will run + + // no need to look after found first + if (nextIndex !== null && publicNextIndex !== null) continue; + + // look for next events + // check how far the start is from now + const wait = e.timeStart - now; + + if (nextIndex === null || wait < timeToNext) { + timeToNext = wait; + nextIndex = arr.findIndex((a) => a.id === e.id); + } + if ((publicNextIndex === null || wait < publicTimeToNext) && e.isPublic) { + publicTimeToNext = wait; + publicNextIndex = arr.findIndex((a) => a.id === e.id); + } } } } + return { nowIndex, nowId, diff --git a/server/src/package.json b/server/src/package.json index 3e6f624a7..3d63c1a0e 100644 --- a/server/src/package.json +++ b/server/src/package.json @@ -1,7 +1,7 @@ { "name": "ontime-server", "type": "module", - "version": "1.1.0", + "version": "1.1.1", "dependencies": { "body-parser": "^1.20.0", "dotenv": "^16.0.0",