mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-22 23:49:15 +00:00
feat: implement event timer
This commit is contained in:
+6
-1
@@ -14,11 +14,16 @@ const playbackRouter = require('./routes/playbackRouter.js');
|
|||||||
const port = process.env.PORT || config.server.port;
|
const port = process.env.PORT || config.server.port;
|
||||||
|
|
||||||
// Global Objects
|
// Global Objects
|
||||||
|
const eventlist = require('./data/eventsData.json');
|
||||||
const EventTimer = require('./classes/EventTimer.js');
|
const EventTimer = require('./classes/EventTimer.js');
|
||||||
|
|
||||||
// TODO: this should be replaced by some sort of calculation
|
// TODO: this should be replaced by some sort of calculation
|
||||||
let durationForNow = 5400;
|
let durationForNow = 5400;
|
||||||
global.timer = new EventTimer();
|
global.timer = new EventTimer();
|
||||||
timer.setupWithSeconds(durationForNow, true);
|
timer.setupWithEventList(eventlist);
|
||||||
|
// timer.setupWithSeconds(durationForNow, true);
|
||||||
|
|
||||||
|
console.log(timer.print());
|
||||||
|
|
||||||
// Socket
|
// Socket
|
||||||
const initiateSocket = require('./controllers/socketController.js');
|
const initiateSocket = require('./controllers/socketController.js');
|
||||||
|
|||||||
@@ -18,10 +18,84 @@ class EventTimer extends Timer {
|
|||||||
visible: false,
|
visible: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
selectedEvent = null;
|
||||||
|
selectedEventId = null;
|
||||||
|
numEvents = null;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupWithEventList(eventlist) {
|
||||||
|
// filter only events
|
||||||
|
const events = eventlist.filter((e) => e.type === 'event');
|
||||||
|
|
||||||
|
const numEvents = events.length;
|
||||||
|
if (numEvents < 1) return;
|
||||||
|
|
||||||
|
// set general
|
||||||
|
this._eventList = events;
|
||||||
|
this.numEvents = numEvents;
|
||||||
|
|
||||||
|
// load first event
|
||||||
|
this.loadEvent(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadEvent(eventIndex) {
|
||||||
|
// set event specific
|
||||||
|
const e = this._eventList[eventIndex];
|
||||||
|
const start =
|
||||||
|
e.timeStart == null || e.timeStart === '' ? 0 : new Date(e.timeStart);
|
||||||
|
const end =
|
||||||
|
e.timeEnd == null || e.timeEnd === '' ? 0 : new Date(e.timeEnd);
|
||||||
|
|
||||||
|
this._resetTimers();
|
||||||
|
this.duration = end - start;
|
||||||
|
this.current = this.duration;
|
||||||
|
this.selectedEvent = eventIndex;
|
||||||
|
this.selectedEventId = e.id;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
print() {
|
||||||
|
// TODO: better formatting?
|
||||||
|
return `
|
||||||
|
Timer
|
||||||
|
=========
|
||||||
|
|
||||||
|
Playback
|
||||||
|
------------------------------
|
||||||
|
state = ${this.state}
|
||||||
|
current = ${this.current}
|
||||||
|
duration = ${this.duration}
|
||||||
|
|
||||||
|
Options
|
||||||
|
------------------------------
|
||||||
|
showNegative = ${this.showNegative}
|
||||||
|
|
||||||
|
Events
|
||||||
|
------------------------------
|
||||||
|
selectedEvent = ${this.selectedEvent}
|
||||||
|
selectedEventId = ${this.selectedEventId}
|
||||||
|
numEvents = ${this.numEvents}
|
||||||
|
|
||||||
|
Messages
|
||||||
|
------------------------------
|
||||||
|
presenter text = ${this.presenter.text}
|
||||||
|
presenter vis = ${this.presenter.visible}
|
||||||
|
public text = ${this.public.text}
|
||||||
|
public vis = ${this.public.visible}
|
||||||
|
|
||||||
|
Private
|
||||||
|
------------------------------
|
||||||
|
finishAt = ${this._finishAt}
|
||||||
|
startedAt = ${this._startedAt}
|
||||||
|
pausedAt = ${this._pausedAt}
|
||||||
|
pausedInterval = ${this._pausedInterval}
|
||||||
|
pausedTotal = ${this._pausedTotal}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
set presenterText(text) {
|
set presenterText(text) {
|
||||||
this.presenter.text = text;
|
this.presenter.text = text;
|
||||||
}
|
}
|
||||||
@@ -46,6 +120,34 @@ class EventTimer extends Timer {
|
|||||||
return this.public;
|
return this.public;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getEventData() {}
|
||||||
|
|
||||||
|
goto(eventIndex) {
|
||||||
|
this.loadEvent(eventIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
roll() {
|
||||||
|
console.log('roll: not yet implemented');
|
||||||
|
return false;
|
||||||
|
this.state = 'roll';
|
||||||
|
}
|
||||||
|
|
||||||
|
previous() {
|
||||||
|
const gotoEvent = this.selectedEvent > 0 ? this.selectedEvent - 1 : 0;
|
||||||
|
|
||||||
|
if (gotoEvent === this.selectedEvent) return;
|
||||||
|
this.goto(gotoEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
next() {
|
||||||
|
const gotoEvent =
|
||||||
|
this.selectedEvent < this.numEvents - 1
|
||||||
|
? this.selectedEvent + 1
|
||||||
|
: this.numEvents - 1;
|
||||||
|
|
||||||
|
if (gotoEvent === this.selectedEvent) return;
|
||||||
|
this.goto(gotoEvent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = EventTimer;
|
module.exports = EventTimer;
|
||||||
|
|||||||
+44
-23
@@ -5,13 +5,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Timer {
|
class Timer {
|
||||||
_current = null;
|
duration = null;
|
||||||
|
current = null;
|
||||||
_finishAt = null;
|
_finishAt = null;
|
||||||
_startedAt = null;
|
_startedAt = null;
|
||||||
_pausedAt = null;
|
_pausedAt = null;
|
||||||
_pausedInterval = null;
|
_pausedInterval = null;
|
||||||
_pausedTotal = null;
|
_pausedTotal = null;
|
||||||
state = 'pause';
|
state = 'stop';
|
||||||
showNegative = false;
|
showNegative = false;
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
@@ -22,6 +23,7 @@ class Timer {
|
|||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
|
|
||||||
// populate targets
|
// populate targets
|
||||||
|
this.duration = seconds * 1000;
|
||||||
this._finishAt = now + seconds * 1000;
|
this._finishAt = now + seconds * 1000;
|
||||||
|
|
||||||
// start counting
|
// start counting
|
||||||
@@ -47,15 +49,18 @@ class Timer {
|
|||||||
case 'start':
|
case 'start':
|
||||||
// update current timer
|
// update current timer
|
||||||
if (!this.showNegative) {
|
if (!this.showNegative) {
|
||||||
this._current = Math.max(this._finishAt + this._pausedTotal - now, 0);
|
this.current = Math.max(this._finishAt + this._pausedTotal - now, 0);
|
||||||
} else {
|
} else {
|
||||||
(this._current = this._finishAt + this._pausedTotal - now), 0;
|
(this.current = this._finishAt + this._pausedTotal - now), 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'pause':
|
case 'pause':
|
||||||
// update paused time
|
// update paused time
|
||||||
this._pausedInterval = now - this._pausedAt;
|
this._pausedInterval = now - this._pausedAt;
|
||||||
break;
|
break;
|
||||||
|
case 'stop':
|
||||||
|
// nothing here yet
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('Timer: no playstate on update call', this.state);
|
console.error('Timer: no playstate on update call', this.state);
|
||||||
break;
|
break;
|
||||||
@@ -71,11 +76,20 @@ class Timer {
|
|||||||
return this._finishAt + (this._pausedInterval + this._pausedTotal);
|
return this._finishAt + (this._pausedInterval + this._pausedTotal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_resetTimers() {
|
||||||
|
this._finishAt = null;
|
||||||
|
this._startedAt = null;
|
||||||
|
this._pausedAt = null;
|
||||||
|
this._pausedInterval = null;
|
||||||
|
this._pausedTotal = null;
|
||||||
|
this.state = 'stop';
|
||||||
|
}
|
||||||
|
|
||||||
// getObject
|
// getObject
|
||||||
getObject() {
|
getObject() {
|
||||||
this.update();
|
this.update();
|
||||||
return {
|
return {
|
||||||
currentSeconds: Timer.toSeconds(this._current),
|
currentSeconds: Timer.toSeconds(this.current),
|
||||||
expectedFinish: this._getExpectedFinish(),
|
expectedFinish: this._getExpectedFinish(),
|
||||||
startedAt: this._startedAt,
|
startedAt: this._startedAt,
|
||||||
};
|
};
|
||||||
@@ -85,7 +99,7 @@ class Timer {
|
|||||||
getCurrentInSeconds() {
|
getCurrentInSeconds() {
|
||||||
// update timeStamp
|
// update timeStamp
|
||||||
this.update();
|
this.update();
|
||||||
return Timer.toSeconds(this._current);
|
return Timer.toSeconds(this.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
get playState() {
|
get playState() {
|
||||||
@@ -96,21 +110,21 @@ class Timer {
|
|||||||
start() {
|
start() {
|
||||||
// do we need to change
|
// do we need to change
|
||||||
if (this.state === 'start') return;
|
if (this.state === 'start') return;
|
||||||
|
else if (this.state === 'stop') {
|
||||||
// update start time if needed
|
const now = new Date().getTime();
|
||||||
if (!this._startedAt) {
|
this._startedAt = now;
|
||||||
this._startedAt = new Date().getTime();
|
this._finishAt = now + this.duration;
|
||||||
|
} else {
|
||||||
|
// check if there is paused time
|
||||||
|
if (this._pausedInterval) {
|
||||||
|
this._pausedTotal += this._pausedInterval;
|
||||||
|
this._pausedInterval = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if there is paused time
|
|
||||||
if (this._pausedInterval) {
|
|
||||||
this._pausedTotal += this._pausedInterval;
|
|
||||||
this._pausedInterval = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// change state
|
// change state
|
||||||
this.state = 'start';
|
this.state = 'start';
|
||||||
}
|
}
|
||||||
|
|
||||||
pause() {
|
pause() {
|
||||||
// do we need to change
|
// do we need to change
|
||||||
if (this.state === 'pause') return;
|
if (this.state === 'pause') return;
|
||||||
@@ -122,12 +136,19 @@ class Timer {
|
|||||||
this.state = 'pause';
|
this.state = 'pause';
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
console.log('stop: not yet implemented');
|
// do we need to change
|
||||||
return false;
|
if (this.state === 'stop') return;
|
||||||
}
|
|
||||||
roll() {
|
// clear all timers
|
||||||
console.log('roll: not yet implemented');
|
this.current = duration;
|
||||||
return false;
|
this._finishAt = null;
|
||||||
|
this._startedAt = null;
|
||||||
|
this._pausedAt = null;
|
||||||
|
this._pausedInterval = null;
|
||||||
|
this._pausedTotal = null;
|
||||||
|
|
||||||
|
// change state
|
||||||
|
this.state = 'stop';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,13 +47,13 @@ exports.pbRoll = async (req, res) => {
|
|||||||
// Create controller for GET request to '/playback/previous'
|
// Create controller for GET request to '/playback/previous'
|
||||||
// Sets timer object to roll mode
|
// Sets timer object to roll mode
|
||||||
exports.pbPrevious = async (req, res) => {
|
exports.pbPrevious = async (req, res) => {
|
||||||
console.log('previous: not implemented');
|
global.timer.previous();
|
||||||
res.sendStatus(501);
|
res.sendStatus(200);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create controller for GET request to '/playback/next'
|
// Create controller for GET request to '/playback/next'
|
||||||
// Sets timer object to roll mode
|
// Sets timer object to roll mode
|
||||||
exports.pbNext = async (req, res) => {
|
exports.pbNext = async (req, res) => {
|
||||||
console.log('next: not implemented');
|
global.timer.next();
|
||||||
res.sendStatus(501);
|
res.sendStatus(200);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,11 +20,10 @@ const initiateSocket = (server, config) => {
|
|||||||
// send current data
|
// send current data
|
||||||
socket.emit('timer', global.timer.getObject());
|
socket.emit('timer', global.timer.getObject());
|
||||||
|
|
||||||
// unsubscribe to timer
|
socket.on('get-state', () => {
|
||||||
socket.on('release-timer', () => {
|
socket.emit('timer', global.timer.getObject());
|
||||||
console.log('Releasing subscription');
|
socket.emit('playstate', global.timer.playState);
|
||||||
// avoid multiple intervals
|
socket.emit('selected-id', global.timer.selectedEventId);
|
||||||
if (interval) clearInterval(interval);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('get-timer', () => {
|
socket.on('get-timer', () => {
|
||||||
@@ -35,6 +34,10 @@ const initiateSocket = (server, config) => {
|
|||||||
socket.emit('playstate', global.timer.playState);
|
socket.emit('playstate', global.timer.playState);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('get-selected-id', () => {
|
||||||
|
socket.emit('selected-id', global.timer.selectedEventId);
|
||||||
|
});
|
||||||
|
|
||||||
// playback API
|
// playback API
|
||||||
socket.on('set-presenter-text', (data) => {
|
socket.on('set-presenter-text', (data) => {
|
||||||
global.timer.presenterText = data;
|
global.timer.presenterText = data;
|
||||||
|
|||||||
Reference in New Issue
Block a user