* feat/153: always roll
* feat/153: version bump
This commit is contained in:
Carlos Valente
2022-06-15 20:37:21 +02:00
committed by GitHub
parent 323d365f1a
commit 1f001cf188
3 changed files with 124 additions and 85 deletions
+52 -29
View File
@@ -1,8 +1,8 @@
import { import {
DAY_TO_MS, DAY_TO_MS,
getSelectionByRoll, getSelectionByRoll,
replacePlaceholder,
normaliseEndTime, normaliseEndTime,
replacePlaceholder,
sortArrayByProperty, sortArrayByProperty,
updateRoll, updateRoll,
} from '../classUtils.js'; } from '../classUtils.js';
@@ -59,7 +59,7 @@ describe('test that roll loads selection in right order', () => {
const eventlist = [ const eventlist = [
{ {
id: 1, id: 1,
timeStart: 0, timeStart: 5,
timeEnd: 10, timeEnd: 10,
isPublic: false, isPublic: false,
}, },
@@ -67,7 +67,7 @@ describe('test that roll loads selection in right order', () => {
id: 2, id: 2,
timeStart: 10, timeStart: 10,
timeEnd: 20, timeEnd: 20,
isPublic: true, isPublic: false,
}, },
{ {
id: 3, id: 3,
@@ -109,19 +109,35 @@ describe('test that roll loads selection in right order', () => {
it('if timer is at 0', () => { it('if timer is at 0', () => {
const now = 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 = { const expected = {
nowIndex: 0, nowIndex: 0,
nowId: 1, nowId: 1,
publicIndex: null, publicIndex: null,
nextIndex: 1, nextIndex: 1,
publicNextIndex: 1, publicNextIndex: 4,
timers: { timers: {
_finishAt: 10, _finishAt: 10,
_startedAt: 0, _startedAt: 5,
current: 10, current: 5,
duration: 10, duration: 5,
}, },
timeToNext: 10, timeToNext: 5,
}; };
const state = getSelectionByRoll(eventlist, now); const state = getSelectionByRoll(eventlist, now);
@@ -133,7 +149,7 @@ describe('test that roll loads selection in right order', () => {
const expected = { const expected = {
nowIndex: 1, nowIndex: 1,
nowId: 2, nowId: 2,
publicIndex: 1, publicIndex: null,
nextIndex: 2, nextIndex: 2,
publicNextIndex: 4, publicNextIndex: 4,
timers: { timers: {
@@ -154,7 +170,7 @@ describe('test that roll loads selection in right order', () => {
const expected = { const expected = {
nowIndex: 2, nowIndex: 2,
nowId: 3, nowId: 3,
publicIndex: 1, publicIndex: null,
nextIndex: 3, nextIndex: 3,
publicNextIndex: 4, publicNextIndex: 4,
timers: { timers: {
@@ -239,15 +255,38 @@ describe('test that roll loads selection in right order', () => {
nowIndex: null, nowIndex: null,
nowId: null, nowId: null,
publicIndex: null, publicIndex: null,
nextIndex: null, nextIndex: 0,
publicNextIndex: null, publicNextIndex: 4,
timers: null, timers: null,
timeToNext: null, timeToNext: DAY_TO_MS - now + eventlist[0].timeStart,
}; };
const state = getSelectionByRoll(eventlist, now); const state = getSelectionByRoll(eventlist, now);
expect(state).toStrictEqual(expected); 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() // test getSelectionByRoll()
@@ -372,22 +411,6 @@ describe('test that roll behaviour with overlapping times', () => {
const state = getSelectionByRoll(eventlist, now); const state = getSelectionByRoll(eventlist, now);
expect(state).toStrictEqual(expected); 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() // test replacePlaceholder()
+71 -55
View File
@@ -62,16 +62,8 @@ export const getSelectionByRoll = (arr, now) => {
// current timer // current timer
let timers = null; let timers = null;
// Order events by startTime // exit early if there are no events
const orderedEvents = sortArrayByProperty(arr, 'timeStart'); if (arr.length < 1) {
// 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) {
return { return {
nowIndex, nowIndex,
nowId, nowId,
@@ -83,59 +75,83 @@ export const getSelectionByRoll = (arr, now) => {
}; };
} }
// loop through events, look for where we should be // Order events by startTime
for (const e of orderedEvents) { const orderedEvents = sortArrayByProperty(arr, 'timeStart');
// When does the event end (handle midnight)
const normalEnd = normaliseEndTime(e.timeStart, e.timeEnd);
if (normalEnd <= now) { // preload first if we are past events
// event ran already const lastEvent = orderedEvents[orderedEvents.length - 1];
const lastNormalEnd = normaliseEndTime(lastEvent.timeStart, lastEvent.timeEnd);
// public event might not be the one running if (now > lastNormalEnd) {
if (e.isPublic && normalEnd > publicTime) { nextIndex = 0;
publicTime = normalEnd; timeToNext = orderedEvents[0].timeStart + DAY_TO_MS - now;
publicIndex = arr.findIndex((a) => a.id === e.id);
}
} else if (normalEnd > now && now >= e.timeStart && !nowFound) {
// event is running
// it could also be public // look for next public
for (const e of orderedEvents) {
if (e.isPublic) { 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); 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 { return {
nowIndex, nowIndex,
nowId, nowId,
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "ontime-server", "name": "ontime-server",
"type": "module", "type": "module",
"version": "1.1.0", "version": "1.1.1",
"dependencies": { "dependencies": {
"body-parser": "^1.20.0", "body-parser": "^1.20.0",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",