-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
195 lines (153 loc) · 5.26 KB
/
app.js
File metadata and controls
195 lines (153 loc) · 5.26 KB
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
var http = require('http'),
static = require('node-static'),
fs = require('fs'),
osc = require('osc-min'),
dgram = require('dgram'),
path = require('path'),
udp = dgram.createSocket('udp4');
var outport = 41234;
var file = new(static.Server)();
http.createServer(function (req, res) {
file.serve(req, res);
}).listen(8080);
var io = require('socket.io').listen(8081);
io.set("origins = *");
io.set("log level", 1); // reduce logging
function sendOSC(oscAddress, state) {
var buf;
if (oscAddress != undefined) {
var address = "/" + oscAddress;
buf = osc.toBuffer({
address: address,
args: [state]
})
// console.log(address);
return udp.send(buf, 0, buf.length, outport, "localhost");
};
};
var usernames = {};
var usernames2 = [];
var controller;
var display;
user = new Object();
user.userName = "Blah";
user.assignedButtons = [0,1];
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
var usersCount;
function sortButtons (count) {
console.log('Dividing buttons among '+count+ ' users');
switch (count) {
case 1:
return [[0, 1, 2, 3], [], [], []];
case 2:
return [[0, 1], [2, 3], [], []];
case 3:
return [[0, 1], [2], [3], []];
case 4:
return [[0], [1], [2], [3]];
default:
return [0, 1, 2, 3];
}
}
function addUser(name, assignedButtons) {
this.userName = name;
this.assignedButtons = assignedButtons;
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
console.log("User connected");
// we store the username in the socket session for this client
socket.username = username;
// set up a new user object and pre-populate it
var newUser = new addUser(username, [0, 1, 2, 3]);
console.log('New user = ' + newUser.userName);
// add the client's username to the global list
usernames[username] = newUser;
usernames2.unshift(newUser);
// recalculate number of users
usersCount = Object.size(usernames);
if (usersCount == 5) {
console.log('Removing a user');
usernames2.splice(3, 1)
delete usernames[socket.username];
usersCount = Object.size(usernames);
}
// recalculate the distribution of buttons across users based on number of connected users
var sortedButtons = sortButtons(usersCount);
// assign each connected user their buttons
for (i=0; i < usersCount; i++) {
usernames2[i].assignedButtons = sortedButtons[i];
console.log('assigning user ' + usernames2[i] + ' these buttons ' + sortedButtons[usersCount-1]);
}
console.log(usernames2);
io.sockets.socket(display).emit('userCountUpdated', usersCount);
// tell client to update its view
io.sockets.emit('assignButtons', usernames2);
});
socket.on('registerAsDisplay', function(id) {
console.log('Display registered');
display = socket.id;
})
socket.on('didAccelerate', function(tilt) {
for (var i=0; i<usersCount; i++) {
if (usernames2[i].userName == socket.username) {
sendOSC(sendOSC(i + "/accelX", tilt[0]));
sendOSC(sendOSC(i + "/accelY", tilt[1]));
sendOSC(sendOSC(i + "/accelZ", tilt[2]));
}
}
});
socket.on('buttonPressed', function(buttonIndex) {
for (var i=0; i<usersCount; i++) {
if (usernames2[i].userName == socket.username) {
console.log(usernames2[i].userName + ' pressed = ' + buttonIndex);
sendOSC(sendOSC(i + "/button" + buttonIndex, 1));
io.sockets.emit('peerButtonPressed', buttonIndex);
}
}
});
socket.on('buttonReleased', function(buttonIndex) {
for (var i=0; i<usersCount; i++) {
if (usernames2[i].userName == socket.username) {
console.log(usernames2[i].userName + ' released = ' + buttonIndex);
sendOSC(sendOSC(i + "/button" + buttonIndex, 0));
io.sockets.emit('peerButtonReleased', buttonIndex);
}
}
});
// when the user disconnects.. perform this
// TO-DO // TIDY THIS UP. LOTS OF IDENTICAL/REDUNTANT CALLS TO .connect ABOVE
socket.on('disconnect', function(){
console.log(socket.username + ' disconnected');
for (var i=0; i<usersCount; i++){
if (usernames2[i].userName == socket.username) {
usernames2.splice(i, 1);
console.log('Removing item ' + i + ' from array');
}
// remove the username from global usernames list
delete usernames[socket.username];
// recalculate number of users
usersCount = Object.size(usernames);
// recalculate the distribution of buttons across users based on number of connected users
var sortedButtons = sortButtons(usersCount);
}
// assign each connected user their buttons
for (i=0; i < usersCount; i++) {
usernames2[i].assignedButtons = sortedButtons[i];
console.log('assigning user ' + usernames2[i] + ' these buttons ' + sortedButtons[usersCount-1]);
}
// tell client to update its view
io.sockets.emit('assignButtons', usernames2);
console.log(usernames2);
io.sockets.socket(display).emit('userCountUpdated', usersCount);
});
});