-
Notifications
You must be signed in to change notification settings - Fork 1
/
room.js
222 lines (186 loc) · 4.61 KB
/
room.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var MAX_DJS = 5;
function Room(buoy, name) {
this.buoy = buoy;
this.name = name;
this.peers = [];
this.djs = [];
this.activeDJ = -1; // Val is an index in this.djs
this.activeTrack = null;
this.trackStartTime = null;
console.log("[debug] Creating new room "+ name);
}
util.inherits(Room, EventEmitter);
/*
* Peer object serialization
*/
function peerData(peer) {
return {
id: peer.id,
name: peer.name,
gravatar: peer.gravatar,
votes: peer.vote
};
}
function peerToId(peer) {
return peer.id;
}
/*
* Adds a peer to the room
*/
Room.prototype.join = function(peer) {
this.peers.push(peer);
this.sendAllBut(peer, "peerJoined", peerData(peer));
peer.send("roomData", {
name: this.name,
peers: this.peers.map(peerData),
djs: this.djs.map(peerToId),
activeDJ: this.activeDJ,
activeTrack: this.activeTrack,
currentTime: this.getCurrentTime()
});
};
/*
* Removes a peer from the room
*/
Room.prototype.leave = function(peer) {
var i = this.peers.indexOf(peer);
this.peers.splice(i, 1);
console.log("[debug] "+ peer.name +" left "+ this.name);
this.removeDJ(peer);
// If the last peer left, delete the room
if(this.peers.length == 0) {
this.buoy.deleteRoom(this.name);
return;
}
this.sendAllBut(peer, "peerLeft", {
id: peer.id
});
}
/*
* Sends out a chat to all users
*/
Room.prototype.sendChat = function(from, message) {
this.sendAllBut(from, "chat", { msg: message, from: from.id });
}
/*
* Sends a message to all peers in the room
*/
Room.prototype.sendAll = function(event, data) {
for(var i = 0; i < this.peers.length; i++) {
this.peers[i].send(event, data);
}
}
/*
* Sends a message to all peers except for the given ID
*/
Room.prototype.sendAllBut = function(but, event, data) {
for(var i = 0; i < this.peers.length; i++) {
if(this.peers[i] == but) continue;
this.peers[i].send(event, data);
}
}
/*
* Adds a peer to the DJ list
*/
Room.prototype.addDJ = function(peer) {
if (this.djs.indexOf(peer) != -1) return;
if(this.djs.length >= MAX_DJS) {
return false;
}
this.djs.push(peer);
// Notify peers
this.sendAll("newDJ", {
id: peer.id
});
if(this.djs.length == 1) {
this.setActiveDJ(peer);
}
};
/*
* Removes a user from the DJ list
*/
Room.prototype.removeDJ = function(peer) {
var i = this.djs.indexOf(peer);
if (i == -1) return;
this.djs.splice(i, 1);
// Notify peers
this.sendAll("removeDJ", {
id: peer.id
});
if(this.activeDJ == i) {
this.skip();
} else if(this.activeDJ > i) {
this.activeDJ--;
}
};
/*
* Sets the given peer to the active DJ of the room
*/
Room.prototype.setActiveDJ = function(peer) {
var i = this.djs.indexOf(peer);
if(i == -1) {
this.sendAll("setActiveDJ", { peer: null });
return;
}
this.activeDJ = i;
this.sendAll("setActiveDJ", {
peer: peer.id,
});
};
/*
* Skip the active DJ and their track
*/
Room.prototype.skip = function() {
var nextDJ = this.djs[this.activeDJ + 1] || this.djs[0];
this.setActiveDJ(nextDJ);
this.setActiveTrack(null);
};
/*
* Get the current DJ of the room
*/
Room.prototype.getActiveDJ = function() {
return this.djs[this.activeDJ];
};
/*
* Sets the active track of the room
*/
Room.prototype.setActiveTrack = function(track) {
if (!track) {
// if the track is being cleared, clear the start time
this.trackStartTime = null;
} else if (!this.activeTrack) {
// if this is a new track, reset the start time
this.trackStartTime = new Date();
}
this.activeTrack = track;
this.sendAll("setActiveTrack", {
track: track
});
// Reset user votes
this.peers.forEach(function(peer) {
peer.vote = 0;
});
};
/*
* Sets the duration (ms) of the active track of the room
*/
Room.prototype.setActiveTrackDuration = function(duration) {
if (!this.activeTrack) {
console.error("Got active track duration without active track");
return;
}
this.activeTrack.duration = duration;
this.sendAll("setDuration", {
duration: duration
});
};
/*
* Gets the time (ms) into the current track
* Returns null if there is no current track or start time is unknown
*/
Room.prototype.getCurrentTime = function() {
return this.trackStartTime && (new Date() - this.trackStartTime);
};
exports.Room = Room;