-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
223 lines (198 loc) · 8.86 KB
/
server.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
223
var express = require('express'),
nko = require('nko')('ifZu/MT8VJF/wtlB');
var app = express.createServer();
var io = require('socket.io').listen(app);
var port = 7777;
app.configure(function() {
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use('/media', express.static(__dirname + '/media'));
app.use('/', express.static(__dirname + '/templates/'));
});
app.configure('production', function() {
app.set('log level', 1);
app.use(express.errorHandler());
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.listen(port);
console.log(" `,,,,, ,,, ,, ,,` ,,, `,, \n `...., ,., .. ,.` ,., `.. \n `::::, ,., :: ,.` ,., `.. \n ,,, `,, ,,,,,,,, ,,,,,,,, ,,,,,,,, ,,,,, :,,,,,,: ,,,,,,,, :,,,,,,: \n ,,, `,, ,,,,,,,, ,,,,,,,, ,,,,,,,, ,,,,, :,,,,,,: ,,,,,,,, :,,,,,,: \n `::::, ,,, ::` :: ,,` ,,, `:: ,:: ,,,::` :: :,: `,, ::,,: \n `,,,,, ,,, ,,` ,, ,,` :,, `,, ,,, ,,,,,` ,, :,: `,, ,,,,: \n `::::, ,,: ,,` ,, ,,` :,, `,, ,,: ,::::` ,, :,: `,, ::::: \n ::, `:: ,:: ::` :: ::` ::, `:: ,::::` :: ::: `:: ::::: \n ::, `:: ,:: ::` :: ::` ::, `:: ,::::` :: ::: `:: ::::: \n `::::, ,::::::: :: ,:: :::::::, ::::: :::::::: ::, ::::::: \n `::::, ,::::::: :: ,:: :::::::, ::::: :::::::: ::, ::::::: \n `::::, ,::::::: :: ,:: :::::::, ::::: :::::::: ::, ::::::: \n");
console.log('eightbitbeats.com! Listening on port ' + app.address().port);
var TRACK_COUNT = 8;
var STEP_COUNT = 64;
var users = {};
var tracks = {};
for(var i = 0; i < TRACK_COUNT; i++) {
var trackID = 'track' + i
tracks[trackID] = {
instrument:null,
user:null,
steps:[],
timestamp: null,
release: function(data, userTrack) {
// data = {'trackID': 'track0' }
if (userTrack != null && userTrack == data.trackID) {
// clear ownership of track
socket.track = null
tracks[userTrack].user = null;
tracks[userTrack].instrument = null;
tracks[userTrack].clearSteps();
socket.broadcast.emit('release', data);
console.log('released: ' + data.trackID);
}
},
clearSteps: function() {
for (var sCnt=0; sCnt < STEP_COUNT; sCnt++) {
this.steps[sCnt] = {'notes': []};
}
}
};
for(var j = 0; j < STEP_COUNT; j++) {
tracks[trackID].steps[j] = {'notes': []};
}
}
tracks.getClaimed = function() {
var trackID;
var claimedTracks = {};
for(var i = 0; i < TRACK_COUNT; i++) {
trackID = 'track' + i;
if (tracks[trackID].user != null) {
claimedTracks[trackID] = tracks[trackID];
}
}
return claimedTracks;
};
tracks.releaseClaimed = function(userSocket) {
// clear ownership of track if we own one
if (userSocket.track != null) {
tracks[userSocket.track].user = null;
tracks[userSocket.track].instrument = null;
tracks[userSocket.track].clearSteps();
userSocket.broadcast.emit('release', {'trackID': userSocket.track});
userSocket.track = null;
}
};
function disconnectUser(userSocket, data) {
tracks.releaseClaimed(userSocket);
if (userSocket.username != null && users[userSocket.username] != undefined) {
console.log(userSocket.username + ' logged out!');
delete users[userSocket.username];
}
}
io.sockets.on('connection', function(socket) {
//----------- LOGIN ------------
socket.on('login', function(data) {
// add double username check
if(!data.name.match(/^[a-zA-Z0-9_]{3,16}$/)) {
socket.emit('err', {'msg': "Please choose a username that's alphanumeric and up to 16 characters long. Underscores are ok too."});
return;
}
else if(users[data.name] != undefined) {
socket.emit('err', {'msg': "Sorry, but that username is already being used by someone"});
return;
}
if (socket.username == null) {
socket.username = data.name;
users[data.name] = data.name;
// sync new user's tracks
socket.emit('sync', {'tracks': tracks.getClaimed(), 'user': data});
console.log(data.name + ' logged in!');
}
});
//----------- SYNC ------------
socket.on('sync', function(data) {
socket.emit('sync', tracks.getClaimed());
});
//----------- DISCONNECT ------------
socket.on('disconnect', function(data) {
disconnectUser(socket, data);
});
//----------- CHANGE ------------
socket.on('change', function(data) {
// Takes in changes to a step in a track
// {track: 'track1', step: 3, notes: [0,0,0,...]}
if (socket.track != null && data.track == socket.track) {
tracks[data.track].steps[data.step].notes = data.notes;
socket.broadcast.emit('change', data);
}
});
//----------- CLAIM ------------
socket.on('claim', function(data) {
// check if we already own a track
if (socket.track != null) {
socket.emit('err', {'msg': 'You can only control one track at a time!'});
return;
}
// assign a track id
var trackID = undefined;
for(var i = 0; i < TRACK_COUNT; i++) {
trackID = 'track' + i;
if (tracks[trackID].user == null) {
tracks[trackID].user = data.user;
tracks[trackID].instrument = data.instrument;
break;
}
trackID = undefined;
}
if (trackID != undefined) {
socket.track = trackID;
console.log('assigned ' + trackID);
// broadcast claim call to everyone including claimer
var claimTimestamp = +new Date();
tracks[trackID].timestamp = claimTimestamp;
var return_data = {
'trackID': trackID,
'user': data.user,
'timestamp': claimTimestamp,
'instrument': data.instrument
};
io.sockets.emit('claim', return_data);
}
// all tracks taken
else {
socket.emit('err', {'msg': 'Sorry all tracks are currently occupied by other users :('});
}
});
//----------- RELEASE ------------
socket.on('release', function() {
tracks.releaseClaimed(socket);
});
//----------- INSTRUMENT ------------
socket.on('instrument', function(data) {
// TODO update server track owner data
if (socket.track != null && socket.track == data.trackID) {
console.log(data.trackID + ' instrument changed to ' + data.instrument.name);
if (data.instrument.sounds.length != tracks[data.trackID].instrument.sounds.length) {
tracks[data.trackID].clearSteps();
}
tracks[data.trackID].instrument = data.instrument;
socket.broadcast.emit('instrument', data);
}
});
//------------ CHAT --------------
socket.on('chat', function(data) {
if (socket.username != null) {
socket.broadcast.emit('chat', {'username': socket.username, 'content': data.content});
}
});
//----------- ADMIN --------------
socket.on('admindc', function(data) {
if (data.password == 'g4m3ch4ng3r') {
for (var i=0; i < TRACK_COUNT; i++) {
var trackID = 'track' + i;
if (tracks[trackID] != undefined && tracks[trackID].user != null && tracks[trackID].user.name == data.username) {
// TODO, refactor this to ensure DRY
if (socket.username != data.username) {
tracks[trackID].user = null;
tracks[trackID].instrument = null;
tracks[trackID].clearSteps();
io.sockets.emit('release', {'trackID': trackID});
console.log('** ' + socket.username + ' KICKED ' + data.username);
}
}
}
}
});
});