-
Notifications
You must be signed in to change notification settings - Fork 0
/
admincmds.js
718 lines (636 loc) · 18.1 KB
/
admincmds.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
var globals = require('./globals.js');
var helpers = require('./helpers.js');
var votehelpers = require('./votehelpers.js');
require('./libs/utils.js'); //just for generateTempId
exports.response = function(socket)
{
console.log('an admin connected');
socket.on('cmd', function(msg)
{
if(msg.cmd == "change_mode")
{
helpers.parseOptions(msg.args, function(options)
{
options.mode = msg.mode;
if(msg.mode == "story")
{
helpers.startStoryClip();
}
else if(msg.mode == "love")
{
globals.display.emit('cmd', {type: 'love'});
}
//handle subrooms for story
helpers.useRoom(msg, function(rm)
{
if(msg.mode == "story")
{
handleStorySubrooms(rm, options, msg.cli_id);
}
//handle display ... TODO - perhaps an override for no display switching
globals.players.to(rm).emit('cmd', {cmd: 'change_mode', value: options});
});
});
}
else if(msg.cmd == "chat_update")
{
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_update', value: msg.value});
}
else if(msg.cmd == "chat_clear")
{
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_clear'});
}
else if(msg.cmd == "chat_newline")
{
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_newline'});
}
else if(msg.cmd == "story_clear")
{
var txts = globals.story[globals.storyChapter].clips[globals.storyClip].texts;
if(txts.length > 1 && globals.storyRooms.length > 1)
{
//send the clear to room 0
globals.storyCurrText.push("");
globals.players.to(globals.storyRooms[0]).emit('cmd', {cmd: 'chat_clear'});
for(var i = 1; i < globals.storyRooms.length; i++)
{
var tidx = i%txts.length;
if(tidx == 0)
{
//send the clear
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_clear'});
}
}
}
else
{
globals.storyCurrText.push("");
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_clear'});
}
}
else if(msg.cmd == "story_update")
{
if(globals.story[globals.storyChapter].clips[globals.storyClip].texts != undefined) //check this clip has text
{
var txts = globals.story[globals.storyChapter].clips[globals.storyClip].texts;
if(txts.length > 1 && globals.storyRooms.length > 1) //we only need to bother if there are alternative texts
{
//the first room gets the original text
globals.players.to(globals.storyRooms[0]).emit('cmd', {cmd: 'chat_update', value: msg.value});
globals.storyCurrText[globals.storyCurrText.length - 1] = msg.value;
//count the characters
var num_chars = 0;
for(var i = 0; i < globals.storyCurrText.length; i++)
{
num_chars += globals.storyCurrText[i].length;
}
//TODO implement new lines for dummy text
var prog = Math.min(1.0,num_chars/(txts[0].length * 0.9)); // slightly optimisitic to account for typos etc
for(var i = 1; i < globals.storyRooms.length; i++)
{
//new line and clear will also need to work this out
var tidx = i%txts.length;
if(tidx == 0)
{
//send the original text
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_update', value: msg.value});
}
else if(num_chars > globals.storyNumChars)//alternative texts only go forwards ... delete is ignored
{
var l = prog * txts[tidx].length;
var n = txts[tidx].substring(0,l);
r = /[%$]{1}([^%^$]*?)$/;
res = r.exec(n);
//determine room to send
if(res == null)
{
//before any special char
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_update', value: n});
}
else if(res[0] == "%")
{
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_newline'});
}
else if(res[0] == "$")
{
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_clear'});
}
else
{
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_update', value: res[1]});
}
}
}
if(num_chars > globals.storyNumChars)globals.storyNumChars = num_chars;
}
else
{
//otherwise just send to all
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_update', value: msg.value});
}
}
}
else if(msg.cmd == "story_newline")
{
var txts = globals.story[globals.storyChapter].clips[globals.storyClip].texts;
//catch error here
if(txts.length > 1 && globals.storyRooms.length > 1)
{
//send the new line to room 0
globals.storyCurrText.push("");
globals.players.to(globals.storyRooms[0]).emit('cmd', {cmd: 'chat_newline'});
for(var i = 1; i < globals.storyRooms.length; i++)
{
var tidx = i%txts.length;
if(tidx == 0)
{
//send the return
globals.players.to(globals.storyRooms[i]).emit('cmd', {cmd: 'chat_newline'});
}
}
}
else
{
globals.storyCurrText.push("");
globals.players.to(msg.room).emit('cmd', {cmd: 'chat_newline'});
}
}
else if(msg.cmd == "story_next")
{
helpers.incrementStoryClip();
helpers.startStoryClip(msg.room);
}
else if(msg.cmd == "sreset")
{
globals.storyChapter = 0;
globals.storyClip = 0;
helpers.startStoryClip(msg.room);
globals.admin.emit('server_report', {id: msg.cli_id});
}
else if(msg.cmd == "sreload")
{
helpers.loadStory(function(resp)
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
});
}
else if (msg.cmd == "sgoto")
{
helpers.parseOptions(msg.args, function(options){
if(options.chapter != undefined)
{
if(!isNaN(options.chapter) && options.chapter != '')
{
globals.storyChapter = Math.max(0,options.chapter);
globals.storyClip = 0;
helpers.startStoryClip(msg.room);
globals.admin.emit('server_report', {id: msg.cli_id, msg: "chapter " + options.chapter + " : " + globals.story[options.chapter].name});
return;
}
else {
for(var i = 0; i < globals.story.length; i++)
{
if(globals.story[i].name == options.chapter)
{
globals.storyChapter = i;
globals.storyClip = 0;
helpers.startStoryClip(msg.room);
globals.admin.emit('server_report', {id: msg.cli_id, msg: "chapter " + i + " : " + globals.story[i].name});
return;
}
}
}
}
globals.admin.emit('server_report', {id: msg.cli_id, msg: "chapter not found"});
})
}
else if(msg.cmd == "lplayers")
{
listPlayers( msg.args, msg.room, function(r)
{
globals.admin.emit('server_report', {id: msg.cli_id, room: msg.room, isproc: msg.isproc , msg: r}); //same room response
})
}
else if(msg.cmd == "lrooms")
{
listRooms( msg.args, msg.room, function(r)
{
globals.admin.emit('server_report', {id: msg.cli_id, room: msg.room, isproc: msg.isproc , msg: r}); //same room response
})
}
else if(msg.cmd == "close")
{
globals.Rooms.find({room: msg.room}).then((docs)=>
{
if(docs.length > 0)
{
for(var i =0; i < docs[0].population.length; i++)
{
//NB. the player might have disappeared
if(globals.sockets[docs[0]['population'][i]] != undefined)
{
globals.sockets[docs[0]['population'][i]].leave(docs[0]['room']); // all leave the room
}
}
globals.Rooms.remove({room: msg.room});
globals.UserData.update({},{$pull: {rooms: msg.room}},{multi: true} );
globals.admin.emit('server_report', {id: msg.cli_id , msg: "room: " + msg.room + " removed", room: "" });
}
else
{
globals.admin.emit('server_report', {id: msg.cli_id , msg: "room: " + msg.room + " can't be found", room: "" });
}
});
}
else if(msg.cmd == "closeall")
{
closeAll(function(){
globals.admin.emit('server_report', {id: msg.cli_id , msg: "all rooms removed" });
})
}
else if(msg.cmd == "get_rooms")
{
globals.Rooms.find({}).then((docs)=>
{
if(docs.length < 1)
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: "there are no rooms"});
}
else
{
var res = [];
for(var i = 0; i < docs.length; i++)
{
res.push(docs[i].room);
}
globals.admin.emit('server_report', {id: msg.cli_id, suslist: res, susmode: "room", selected: msg.room});
}
});
}
else if(msg.cmd == "create_room")
{
helpers.useRoom(msg, null);
}
else if(msg.cmd == "sub")
{
if(msg.room == "" || msg.room == undefined)
{
globals.admin.emit('server_report', {id: msg.cli_id});
return;
}
helpers.parseOptions(msg.args, function(options)
{
if(options['rooms'] != undefined)
{
helpers.subRoom(msg.room, options['rooms'], function(r)
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: r});
})
}
});
}
else if(msg.cmd == "set")
{
helpers.parseOptions(msg.args, function(options)
{
helpers.useRoom(msg, function(rm)
{
globals.players.to(rm).emit('cmd', {cmd: 'set_params', value: options});
});
});
}
else if(msg.cmd == "cleanup")
{
//NB. disconnected users can somehow still appear as connected ... look into this
globals.UserData.find({connected: false}).then((docs)=>
{
if(docs == null)return;
var users = [];
for(var i = 0; i < docs.length; i++)
{
delete globals.sockets[docs[i]._id]; //does this work ?
delete globals.checkins[docs[i]._id];
clearInterval(globals.procs[docs[i]._id]);
globals.UserData.remove(docs[i]._id);
users.push(docs[i]._id);
}
globals.Rooms.update({},{$pull: {population: {$in: users }}},{multi: true}); //remove these users from anyglobals.Rooms
globals.admin.emit('server_report', {id: msg.cli_id, msg: users.length + " disconnected users removed "});
});
}
else if(msg.cmd == "resetall")
{
closeAll(function()
{
//clear the Databases
globals.sockets = {};
globals.UserData.remove({});
globals.Votes.remove({});
globals.voteDisplaySlots =
{
a: [0,0,0,0],
b: [0,0,0,0]
};
globals.checkins = {};
globals.admin.emit('server_report', {id: msg.cli_id, msg: "all databases reset", room: ""});
globals.players.emit('whoareyou'); //causes any connected players to reset
})
globals.display.emit("cmd", {type: "instruct"});
globals.display.emit('cmd', {type: 'clear_display'});
globals.storyChapter = 0;
globals.storyClip = 0;
var keys = Object.keys(globals.procs);
for(var i = 0; i < keys.length; i++)
{
clearInterval(globals.procs[keys[i]]);
clearTimeout(globals.procs[keys[i]]);
}
globals.udpPort.send(
{
address: "/resetPhrases",
args: []
},
"127.0.0.1", 57120);
}
else if(msg.cmd == "stats")
{
//TODO make istats
globals.UserData.find({},'connected').then((docs)=>{
var resp = "";
resp += "players: " + docs.length + "\n";
var numconnected = 0;
docs.forEach(function(e)
{
if(e.connected)numconnected += 1;
})
resp += "connected: " + numconnected;
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
});
}
else if(msg.cmd == "end")
{
globals.udpPort.send(
{
address: "/allOff",
args: []
},
"127.0.0.1", 57120);
globals.admin.emit('server_report', {id: msg.cli_id});
globals.display.emit("cmd", {type: "end"});
globals.players.emit('cmd', {cmd: 'change_mode', value: {mode: "blank"}});
}
else if(msg.cmd == "vnew")
{
votehelpers.startVote(msg)
.then((resp)=>
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
})
.catch((resp)=>{
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
});
}
else if(msg.cmd == "vjoin")
{
votehelpers.joinVotes(msg)
.then((resp)=>
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
})
.catch((resp)=>{
globals.admin.emit('server_report', {id: msg.cli_id, msg: resp});
});
}
else if(msg.cmd == "lvotes")
{
//make a list of votes
votehelpers.listVotes().then((doc)=>
{
globals.admin.emit('server_report', {id: msg.cli_id, isproc: msg.isproc , msg: doc});
})
}
//console.log('admin command: ' , msg);
});
/////////////////////////////////////////////////////////////////////
//SOUND COMMANDS
socket.on('sound_cmd', function(msg)
{
if(msg.cmd == "reloadsamples")
{
globals.udpPort.send(
{
address: "/loadSamples",
args: [globals.settings.samplePath]
},
"127.0.0.1", 57120);
globals.admin.emit('server_report', {id: msg.cli_id});
}
else if(msg.cmd == "play")
{
var options = helpers.parseOptions(msg.args);
helpers.playSound(options);
globals.admin.emit('server_report', {id: msg.cli_id});
}
else if(msg.cmd == "killsound")
{
globals.udpPort.send({
address: "/allOff",
args: []
}, "127.0.0.1", 57120);
globals.admin.emit('server_report', {id: msg.cli_id});
}
});
/////////////////////////////////////////////////////////////////////
//DISPLAY COMMANDS
socket.on('disp_cmd', function(msg)
{
if(msg.cmd == "dinstruct")
{
globals.display.emit("cmd", {type: "instruct"});
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dlove")
{
globals.display.emit("cmd", {type: "love"});
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dvote")
{
globals.display.emit("cmd", {type: "vote", cmd: "new"});
globals.voteDisplayIndexes = {};
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dstory")
{
globals.display.emit("cmd", {type: "story"});
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dclear")
{
globals.display.emit("cmd", {type: "clear"});
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dsplat")
{
if(msg.args.length > 0)
{
var id = msg.args[0];
}
else
{
var id = generateTempId(5);
}
globals.display.emit("cmd", {type: "splat", val: {_id: id,
colSeed: globals.LoveParameters.colSeed,
colMode: globals.LoveParameters.colMode,
blobSeed: Math.random(),
splatPan: (Math.random() * 2.0 - 1.0) * 0.85,
}});
globals.admin.emit('server_report', {id: msg.cli_id}); //empty response
}
else if(msg.cmd == "dtransform")
{
//transforms splats into blobs
var selector = helpers.parseFilters(msg.args, msg.room);
if(selector)
{
helpers.selectPlayers(selector, function(uids)
{
globals.UserData.find({_id: {$in: uids}, isMobile: false}).then((docs)=>
{
globals.display.emit("cmd", {type: "transform", val: docs});
globals.admin.emit('server_report', {id: msg.cli_id, msg: docs.length + " splats have been transformed"});
})
})
}
else
{
//use the room population
globals.Rooms.find({room: msg.room}).then((docs)=>
{
if(docs.length > 0)
{
globals.UserData.find({_id: {$in: docs[0].population}, isMobile: false}).then((docs2)=>
{
globals.display.emit("cmd", {type: "transform", val: docs2});
globals.admin.emit('server_report', {id: msg.cli_id, msg: docs2.length + " splats have been transformed"});
});
}
else
{
globals.admin.emit('server_report', {id: msg.cli_id, msg: "room not found"});
}
})
}
}
})
socket.on('disconnect', function()
{
console.log('an admin disconnected');
});
}
//////////////////////HELPER FUNCTIONS/////////////////////////
//TODO MOVE TO HELPERS ?
function listPlayers(args, room, cb)
{
var selector = helpers.parseFilters(args, room);
if(!selector)selector = {};
var so = helpers.generateSearchObj(selector);
//number filters don't work here
var results = "";
globals.UserData.find(so).then((docs)=>
{
for(var i = 0; i < docs.length; i++)
{
var e = docs[i];
var id = String(e._id);
var str = id.substring(0,3) + "..." + id.substring(id.length -3, id.length) ;
str += (e.connected) ? " connected " : " dormant ";
str += ", mode: " + e.mode;
if(e.mode == "love")
{
str += ", state: " + Math.round((e.state + e.state_z)*100)/100;
str += ", maxState: " + e.maxState;
str += ", isSplat: " + e.isSplat;
str += ", isMobile: " + e.isMobile;
str += ", isDying: " + e.isDying;
str += ", death: " + Math.round(e.death * 100)/100;
str += ", envTime: " + e.envTime;
}
else if(e.mode == "vote")
{
var vid = String(e.currentVoteId);
if(vid.length > 3)
{
str += ", id: " + vid.substring(0,3) + "..." + vid.substring(-3);
if(typeof(e.currentVotePair) == "object" && e.currentVotePair != null)str += ", pair: [" + e.currentVotePair[0] + ", " + e.currentVotePair[1] + "]";
}
}
results += str + "\n";
}
cb(results);
});
}
function listRooms(args, room, cb)
{
var results = "";
globals.Rooms.find({}).then((docs)=>
{
docs.forEach(function(e)
{
var str = e.room + " :: " + e.population.length;
if(e.room == room)str += " *";
results += str + "\n";
});
cb(results);
});
}
function closeAll(cb)
{
//all users leave any rooms
globals.Rooms.find({}).then((docs)=>
{
if(docs.length > 0)
{
for(var i =0; i < docs[0].population.length; i++)
{
if(globals.sockets[docs[0]['population'][i]] != undefined)
{
globals.sockets[docs[0]['population'][i]].leave(docs[0]['room']); // all leave the room
}
}
}
cb();
globals.Rooms.remove({});
globals.UserData.update({},{$set: {rooms:[]}},{multi: true} );
});
}
function handleStorySubrooms(room, options, cli_id)
{
if(options.sub != undefined)
{
helpers.subRoom(room, options.sub, function(r)
{
globals.storyRooms = [];
globals.admin.emit('server_report', {id: cli_id, msg: r});
for(var i = 0; i < options.sub; i++)
{
globals.storyRooms.push(room + "_" + i);
}
})
}
else
{
//NB. this doesn't guarantee that the right players are memebers
globals.storyRooms = [];
var re = new RegExp(room + ".+", "g");
globals.Rooms.find({room: { $regex: re }}, 'room').then((docs)=>
{
for(var i = 0; i < docs.length; i++)
{
globals.storyRooms.push(docs[i].room);
}
globals.admin.emit('server_report', {id: cli_id, msg: docs.length + " subrooms found"});
});
}
}