Merge pull request #3 from cpvalente/feat/roll

Feat/roll
This commit is contained in:
Carlos Valente
2021-05-26 23:12:55 +02:00
committed by GitHub
9 changed files with 210 additions and 53 deletions
+167 -28
View File
@@ -109,8 +109,29 @@ class EventTimer extends Timer {
update() {
// if there is nothing selected, no nothing
if (this.selectedEventId == null) return;
super.update();
if (this.selectedEventId == null && this.state !== 'roll') return;
// only implement roll here
if (this.state !== 'roll') {
super.update();
return;
}
// get current time
const now = this._getCurrentTime();
this.clock = now;
if (this.selectedEventId && this.current > 0) {
// update timer as usual
this.current = this._finishAt - now;
} else {
// look for event if none is loaded
if (this.current <= 0 || this.secondaryTimer <= 0) this.rollLoad();
// count to next event
// TODO: replace with proper counter
if (this.secondaryTimer != null) this.secondaryTimer -= 1000;
}
}
start() {
@@ -140,10 +161,11 @@ class EventTimer extends Timer {
else if (payload === 'next') this.next();
else if (payload === 'reload') this.reload();
else if (payload === 'unload') this.unload();
else if (payload === 'roll') this.roll();
// Not yet implemented
// else if (payload === 'roll') this.roll();
// TODO: Cleanup
// here tdo this.broadcastState;
// remove broadcast from functions
this.broadcastThis('playstate', this.state);
this.broadcastThis('selected-id', this.selectedEventId);
this.broadcastThis('titles', this.titles);
@@ -522,18 +544,12 @@ class EventTimer extends Timer {
if (e == null) return;
// private title is always current
this.titles.titleNow = e.title;
this.titles.subtitleNow = e.subtitle;
this.titles.presenterNow = e.presenter;
this.selectedEventId = e.id;
// check if current is also public
if (e.isPublic) {
this.titlesPublic.titleNow = e.title;
this.titlesPublic.subtitleNow = e.subtitle;
this.titlesPublic.presenterNow = e.presenter;
this.selectedPublicEventId = e.id;
this._loadThisTitles(e, 'now');
} else {
this._loadThisTitles(e, 'now-private');
// assume there is no public event
this.titlesPublic.titleNow = null;
this.titlesPublic.subtitleNow = null;
@@ -549,16 +565,77 @@ class EventTimer extends Timer {
this._eventlist[i].type === 'event' &&
this._eventlist[i].isPublic
) {
this.titlesPublic.titleNow = this._eventlist[i].title;
this.titlesPublic.subtitleNow = this._eventlist[i].subtitle;
this.titlesPublic.presenterNow = this._eventlist[i].presenter;
this.selectedPublicEventId = this._eventlist[i].id;
this._loadThisTitles(this._eventlist[i], 'now-public');
break;
}
}
}
}
_loadThisTitles(e, type) {
if (e == null) return;
switch (type) {
// now, load to both public and private
case 'now':
// public
this.titlesPublic.titleNow = e.title;
this.titlesPublic.subtitleNow = e.subtitle;
this.titlesPublic.presenterNow = e.presenter;
this.selectedPublicEventId = e.id;
// private
this.titles.titleNow = e.title;
this.titles.subtitleNow = e.subtitle;
this.titles.presenterNow = e.presenter;
this.selectedEventId = e.id;
break;
case 'now-public':
this.titlesPublic.titleNow = e.title;
this.titlesPublic.subtitleNow = e.subtitle;
this.titlesPublic.presenterNow = e.presenter;
this.selectedPublicEventId = e.id;
break;
case 'now-private':
this.titles.titleNow = e.title;
this.titles.subtitleNow = e.subtitle;
this.titles.presenterNow = e.presenter;
this.selectedEventId = e.id;
break;
// next, load to both public and private
case 'next':
// public
this.titlesPublic.titleNext = e.title;
this.titlesPublic.subtitleNext = e.subtitle;
this.titlesPublic.presenterNext = e.presenter;
this.nextPublicEventId = e.id;
// private
this.titles.titleNext = e.title;
this.titles.subtitleNext = e.subtitle;
this.titles.presenterNext = e.presenter;
this.nextEventId = e.id;
break;
case 'next-public':
this.titlesPublic.titleNext = e.title;
this.titlesPublic.subtitleNext = e.subtitle;
this.titlesPublic.presenterNext = e.presenter;
this.nextPublicEventId = e.id;
break;
case 'next-private':
this.titles.titleNext = e.title;
this.titles.subtitleNext = e.subtitle;
this.titles.presenterNext = e.presenter;
this.nextEventId = e.id;
break;
default:
break;
}
}
_loadTitlesNext() {
// maybe there is nothing to load
if (this.selectedEventIndex == null) return;
@@ -583,19 +660,13 @@ class EventTimer extends Timer {
if (this._eventlist[i].type === 'event') {
// if we have not set private
if (!nextPrivate) {
this.titles.titleNext = this._eventlist[i].title;
this.titles.subtitleNext = this._eventlist[i].subtitle;
this.titles.presenterNext = this._eventlist[i].presenter;
this.nextEventId = this._eventlist[i].id;
this._loadThisTitles(this._eventlist[i], 'next-private');
nextPrivate = true;
}
// if event is public
if (this._eventlist[i].isPublic) {
this.titlesPublic.titleNext = this._eventlist[i].title;
this.titlesPublic.subtitleNext = this._eventlist[i].subtitle;
this.titlesPublic.presenterNext = this._eventlist[i].presenter;
this.nextPublicEventId = this._eventlist[i].id;
this._loadThisTitles(this._eventlist[i], 'next-public');
nextPublic = true;
}
}
@@ -642,6 +713,7 @@ class EventTimer extends Timer {
state = ${this.state}
current = ${this.current}
duration = ${this.duration}
secondaryTimer = ${this.secondaryTimer}
Events
------------------------------
@@ -730,10 +802,77 @@ class EventTimer extends Timer {
this.broadcastState();
}
rollLoad() {
const now = this._getCurrentTime();
this._resetTimers(true);
this._resetSelection();
let foundNow = null;
let nextIndex = null;
let nextStart = null;
// loop through events, look for where we should be
for (const [index, e] of this._eventlist.entries()) {
if (!foundNow) {
if (e.timeStart <= now && now < e.timeEnd) {
// set flag
foundNow = true;
// set timers
this._startedAt = e.timeStart;
this._finishAt = e.timeEnd;
this.duration = e.timeEnd - e.timeStart;
this.current = e.timeEnd - now;
// set selection
this.selectedEventId = e.id;
this.selectedEventIndex = index;
// set titles
this._loadTitlesNow();
// skip this entry for next
continue;
}
}
// check how far the start is from now
let wait = e.timeStart - now;
if (wait > 0) {
if (nextStart == null || wait < nextStart) {
nextStart = wait;
nextIndex = index;
}
}
}
// nothing to play next, unload
if (!foundNow && !nextIndex) {
this.unload();
return;
}
if (nextIndex) {
// load titles
this._loadThisTitles(nextIndex, 'next');
if (!foundNow) {
// timer counts to nextStart
this.secondaryTimer = nextStart;
}
}
}
roll() {
console.log('roll: not yet implemented');
return false;
// do we need to change
if (this.state === 'roll') return;
// load into event
this.rollLoad();
// set state
this.state = 'roll';
this.broadcastState();
}
previous() {