-
Notifications
You must be signed in to change notification settings - Fork 10
Expand the functionality of the linked list #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Smidqe
wants to merge
9
commits into
BTDev:master
Choose a base branch
from
Smidqe:refactor/extend-linkedlist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a6c5791
Remove all old playlist iterations in favor of new functions
Smidqe b23e513
Fix few issues
Smidqe 66e5200
More minor fixes
Smidqe b2bf035
Incorrect check, not correct
Smidqe 9a0fe99
Don't use find in indexOf
Smidqe 810e81e
Fixes and minor cleanups
Smidqe 8dc436e
More minor fixes
Smidqe 525be35
More small fixes
Smidqe 780335a
Rename variable to better reflect its use, and fix wrong text
Smidqe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,6 +197,75 @@ LinkedList.Circular.prototype.toArray = function () { | |
| return out; | ||
| }; | ||
|
|
||
|
|
||
| LinkedList.Circular.prototype.some = function(cb) { | ||
| return this.find(cb) !== null; | ||
| } | ||
|
|
||
| LinkedList.Circular.prototype.find = function(cb) { | ||
| let video = this.first; | ||
|
|
||
| for (let i = 0; i < this.length; i++) { | ||
| if (cb(video, i)) { | ||
| return video; | ||
| } | ||
|
|
||
| video = video.next; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| LinkedList.Circular.prototype.multiple = function(indexes) { | ||
| let map = new Map( | ||
| indexes.map(i => [i, null]) | ||
| ); | ||
|
|
||
| let video = this.first; | ||
| let max = Math.max(...indexes); | ||
|
|
||
| for (let i = 0; i <= max; i++) { | ||
| if (map.has(i)) { | ||
| map.set(i, video); | ||
| } | ||
|
|
||
| video = video.next; | ||
| } | ||
|
|
||
| return indexes.map(i => map.get(i)); | ||
| } | ||
| LinkedList.Circular.prototype.at = function(index) { | ||
| let video = this.first; | ||
|
|
||
| for (let i = 0; i < index; i++) { | ||
| video = video.next; | ||
| } | ||
|
|
||
| return video; | ||
| } | ||
|
|
||
| LinkedList.Circular.prototype.each = function(cb) { | ||
| let video = this.first; | ||
|
|
||
| for (let i = 0; i < this.length; i++) { | ||
| cb(video, i); | ||
| video = video.next; | ||
| } | ||
| } | ||
|
|
||
| LinkedList.Circular.prototype.indexOf = function(cb) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'm guessing this should be |
||
| let video = this.first; | ||
|
|
||
| for (let i = 0; i < this.length; i++) { | ||
| if (cb(video, i)) { | ||
| return i; | ||
| } | ||
|
|
||
| video = video.next; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| /* VAR INIT */ | ||
| SERVER.PLAYLIST = new LinkedList.Circular(); | ||
| SERVER.ACTIVE = null; | ||
|
|
@@ -279,20 +348,19 @@ function initPlaylist(callback) { | |
| } | ||
| function initResumePosition(callback) { | ||
| getMisc({ name: 'server_active_videoid' }, function (old_videoid) { | ||
| var elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < SERVER.PLAYLIST.length; i++) { | ||
| if (elem.videoid == old_videoid) { | ||
| SERVER.ACTIVE = elem; | ||
| getMisc({ name: 'server_time' }, function (old_time) { | ||
| if (+old_time) { | ||
| SERVER.TIME = +old_time + 1; | ||
| } | ||
| if (callback) { callback(); } | ||
| }); | ||
| return; | ||
| } | ||
| elem = elem.next; | ||
| const active = SERVER.PLAYLIST.find(video => video.videoid === old_videoid); | ||
|
|
||
| if (active) { | ||
| SERVER.ACTIVE = active; | ||
|
|
||
| getMisc({ name: 'server_time' }, function (old_time) { | ||
| if (+old_time) { | ||
| SERVER.TIME = +old_time + 1; | ||
| } | ||
| if (callback) { callback(); } | ||
| }); | ||
| } | ||
|
|
||
| if (callback) { callback(); } | ||
| }); | ||
| } | ||
|
|
@@ -481,14 +549,14 @@ function doorStuck(socket) { | |
| } | ||
| function playNext() { | ||
| const active = { | ||
| position: getVideoPosition(SERVER.ACTIVE), | ||
| position: SERVER.PLAYLIST.indexOf(video => video.videoid === SERVER.ACTIVE.videoid), | ||
| node: SERVER.ACTIVE | ||
| }; | ||
|
|
||
| SERVER.ACTIVE = SERVER.ACTIVE.next; | ||
|
|
||
| if (!active.node.volat && 'colorTagVolat' in active.node.meta) { | ||
| _setVideoColorTag(active.node, active.position, false, false); | ||
| setVideoColorTag(active.node, active.position, false, false); | ||
| } | ||
|
|
||
| if (active.node.volat) { | ||
|
|
@@ -658,17 +726,15 @@ function kickUserByNick(socket, nick, reason) { | |
| sessionService.forNick(nick, session => session.kick(reason, getSocketName(socket))); | ||
| } | ||
| var commit = function () { | ||
| var elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < SERVER.PLAYLIST.length; i++) { | ||
| SERVER.PLAYLIST.each((video, i) => { | ||
| var sql = `update ${SERVER.dbcon.video_table} set position = ? where videoid = ?`; | ||
| mysql.query(sql, [i, '' + elem.videoid], function (err) { | ||
| mysql.query(sql, [i, '' + video.videoid], function (err) { | ||
| if (err) { | ||
| DefaultLog.error(events.EVENT_DB_QUERY, "query \"{sql}\" failed", { sql }, err); | ||
| return; | ||
| } | ||
| }); | ||
| elem = elem.next; | ||
| } | ||
| }); | ||
|
|
||
| for (var i = 0; i < SERVER.AREAS.length; i++) { | ||
| var sql = 'update areas set html = ? where name = ?'; | ||
|
|
@@ -867,30 +933,20 @@ function applyPluginFilters(msg, socket) { | |
|
|
||
| return msg; | ||
| } | ||
| function setVideoVolatile(socket, pos, isVolat) { | ||
| var elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < pos; i++) { | ||
| elem = elem.next; | ||
| } | ||
| elem.volat = isVolat; | ||
|
|
||
| function setVideoVolatile(socket, video, pos, isVolat) { | ||
| video.volat = isVolat; | ||
|
|
||
| DefaultLog.info(events.EVENT_ADMIN_SET_VOLATILE, | ||
| "{mod} set {title} to {status}", | ||
| { mod: getSocketName(socket), type: "playlist", title: decodeURIComponent(elem.videotitle), status: isVolat ? "volatile" : "not volatile" }); | ||
| { mod: getSocketName(socket), type: "playlist", title: decodeURIComponent(video.videotitle), status: isVolat ? "volatile" : "not volatile" }); | ||
|
|
||
| io.sockets.emit("setVidVolatile", { | ||
| pos: pos, | ||
| volat: isVolat | ||
| }); | ||
| } | ||
| function setVideoColorTag(pos, tag, volat) { | ||
| var elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < pos; i++) { | ||
| elem = elem.next; | ||
| } | ||
| _setVideoColorTag(elem, pos, tag, volat); | ||
| } | ||
| function _setVideoColorTag(elem, pos, tag, volat) { | ||
|
|
||
| function setVideoColorTag(elem, pos, tag, volat) { | ||
|
|
||
| if (tag == false) { | ||
| delete elem.meta.colorTag; | ||
|
|
@@ -1342,41 +1398,6 @@ function sendToggleables(socket) { | |
| socket.emit("setToggleables", data); | ||
| } | ||
|
|
||
| function getVideoPosition(node) { | ||
| let video = SERVER.PLAYLIST.first; | ||
|
|
||
| for (let index = 0; index < SERVER.PLAYLIST.length; index++) { | ||
| if (video === node) { | ||
| return index; | ||
| } | ||
|
|
||
| video = video.next; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| function getVideoAt(index) { | ||
| if (index < 0 || index > SERVER.PLAYLIST.length) { | ||
| return null; | ||
| } | ||
|
|
||
| let video = SERVER.PLAYLIST.first; | ||
|
|
||
| for (let i = 0; i < SERVER.PLAYLIST.length; i++) { | ||
| if (i === index) { | ||
| return { | ||
| position: index, | ||
| node: video | ||
| }; | ||
| } | ||
|
|
||
| video = video.next; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function saveToHistory(node) { | ||
| const historyQuery = "insert into videos_history (videoid, videotitle, videolength, videotype, date_added, meta) values (?,?,?,?,NOW(),?)"; | ||
| const historyQueryParams = [ | ||
|
|
@@ -2746,29 +2767,19 @@ io.sockets.on('connection', function (ioSocket) { | |
| return; | ||
| } | ||
|
|
||
| if (data.from == data.to) { return; } //wat. | ||
| if (data.from < 0 || data.to < 0) { return; } //wat. | ||
| var elem = SERVER.PLAYLIST.first; | ||
| var fromelem, toelem; | ||
| for (var i = 0; i < SERVER.PLAYLIST.length; i++) { | ||
| if (i == data.from) { | ||
| fromelem = elem; | ||
| break; | ||
| } | ||
| elem = elem.next; | ||
| } | ||
| if (data.sanityid && elem.videoid != data.sanityid) { return doorStuck(socket); } | ||
| elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < SERVER.PLAYLIST.length; i++) { | ||
| if (i == data.to) { | ||
| toelem = elem; | ||
| break; | ||
| } | ||
| elem = elem.next; | ||
| const [fromelem, toelem] = SERVER.PLAYLIST.multiple([data.from, data.to]); | ||
|
|
||
| if (data.sanityid && fromelem.videoid !== data.sanityid) { | ||
| return doorStuck(socket); | ||
| } | ||
|
|
||
| SERVER.PLAYLIST.remove(fromelem); | ||
| if (data.to > data.from) { SERVER.PLAYLIST.insertAfter(toelem, fromelem); } | ||
| else { SERVER.PLAYLIST.insertBefore(toelem, fromelem); } | ||
| if (data.to > data.from) { | ||
| SERVER.PLAYLIST.insertAfter(toelem, fromelem); | ||
| } | ||
| else { | ||
| SERVER.PLAYLIST.insertBefore(toelem, fromelem); | ||
| } | ||
|
|
||
| io.sockets.emit("sortPlaylist", data); | ||
|
|
||
|
|
@@ -2782,28 +2793,18 @@ io.sockets.on('connection', function (ioSocket) { | |
| return; | ||
| } | ||
|
|
||
| let prev = null; | ||
| let next = null; | ||
|
|
||
| let video = SERVER.PLAYLIST.first; | ||
| for (let index = 0; index < SERVER.PLAYLIST.length; index++) { | ||
| if (video === SERVER.ACTIVE) { | ||
| prev = {node: video, position: index}; | ||
| } | ||
|
|
||
| if (index === data.index) { | ||
| next = {node: video, position: index}; | ||
| } | ||
|
|
||
| if (next && prev) { | ||
| break; | ||
| } | ||
| const prev = { | ||
| node: SERVER.ACTIVE, | ||
| position: SERVER.PLAYLIST.indexOf(video => video.videoid === SERVER.ACTIVE.videoid) | ||
| }; | ||
|
|
||
| video = video.next; | ||
| } | ||
| const next = { | ||
| node: SERVER.PLAYLIST.at(data.index), | ||
| position: data.index | ||
| }; | ||
|
|
||
| //check if we actually got both | ||
| if (!next || !prev) { | ||
| if (!next.node || prev.position === -1) { | ||
| return doorStuck(socket); | ||
| } | ||
|
|
||
|
|
@@ -2812,7 +2813,7 @@ io.sockets.on('connection', function (ioSocket) { | |
| } | ||
|
|
||
| if (!prev.node.volat && 'colorTagVolat' in prev.node.meta) { | ||
| _setVideoColorTag(prev.node, prev.position, false, false); | ||
| setVideoColorTag(prev.node, prev.position, false, false); | ||
| } | ||
|
|
||
| SERVER.ACTIVE = next.node; | ||
|
|
@@ -2834,7 +2835,7 @@ io.sockets.on('connection', function (ioSocket) { | |
| return; | ||
| } | ||
|
|
||
| const video = getVideoAt(data.index); | ||
| const video = SERVER.PLAYLIST.at(data.index); | ||
|
|
||
| if (video.node.videoid !== data.sanityid) { | ||
| return doorStuck(socket); | ||
|
|
@@ -3076,11 +3077,11 @@ io.sockets.on('connection', function (ioSocket) { | |
| }); | ||
| socket.on("fondleVideo", function (data) { | ||
| // New abstraction for messing with video details | ||
| var elem = SERVER.PLAYLIST.first; | ||
| for (var i = 0; i < data.info.pos; i++) { | ||
| elem = elem.next; | ||
| const video = SERVER.PLAYLIST.at(data.info.pos); | ||
|
|
||
| if (data.sanityid && video.videoid != data.sanityid) { | ||
| return doorStuck(socket); | ||
| } | ||
| if (data.sanityid && elem.videoid != data.sanityid) { return doorStuck(socket); } | ||
|
|
||
| if ("action" in data) { | ||
| if (data.action == "setVolatile") { | ||
|
|
@@ -3090,9 +3091,7 @@ io.sockets.on('connection', function (ioSocket) { | |
| return; | ||
| } | ||
|
|
||
| pos = data.pos; | ||
| isVolat = data.volat; | ||
| setVideoVolatile(socket, pos, isVolat); | ||
| setVideoVolatile(socket, video, data.pos, data.volat); | ||
| } | ||
| if (data.action == "setColorTag") { | ||
| data = data.info; // Drop action name. | ||
|
|
@@ -3101,10 +3100,12 @@ io.sockets.on('connection', function (ioSocket) { | |
| return; | ||
| } | ||
|
|
||
| pos = ("pos" in data ? data.pos : 0); | ||
| tag = ("tag" in data ? data.tag : false); | ||
| volat = ("volat" in data ? data.volat : false); | ||
| setVideoColorTag(pos, tag, volat); | ||
| setVideoColorTag( | ||
| video, | ||
| data.pos, | ||
| data.tag, | ||
| data.volat | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest making sure that this method has a verb in the name (unless this name comes from a standard somewhere?). It's fairly clear what it does at the callsites, but it took a second to figure out what it did when I first saw this line. Something like
getMultipleByIndexor something.super-nitpicky optimization suggestions:
Setinstead ofMapto save a few array allocationsSet