* 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 {
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()
+71 -55
View File
@@ -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,
+1 -1
View File
@@ -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",