-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.js
197 lines (156 loc) · 5.98 KB
/
socket.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
var services = require('./services.js')();
module.exports = function(app, rooms, rdio, host) {
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('join', function(roomName, firstName, userId) {
console.log("user joined room: "+roomName);
socket.join(roomName);
socket.set('room', roomName);
socket.set('name', firstName);
socket.set('userId', userId);
var clients = io.sockets.clients(roomName);
console.log("users in room "+roomName+": "+clients.length);
var room = rooms.get(roomName);
room.users[socket.id] = firstName;
room.userCount++;
if (!userId)
room.guestCount++;
room.points[socket.id] = 100;
if (room.currentTrack)
socket.emit('djPlayedTrack', room.currentTrack);
socket.broadcast.to(roomName).emit('userJoin', socket.id, firstName);
socket.emit('loadUsers', room.users);
if (room.chatMessages)
socket.emit('loadChat', room.chatMessages);
for(var i = clients.length; i--;) {
var client = clients[i];
console.log(client.id);
client.emit('updatePoints', room.points[client.id]);
}
});
socket.on('playTrack', function(trackKey) {
socket.get('room', function(error, roomName) {
var room = rooms.get(roomName);
room.currentTrack = trackKey;
console.log("current track for room: "+trackKey);
socket.broadcast.to(roomName).emit('djPlayedTrack', trackKey);
});
});
socket.on('placeBid', function(bidAmount) {
socket.get('room', function(error, roomName) {
socket.get('userId', function(error, userId) {
var room = rooms.get(roomName);
var clients = io.sockets.clients(roomName);
if(userId in room.bids) return false; //double bidding D:
if(room.points[socket.id] < bidAmount) bidAmount = room.points[socket.id];
room.points[socket.id] -= bidAmount;
for(var i = clients.length; i--;) {
var client = clients[i];
console.log(client.id)
client.emit('updatePoints', room.points[client.id]);
}
room.bidTotal = parseInt(room.bidTotal)+parseInt(bidAmount);
room.usersBid++;
console.log("current bid total for room: "+room.bidTotal);
socket.broadcast.to(roomName).emit('bidPlaced', room.bidTotal);
room.bids[userId] = bidAmount;
if (!room.currentTrack) {
var userStreamCount = (room.userCount - room.guestCount);
if (userStreamCount <= 2) {//if only two users, both need to bid
if (userStreamCount == room.usersBid)
setDJ();
} else if (room.usersBid >= userStreamCount/2) {
setDJ();
}
}
});
});
});
var setDJ = function() {
socket.get('room', function(error, roomName) {
var room = rooms.get(roomName);
//socket.broadcast.to(roomName).emit('unsetDJ');
//socket.emit('unsetDJ');
socket.broadcast.to(roomName).emit('resetBid');
socket.emit('resetBid');
var topBidAmount = -1,
topBidUser = '';
console.log(room.bids);
for(var bid in room.bids) {
if(room.bids[bid] > topBidAmount) {
topBidAmount = room.bids[bid];
topBidUser = bid;
}
}
room.currentDJ = topBidUser;
room.wonBid = topBidAmount;
console.log(topBidUser);
socket.broadcast.to(roomName).emit('setDJ', topBidUser);
console.log("set dj "+topBidUser);
socket.emit('setDJ', topBidUser);
room.bidTotal = 0;
room.usersBid = 0;
room.bids = {};
});
};
socket.on('songEnded', function() {
socket.get('room', function(error, roomName) {
var clients = io.sockets.clients(roomName);
var room = rooms.get(roomName);
room.currentTrack = null;
room.points[socket.id] += (room.wonBid !== 0) ? (room.wonBid*2)-10 : 0;
console.log('song end, awarding cake')
setDJ();
for(var i = clients.length; i--;) {
var client = clients[i];
room.points[client.id] += 10;
client.emit('updatePoints', room.points[client.id]);
}
});
});
socket.on('vote', function(direction) {
socket.get('room', function(error, roomName) {
var room = rooms.get(roomName);
var clients = io.sockets.clients(roomName);
if(direction !== 1 && direction !== -1) return false; //NOPE
room.score += direction;
var negativeThreshold = ~((clients.length/2)-1);
if(negativeThreshold > 0) negativeThreshold = 0;
if(room.score <= negativeThreshold) {
//kick them out!
setDJ();
room.points[room.currentDJ] -= 10;
for(var i = clients.length; i--;) {
var client = clients[i];
room.points[client.id] += 10;
client.emit('updatePoints', room.points[client.id]);
}
}
socket.emit('updateVotes', room.score);
});
});
socket.on('sendMessage', function(name, message) {
socket.get('room', function(error, roomName) {
var room = rooms.get(roomName);
room.chatMessages.push({ name: name, message: message });
socket.broadcast.to(roomName).emit('messageReceived', name, message);
});
});
socket.on('disconnect', function() {
console.log("disconnect");
socket.get('room', function(err, roomName) {
socket.get('userId', function(err, userId) {
if (roomName) {
console.log("user left room: "+roomName);
var room = rooms.get(roomName);
delete room.users[socket.id];
room.userCount--;
if (!userId)
room.guestCount--;
socket.broadcast.to(roomName).emit('userLeave', socket.id);
}
});
});
});
});
};