-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.coffee
126 lines (105 loc) · 3.39 KB
/
Queue.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
_ = require 'underscore'
events = require 'events'
class Queue extends events.EventEmitter
constructor: () ->
@videos = []
@currentVideo = null
@startedVideoOn = null
@setMaxListeners 0
@connections = 0
@timeoutId = null
addVideo: (entry) =>
if @currentVideo is null
@setCurrentVideo entry
else
@videos.push entry
@emit 'update'
shiftTopVideo: () =>
topVideoId = @getTopList()[0].id
newVideoList = []
topVideo = null
_.each @videos, (element) =>
if element.id is topVideoId
topVideo = element
else
newVideoList.push element
@videos = newVideoList
return topVideo
setCurrentVideo: (entry) =>
if entry is null
@currentVideo = null
@startedVideoOn = null
@emit 'skipCount', 0, []
@emit 'noVideo'
return
@currentVideo = entry
@startedVideoOn = new Date().getTime()
@timeoutId = setTimeout () =>
console.log 'timeout skip'
@skipToNextVideo()
console.log 'timeout skip done'
, (entry.videoLength * 1000)
@emit 'currentVideo'
@emit 'update'
@updateSkip()
getSortedList: (sortFunction) =>
listSorted = _.sortBy @videos, sortFunction
list = []
_.each listSorted, (element) =>
list.push element.getElement()
return list
getTopList: () =>
return @getSortedList (a) ->
return -a.getScore()
getRecentList: () =>
return @getSortedList (a) ->
return -a.timestamp
getQueue: () =>
top = @getTopList().slice 0,5
recent = @getRecentList().slice 0,5
return {type: "updatePlaylist", content: {top: top, recent: recent}}
skipCurrent: (user) =>
if @currentVideo?
@currentVideo.skip(user)
@updateSkip()
updateSkip: () =>
skipsNeeded = Math.ceil(@connections/2)
if @currentVideo.skipsters.length >= skipsNeeded
#skip
@skipToNextVideo()
else
@emit 'skipCount', skipsNeeded, @currentVideo.skipsters
updateConnectionNumber: (count) =>
@connections = count
skipToNextVideo: () =>
console.log 'skip debug'
if @timeoutId?
console.log 'delete id '
clearTimeout @timeoutId
@timeoutId = null
if @videos.length
newEntry = @shiftTopVideo()
@setCurrentVideo newEntry
else
@setCurrentVideo null
hasCurrentVideo: () =>
return @currentVideo isnt null
getCurrentVideo: () =>
if @currentVideo is null
return {type: "loadVideo", content: null}
position = new Date().getTime()
position = position - @startedVideoOn
position = position - @currentVideo.videoLength
position = Math.round position / 1000
content = _.clone @currentVideo
content.position = position
return {type: "loadVideo", content: content}
getVideoById: (id) =>
video = null
_.each @videos, (element) =>
if element.id is id
video = element
return video
sendPlaylistUpdate: () =>
@emit 'update'
module.exports = Queue