Skip to content

Commit

Permalink
Keep flashbacks in state
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb committed Jan 5, 2024
1 parent f6a89cb commit bd072e0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 55 deletions.
71 changes: 37 additions & 34 deletions build/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions hxformat.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"sameLine": {
"ifBody": "same",
"elseBody": "same",
"ifElse": "keep",
"comprehensionFor": "keep"
"comprehensionFor": "keep",
"forBody": "keep"
},
"wrapping": {
"maxLineLength": 90,
Expand All @@ -22,6 +22,10 @@
},
"methodChain": {
"rules": []
}
},
},
"emptyLines": {
"beforeDocCommentEmptyLines": "ignore",
"afterFieldsWithDocComments": "ignore"
}
}
37 changes: 20 additions & 17 deletions src/server/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private typedef MainOptions = {
class Main {
static inline var VIDEO_START_MAX_DELAY = 3000;
static inline var VIDEO_SKIP_DELAY = 1000;
static inline var FLASHBACKS_COUNT = 50;
static inline var FLASHBACK_DIST = 30;

final rootDir = '$__dirname/..';
Expand All @@ -55,6 +56,7 @@ class Main {
final videoList = new VideoList();
final videoTimer = new VideoTimer();
final messages:Array<Message> = [];
final flashbacks:Array<FlashbackItem> = [];
final logger:Logger;

static function main():Void {
Expand Down Expand Up @@ -262,24 +264,28 @@ class Main {
timer: {
time: videoTimer.getTime(),
paused: videoTimer.isPaused()
}
},
flashbacks: flashbacks
}
}

function loadState():Void {
if (isNoState) return;
if (!FileSystem.exists(statePath)) return;
trace("Loading state...");
final data:ServerState = Json.parse(File.getContent(statePath));
videoList.setItems(data.videoList);
final state:ServerState = Json.parse(File.getContent(statePath));
videoList.setItems(state.videoList);
videoList.isOpen = state.isPlaylistOpen;
videoList.setPos(state.itemPos);

messages.resize(0);
videoList.isOpen = data.isPlaylistOpen;
videoList.setPos(data.itemPos);
for (message in data.messages) {
messages.push(message);
}
for (message in state.messages) messages.push(message);

flashbacks.resize(0);
for (flashback in state.flashbacks ?? []) flashbacks.push(flashback);

videoTimer.start();
videoTimer.setTime(data.timer.time);
videoTimer.setTime(state.timer.time);
videoTimer.pause();
}

Expand Down Expand Up @@ -1016,26 +1022,23 @@ class Main {
return findFlashbackItem(url, duration)?.time ?? 0.0;
}

final flashbackTimes:Array<FlashbackItem> = [];

function findFlashbackItem(url:String, ?duration:Float):Null<FlashbackItem> {
var item = flashbackTimes.find(item -> item.url == url);
var item = flashbacks.find(item -> item.url == url);
// if there is no url match, find recent flashback item with same duration
if (duration != null && item == null) {
item = flashbackTimes.find(item -> item.duration == duration);
item = flashbacks.find(item -> item.duration == duration);
}
return item;
}

function addRecentFlashback(url:String, duration:Float, time:Float):Void {
flashbackTimes.remove(findFlashbackItem(url));
flashbackTimes.unshift({
flashbacks.remove(findFlashbackItem(url));
flashbacks.unshift({
url: url,
duration: duration,
time: time
});
final length = flashbackTimes.count();
if (length > 10) flashbackTimes.pop();
while (flashbacks.length > FLASHBACKS_COUNT) flashbacks.pop();
}

function isPlaylistLockedFor(client:Client):Bool {
Expand Down
4 changes: 3 additions & 1 deletion src/server/ServerState.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package server;

import Types.FlashbackItem;
import Types.Message;
import Types.VideoItem;

Expand All @@ -10,5 +11,6 @@ typedef ServerState = {
messages:Array<Message>,
timer:{
time:Float, paused:Bool
}
},
?flashbacks:Array<FlashbackItem>
}

0 comments on commit bd072e0

Please sign in to comment.