-
Notifications
You must be signed in to change notification settings - Fork 0
/
playercmds.js
283 lines (228 loc) · 5.89 KB
/
playercmds.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var globals = require('./globals.js');
var helpers = require('./helpers.js');
var votehelpers = require('./votehelpers.js');
exports.response = function(socket)
{
var id;
if(globals.DEBUG)console.log('a player connected ');
socket.emit("whoareyou", "?")
socket.on('hello', function(msg)
{
var usrobj = Object.assign({},globals.usrobj);
usrobj.colSeed = Math.random();
usrobj.colMode = Math.floor(Math.random() * 4);
usrobj.blobSeed = Math.random();
usrobj.voiceNum = Math.floor(Math.random() * 8);
usrobj.voicePan = -1 + Math.random() * 2;
usrobj.voicePitch = 0.75 + Math.random() * 0.5;
//TODO this would work better with 15 * 15 and indexed permutations (guaranteed difference)
usrobj.font = globals.fonts[Math.floor(Math.random() * globals.fonts.length)];
usrobj.fontCol = globals.fontColours[Math.floor(Math.random() * globals.fontColours.length)];
if(msg == "new")
{
globals.UserData.insert(usrobj,{}, function(err,res)
{
if(err)
{
console.log(err);
//throw err;
}
if(globals.DEBUG)console.log('hello new user: ' + res._id);
id = res._id;
socket.join(res._id);
socket.emit('welcome', res);
globals.checkins[id] = Date.now();
globals.sockets[res._id] = socket; //store socket on global list
});
}
else
{
globals.UserData.findOne(msg,{}, function(err,res)
{
if(err)
{
console.log(err);
//throw err;
}
if(!res)
{
//insert a new user instead
globals.UserData.insert(usrobj, {}, function(err,res)
{
if(err) throw err;
if(globals.DEBUG)console.log('hello new user: ' + res._id);
id = res._id;
globals.checkins[id] = Date.now();
socket.join(res._id);
socket.emit('welcome', res);
globals.sockets[res._id] = socket; //store socket on global list
});
}
else
{
id = res._id;
if(globals.DEBUG)console.log('welcome back user: ' + id);
res.connected = true;
socket.join(res._id);
globals.checkins[id] = Date.now();
//join any exitsting Rooms
for(var i = 0; i < res.rooms.length; i++)
{
//if(globals.DEBUG)console.log("joining " + res.rooms[i]);
socket.join(res.rooms[i]);
}
globals.UserData.update( id,{$set: {connected: true}}, {}, function(){
socket.emit('welcome', res);
});
//NB . overwrites if necessary
globals.sockets[res._id] = socket; //store socket on global list
}
});
}
});
socket.on('voted', function(msg)
{
/*voted { pair: [ 'recognise', 'refute' ],
type: 'Vobj',
scores: [ 0, 1 ],
voting: [],
voted: [],
notvoted: [] }*/
if(typeof(msg) != "object")
{
console.log("null msg");
return;
}
msg.id = helpers.validateId(msg.id);
if(!msg.id)
{
console.log("incorrect msg id: " + msg.id);
return;
}
else if(!helpers.validateId(id))
{
console.log("incorrect user id: " + id);
return;
}
var usrobj;
var p = globals.UserData.findOne({_id: String(id)});
p = p.then((data)=>
{
usrobj = data;
return globals.Votes.findOne({_id: msg.id});
})
p = p.then((data)=>
{
data._id = helpers.validateId(data._id);
if(data == null)
{
return Promise.reject("vote " + msg.id + " could not be found ");
}
else if(!data._id)
{
return Promise.reject("vote " + msg.id + " return invalid id");
}
data.scores[msg.choice] += 1.0/data.population;
globals.udpPort.send({
address: "/speakPhrase",
args: [String(data._id), msg.choice, usrobj.voiceNum, usrobj.voicePan, usrobj.voicePitch]
}, "127.0.0.1", 57120);
globals.display.emit('cmd', {
type: "vote", cmd: "displayVote" ,
val: {
choice: msg.choice,
text: data.pair[msg.choice],
font: usrobj.font,
col: usrobj.fontCol,
score: data.scores,
pos: data.pos,
slots: globals.voteDisplaySlots //Indicates the current state of the slots
}
});
return globals.Votes.update(
{
_id: data._id},
{$push: {voted: id},
$pull: {voting: id},
$set:{scores: data.scores}
});
})
p = p.then((data)=>
{
return globals.Votes.findOne({_id: msg.id});
})
p = p.then((data)=>
{
if(data.voted.length == data.population)
{
//resolve the vote if there are no voters left
votehelpers.concludeVote(data);
}
else
{
//initiate the next voter if there is one
votehelpers.sendVote(data);
}
return globals.UserData.update(id,{$set: {currentVoteId: -1, currentVotePair: ["",""]}});
});
p.catch((reason)=>
{
console.log("Error - voted " + reason) ;
})
})
socket.on('update_user', function(msg)
{
if(helpers.validateId(msg._id))
{
globals.UserData.update({_id: msg._id},{$set: msg})
.catch((err)=>{
console.log("Error - update_user: " + err);
});
}
});
socket.on('splat', function(msg){
//if(globals.DEBUG)console.log("splat", msg);
var args = ["pan", msg.splatPan, "rate", msg.splatRate, "pos", msg.splatPos];
globals.udpPort.send({
address: "/splat",
args: args,
}, "127.0.0.1", 57120);
globals.display.emit('cmd', {type: "splat", val: msg});
});
socket.on('moveBlob', function(msg){
//TODO OSC to supercollider here
globals.display.emit('cmd', {type: "moveBlob", val: msg});
});
socket.on('disconnect', function()
{
if(globals.DEBUG)console.log('a player disconnected ' + id);
try
{
if(helpers.validateId(id))
{
globals.UserData.update({_id: id},{$set: {connected: false}})
.catch((err)=>{
console.log("Error - disconnect: " + err);
});
delete globals.checkins[id];
clearInterval(globals.checkinProcs[id]);
delete globals.checkinProcs[id];
}
}
catch(e)
{
console.log("Error - disconnect" + e);
}
});
//a process to check players are with us
globals.checkinProcs[id] = setInterval(function()
{
socket.emit('checkAlive',function(data)
{
if(data != null)
{
globals.checkins[data] = Date.now();
}
});
},5000);
}