-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommands.js
More file actions
1577 lines (1283 loc) · 52.7 KB
/
commands.js
File metadata and controls
1577 lines (1283 loc) · 52.7 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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Command functions are called from the server only,
* to process data requests from the client,
* and send the results by socket.
*/
//*** MODULE DEPENDENCIES ***//
//*** eval() is used, because the code is split into files for readability, only! ***//
if (require.cache[require.resolve('nodemailer')]) {
delete require.cache[require.resolve('nodemailer')];
}
var nodemailer = require('nodemailer');
// Clear cache, for a new copy.
if (require.cache[require.resolve('./operations.js')]) {
delete require.cache[require.resolve('./operations.js')];
}
var operations = require('./operations.js');
// Have a local variable referring to each operation.
for (var opName in operations) {
eval('var ' + opName + ' = ' + operations[opName] + ';');
}
// Clear cache, for a new copy.
if (require.cache[require.resolve('./worldFunctions.js')]) {
delete require.cache[require.resolve('./worldFunctions.js')];
}
var worldFunctions = require('./worldFunctions.js');
// Have a local variable referring to each operation.
for (var functionName in worldFunctions) {
eval('var ' + functionName + ' = ' + worldFunctions[functionName] + ';');
}
// Clear cache, for a new copy.
if (require.cache[require.resolve('./adminFunctions.js')]) {
delete require.cache[require.resolve('./adminFunctions.js')];
}
var adminFunctions = require('./adminFunctions.js');
// Have a local variable referring to each function.
for (var functionName in adminFunctions) {
eval('var ' + functionName + ' = ' + adminFunctions[functionName] + ';');
}
// Clear cache, for a new copy.
if (require.cache[require.resolve('./playerFunctions.js')]) {
delete require.cache[require.resolve('./playerFunctions.js')];
}
var playerFunctions = require('./playerFunctions.js');
// Have a local variable referring to each function.
for (var functionName in playerFunctions) {
eval('var ' + functionName + ' = ' + playerFunctions[functionName] + ';');
}
// *** //
/*
* Commands are requested by the client.
*/
var commands = {}; // WARNING: Global variable from server.js is named 'command'!
// Server control.
commands.god = {
// set PROPERTY VALUE
'set': function (user, cmdArray, cmdStr) {
// List world.config if only 'set' is sent.
if (!cmdArray[1]) {
socketHandler(user, 'info',
'World configuration properties:' +
format.object(JSON.stringify(world.config, null, 2)) +
// .replace(/\[|\]|{|}|,/gm, '')
// .replace(/^\s*\n/gm, ''))
'To reset to defaults, use: ' +
cmdChar + 'set reset');
return;
}
// Some fields may not be changed!
if (cmdArray[1] == '_id') {
socketHandler(user, 'warning', 'Cannot change this field!');
return;
}
// 'reset' to run configureWorld() again.
if (cmdArray[1] == 'reset') {
configureWorld();
socketHandler(user, 'info', 'World configuration has been reset to default!');
return;
}
// MongoDB doesn't allow $ in fields.
if (cmdArray[1].indexOf('$') >= 0) {
socketHandler(user, 'warning', 'Property names cannot include the dollar sign!');
return;
}
// Default value is empty string.
if (!cmdArray[2]) {
cmdArray[2] = '';
}
// Convert dotted notation string into a real object.
var value = cmdStr.substring(cmdStr.indexOf(" ") + 1); // Remove OBJECT.PROERTY part.
var madeObject = objDotted(cmdArray[1], value);
if (!madeObject) {
socketHandler(user, 'warning', 'Failed to set the configuration (parsing)!');
return;
}
// Upsert (update existing and create non-existing properties) the data.
updateObj(world.config, madeObject, true);
world.changed.config = true;
socketHandler(user, 'info', 'World configuration changed successfully.');
},
/* Sets a value into any world.config property, or 'reset' to default.
*/
// resetworld
'resetworld': function (user) {
if (user.account.username != 'Koss') {
socketHandler(user, 'warning', 'This command is not available to you.');
return;
}
resetWorld(user);
},
/* Kicks all users from server, resets world object data,
* reset DB data, and make server available again.
*/
// reloadcode
'reloadcode': function (user) {
if (user.account.username != 'Koss') {
socketHandler(user, 'warning', 'This command is not available to you.');
return;
}
reloadCode(user);
},
/* Reloads the commands.js code.
*/
// kick USERNAME (MESSAGE)
'kick': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'kick USERNAME (MESSAGE)');
return;
}
// Default message.
var msg = 'You have been disconnected by ' + fullNameID(user) + '.';
// Use custom message, instead.
if (cmdArray[2] && cmdArray[2].trim()) {
msg = cmdArray[2].trim();
}
var username = caseName(cmdArray[1]); // Make it case-compatibale for usernames, for example 'Bob'.
// Find player by username in world.users.
var targetUser = world.users[username];
if (targetUser) {
// Inform and kick target user.
socketHandler(targetUser, 'warning', msg);
socketHandler(targetUser, null, null, 'disconnect');
// Inform me of success.
socketHandler(user, 'info', username + ' has been successfully disconnected.');
} else {
// Not found.
socketHandler(user, 'warning', 'Username not found!');
}
}
/* Force a user to disconnect from server.
* MESSAGE defaults to 'You have been disconnected by fullNameID().'
*/
};
// World creation.
commands.builder = {
// create NAME
'create': function (user, cmdArray, cmdStr) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'create NAME');
return;
}
// Target name can only be English letters and spaces.
var name = parseName(cmdStr); // True or false.
if (!name) {
socketHandler(user, 'warning', 'Creation names must be composed only of letters and spaces!');
return;
}
var curRoom = user.room;
// Create the target object.
var targetObj = constructor.target(name, user.player.room);
// Get the target with the highest id value.
targetsdb.findOne({}, { fields: { '_id': 1 }, 'limit': 1 , 'sort': { '_id': -1 } },
function (err, doc) {
if (err) {
console.log(err);
socketHandler(user, 'warning', 'Creation (counter) failed.');
return;
}
// id is 0 for first item, or last id + 1 for new id value.
targetObj['_id'] = (!doc ? 0 : doc['_id'] + 1);
// Instance 0 for first instance of a target in the world.
targetObj.instance = 0;
// Add the the target by '_id' to world targets.
world.targets[targetObj['_id']] = {};
world.targets[targetObj['_id']]['_id'] = targetObj['_id'];
// Add first instance as instance '-1', for future duplication.
var originalObj = copyObj(targetObj);
originalObj.instance = '-1';
world.targets[originalObj['_id']]['-1'] = originalObj;
// Add the new target instance to the world.
world.targets[targetObj['_id']]['0'] = targetObj;
// Add the new target to the current room.
curRoom.targets.push(targetObj);
// Update DB immediately.
saveTarget(originalObj);
saveTarget(targetObj);
saveRoom(curRoom);
var fullID = strTarget(targetObj);
var strCoord = strPos(curRoom.position);
// Inform all (including me) in the room about the new target.
for (var i=0; i < world.watch[strCoord].length; i++) {
socketHandler(world.watch[strCoord][i], 'info',
'Creation #' + fullID + ' has been successful.' + format.newline +
'Its\' template can be changed through instance \'-1\'.');
}
});
},
/* Creates a new target where the player stands.
*/
// destroy (ID).(INSTANCE)
'destroy': function (user, cmdArray) {
var strCoord = strPos(user.player.room);
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'destroy ID.INSTANCE');
return;
}
// Parse target.
var parsedTarget = parseTarget(cmdArray[1]);
if (!parsedTarget) {
socketHandler(user, 'warning', 'Target must be a numeric value, for example: ' +
cmdChar + 'destroy 0.1');
return;
}
var idInst = parsedTarget.split('.');
// Remove last target from targets.
if (!idInst[0]) {
if (user.room.targets.length > 0) {
var curTarget = user.room.targets[user.room.targets.length-1];
var i = user.room.targets.length-1;
// Save target's fullNameID for message.
var targetName = fullNameID(curTarget);
destroyTarget(user, curTarget, i); // Remove from room, world & DB.
// Inform all (including me) in the room about the removed target.
for (var i=0; i < world.watch[strCoord].length; i++) {
socketHandler(world.watch[strCoord][i], 'info', 'Creation ' + targetName +
' has been successfully destroyed.');
}
return;
}
// Nothing to remove.
socketHandler(user, 'warning', 'No targets in the room.');
return;
}
// Make sure ID is a number.
if (isNaN(idInst[0])) {
socketHandler(user, 'warning', 'Target ID must be a number!');
return;
}
// Remove last instance of target by ID.
if (idInst[0] && !idInst[1]) {
// Loop from last, and remove first found instance.
for (var i = user.room.targets.length - 1; i >= 0; i--) {
var curTarget = user.room.targets[i];
if (curTarget['_id'] == idInst[0]) {
// Save target's fullNameID for message.
var targetName = fullNameID(curTarget);
destroyTarget(user, curTarget, i); // Remove from room, world & DB.
// Inform all (including me) in the room about the removed target.
for (var i=0; i < world.watch[strCoord].length; i++) {
socketHandler(world.watch[strCoord][i], 'info', 'Creation ' + targetName +
' has been successfully destroyed.');
}
return;
}
}
// Target not found.
socketHandler(user, 'warning', 'Target #' + idInst[0] + '.' +
idInst[1] + ' was not found.');
return;
}
// Make sure instance is a number.
if (isNaN(idInst[1])) {
socketHandler(user, 'warning', 'Target instance must be a number!');
return;
}
// Or remove target by both ID and instance.
for (var i=0; i < user.room.targets.length; i++) {
var curTarget = user.room.targets[i];
if (curTarget['_id'] == idInst[0] &&
curTarget.instance == idInst[1]) {
// Save target's fullNameID for message.
var targetName = fullNameID(curTarget);
destroyTarget(user, curTarget, i); // Remove from room, world & DB.
// Inform all (including me) in the room about the removed target.
for (var i=0; i < world.watch[strCoord].length; i++) {
socketHandler(world.watch[strCoord][i], 'info', 'Creation ' + targetName +
' has been successfully destroyed.');
}
return;
}
}
// Otherwise, target was not found.
socketHandler(user, 'warning', 'Target #' + idInst[0] + '.' + idInst[1] +
' was not found.');
}
/* Removes a target from current room, last one in targets by default,
* or instance, last one by default.
*/
};
// World manipulation.
commands.master = {
// modify room/ID(.INSTANCE) FIELD(.FIELD) VALUE
'modify': function (user, cmdArray, cmdStr) {
if (!cmdArray[1] || !cmdArray[2]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'modify room/ID FIELD VALUE');
return;
}
// Handle VALUE.
if (!cmdArray[3]) {
cmdArray[3] = '';
cmdStr = '';
} else {
// Remove the here/ID and FIELD words from the string.
for (var i=0; i < 2; i++) {
cmdStr = cmdStr.substring(cmdStr.indexOf(" ") + 1); // VALUE, string of many words.
}
}
var field = cmdArray[2].toLowerCase(); // Field is case-insensitive.
// Do this for room and leave.
if (cmdArray[1].toLowerCase() == 'room') {
modifyRoom(user, field, cmdStr);
return;
}
// Otherwise, this is a target.
var idInstance = parseTarget(cmdArray[1]);
if (!idInstance) {
socketHandler(user, 'warning', '<i>Target must be a number, such as \'0.0\'!</i>');
return;
}
var idInstArray = idInstance.split('.');
var id = idInstArray[0];
var inst = idInstArray[1];
// Original instance '-1' can be changed from anywhere.
if (inst == '-1') {
var curTarget = world.targets[id];
// In case target is not loaded into world.targets object.
if (!curTarget) {
var fieldValueArray = [];
fieldValueArray[0] = field;
fieldValueArray[1] = cmdStr;
loadTarget(user, idInstance, 'modify', fieldValueArray);
return;
}
curTarget = curTarget['-1']; // Get original instance.
modifyTarget(user, field, cmdStr, curTarget);
return;
}
// Find the target ID and instance in this room.
for (var i=0; i < user.room.targets.length; i++) {
var curTarget = user.room.targets[i];
if (curTarget['_id'] == id && curTarget['instance'] == inst) {
var curTarget = user.room.targets[i];
modifyTarget(user, field, cmdStr, curTarget);
return;
}
}
socketHandler(user, 'warning', '<i>Could not find creation [' + idInstance + '] here.</i>');
},
/* Modify an existing field in current room or target,
* or toggle an available object property (e.g. worn) or array item (e.g. commands).
* VALUE can be many words. Instance -1 is a special case, here.
*/
};
// Registered users, only.
commands.player = {
// email
'email': function (user) {
// Try the socket user object for the data.
if (user.account.email) {
socketHandler(user, 'info', 'eMail for user ' + user.account.username +
' is: ' + user.account.email);
return;
}
},
/* Display email address.
*/
// logout
'logout': function (user) {
// Remove reference to user from changed queue.
world.changed.users.splice(world.changed.users.indexOf(user), 1);
// Update 'lastonline'.
user.account.lastonline = new Date();
// Copy relevant user data, for saving queue.
var userCopy = {};
userCopy.account = copyObj(user.account);
userCopy.player = copyObj(user.player);
if (user['_id']) userCopy['_id'] = user['_id']; // If a none logged-in user, somehow, reached here.
// Add to saving queue.
world.changed.users.push(userCopy);
// Continue into logging out...
var oldName = user.player.name;
var newUser = {};
newUser.account = {};
newUser.player = {};
newUser.account.username = randomName();
newUser.player.name = newUser.account.username;
// Copy player position.
newUser.player.map = user.player.map;
newUser.player.room = copyObj(user.player.room); // { 'x': X, 'y': Y, 'z': Z }
newUser.account.access = 'user'; // Reset access level.
updateUser(user, newUser);
delete user['_id']; // Irrelevant for unregistered users.
socketHandler(user, 'info', 'You have logged out, and changed your name to ' +
user.player.name + '.');
// And alert everyone about this...
socketHandler(user, 'info', oldName + ' is now known as ' + user.player.name + '.', 'broadcast');
},
/* Logout from current logged-in user account,
* and replace account.username & player.name with a randomName(), updating user.name.
*/
// wear TARGET
'wear': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'wear TARGET');
return;
}
// TARGET must be a number.
var parsedTarget = parseTarget(cmdArray[1]);
if (!parsedTarget) {
socketHandler(user, 'warning', 'Target must be a number, such as \'0.0\'!');
return;
}
var idInst = parsedTarget.split('.');
// Load target data from DB, if not loaded, yet.
// This should only be for items on players or targets.
if (!world.targets[idInst[0]] || !world.targets[idInst[0]][idInst[1]]) {
loadTarget(user, parsedTarget, 'wear');
} else {
// Try to hold existing target.
wearTarget(user, world.targets[idInst[0]][idInst[1]]); // loadTarget makes this call, as well.
}
},
/* Wear an item from the room or hands.
*/
// remove TARGET
'remove': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'remove TARGET');
return;
}
// TARGET must be a number.
var parsedTarget = parseTarget(cmdArray[1]);
if (!parsedTarget) {
socketHandler(user, 'warning', 'Target must be a number, such as \'0.0\'!');
return;
}
var idInst = parsedTarget.split('.');
// Load target data from DB, if not loaded, yet.
// This should only be for items on players or targets.
if (!world.targets[idInst[0]] || !world.targets[idInst[0]][idInst[1]]) {
loadTarget(user, parsedTarget, 'remove');
} else {
// Try to remove existing target.
removeTarget(user, world.targets[idInst[0]][idInst[1]]); // loadTarget makes this call, as well.
}
},
/* Remove a worn item and hold if possible, or drop to room.
*/
// hold TARGET (HAND)
'hold': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'hold TARGET (HAND)');
return;
}
// TARGET must be a number.
var parsedTarget = parseTarget(cmdArray[1]);
if (!parsedTarget) {
socketHandler(user, 'warning', '<i>Target must be a number, such as \'0.0\'!</i>');
return;
}
var idInst = parsedTarget.split('.');
// Parse hand.
var hand = '';
if (cmdArray[2]) hand = cmdArray[2].toLowerCase();
// Load target data from DB, if not loaded, yet.
// This should only be for items on players or targets.
if (!world.targets[idInst[0]] || !world.targets[idInst[0]][idInst[1]]) {
loadTarget(user, parsedTarget, 'hold', hand);
} else {
// Try to hold existing target.
holdTarget(user, world.targets[idInst[0]][idInst[1]], hand); // loadTarget makes this call, as well.
}
},
/* Hold an item from the room in an empty hand, randomly or selected.
*/
// drop TARGET
'drop': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'drop TARGET');
return;
}
// TARGET must be a number.
var parsedTarget = parseTarget(cmdArray[1]);
if (!parsedTarget) {
socketHandler(user, 'warning', 'Target must be a number, such as \'0.0\'!');
return;
}
var idInst = parsedTarget.split('.');
// Load target data from DB, if not loaded, yet.
// This should only be for items on players or targets.
if (!world.targets[idInst[0]] || !world.targets[idInst[0]][idInst[1]]) {
loadTarget(user, parsedTarget, 'drop');
} else {
// Drop existing target.
dropTarget(user, world.targets[idInst[0]][idInst[1]]); // loadTarget makes this call, as well.
}
},
/* Drop an item to the room, either from hands or worn.
*/
// offer TARGET ITEM (ITEM) ...
'offer': function (user, cmdArray, cmdStr) {
if (!cmdArray[1] || !cmdArray[2]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar +
'offer TARGET/ANYONE ITEM(,AMOUNT) (ITEM)(,AMOUNT) ...');
return;
}
var items = cmdStr.substring(cmdStr.indexOf(" ") + 1); // Remove TARGET from string.
items = items.split(' '); // An array of the items.
// Parse items as targets.
for (var i=0; i < items.length; i++) {
var curItem = items[i];
var parsedItem = parseTarget(curItem);
// Item failed parsing as target.
if (!parsedItem) {
socketHandler(user, 'warning', 'Items must be formatted as ID.INSTANCE, such as \'0.0\'.');
return;
}
// Replace item with its' parsed version.
items[i] = parsedItem;
}
offerItems(user, cmdArray[1], items);
},
/* Offer a target or another player an item or items by ID.INSTANCE
* that I have available in my hands.
*/
// cancel OFFER
'cancel': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'cancel OFFER');
return;
}
var offer = parseInt(cmdArray[1]);
// OFFER must be an integer number.
if (isNaN(offer)) {
socketHandler(user, 'warning', 'Offer must be a number!');
return;
}
cancelOffer(user, offer);
},
/* Cancel an existing offer,
* by its' current index number.
*/
// accept TARGET (OFFER)
'accept': function (user, cmdArray) {
if (!cmdArray[1]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'accept TARGET (OFFER)');
return;
}
// A specific offer has been requested.
if (cmdArray[2]) {
acceptItems(user, cmdArray[1], cmdArray[2]);
return;
}
// Accept first available offer.
acceptItems(user, cmdArray[1]);
},
/* Accept an offer of items from a target or player,
* either by index, or the first (lowest index) available offer!
*/
// attack TARGET
'attack': function () {
// EMPTY
}
/* EMPTY
*
*/
};
commands.user = {
// chat MESSAGE
'chat': function (user, cmdArray, cmdStr) {
var msg = cmdStr.trim(); // Remove surrounding spaces.
if (!msg) {
socketHandler(user, 'warning', '<i>Syntax: ' + cmdChar + 'chat MESSAGE</i>');
return;
}
// Limit chat messages to 200 characters.
if (msg.length > 200) {
socketHandler(user, 'warning', 'You cannot chat a message more than two-hundred characters long.');
return;
}
// Limit chat messages to 3 in every 10 seconds.
if (!user.chatLimit) {
user.chatLimit = {
'count': 1
};
// Start cooldown timer.
user.chatLimit.timer = setTimeout(function () {
delete user.chatLimit;
}, 10000)
} else {
user.chatLimit.count += 1;
if (user.chatLimit.count > 3) {
socketHandler(user, 'warning', 'You cannot chat more than three messages, ' +
'in every ten second period.');
return;
}
}
// Escape <>&" for HTML.
msg = escapeHTML(msg);
// Send to all others.
socketHandler(user, 'message', fullNameID(user) + ': ' + msg, 'broadcast');
// Show me.
socketHandler(user, 'message', '<b>You</b>: ' + msg);
},
/* Speak to everyone in the world/server.
*/
// say MESSAGE
'say': function (user, cmdArray, cmdStr) {
var msg = cmdStr.trim();
if (!msg) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'say MESSAGE');
return;
}
// Limit chat messages to 200 characters.
if (msg.length > 100) {
socketHandler(user, 'warning', 'You cannot say a message more than one-hundred characters long.');
return;
}
// Limit say messages to 5 in every 10 seconds.
if (!user.sayLimit) {
user.sayLimit = {
'count': 1
};
// Start cooldown timer.
user.sayLimit.timer = setTimeout(function () {
delete user.sayLimit;
}, 10000)
} else {
user.sayLimit.count += 1;
if (user.sayLimit.count > 5) {
socketHandler(user, 'warning', 'You cannot say more than five messages, ' +
'in every ten second period.');
return;
}
}
// Escape <>&" for HTML.
msg = escapeHTML(msg);
// Speak to the room, only.
for (var i=0; i < world.watch[strPos(user.player.room)].length; i++) {
var curPlayer = world.watch[strPos(user.player.room)][i];
if (curPlayer.account.username != user.account.username) {
// Show my message to others.
socketHandler(curPlayer, 'say', fullNameID(user) + ' says: ' + msg);
} else {
// Show me my message.
socketHandler(user, 'say', format.bold('You say: ') + msg);
}
}
},
/* Speak to the room.
*/
// tell USERNAME MESSAGE
'tell': function (user, cmdArray, cmdStr) {
// Remove USERNAME part and surrounding spaces,
// or false, if no space after USERNAME.
var msg = (cmdStr.indexOf(" ") >= 0 ? cmdStr.substring(cmdStr.indexOf(" ") + 1).trim() : false);
// Either check for having two arguments, or an argument and a msg.
if (!cmdArray[1] || !msg) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'tell USERNAME MESSAGE');
return;
}
// Cannot tell myself.
if (cmdArray[1] == user.account.username) {
socketHandler(user, 'warning', 'You cannot tell yourself anything!');
return;
}
var username = caseName(cmdArray[1]); // Make it case-compatibale for usernames, for example 'Bob'.
// Escape <>&" for HTML.
msg = escapeHTML(msg);
// Find player by username in world.users.
var targetUser = world.users[username];
if (targetUser) {
// Tell targret player.
socketHandler(targetUser, 'tell', fullNameID(user) + ' tells you: ' + msg);
// Show me the message.
socketHandler(user, 'tell', format.bold('You tell ' + fullNameID(targetUser) + ': ') + msg);
} else {
// Not found.
socketHandler(user, 'warning', 'Username not found!');
}
},
/* Speak to another player, anywhere in the world.
*/
// move DIRECTION
'move': function (user, cmdArray) {
if (!cmdArray[1] || cmdArray[2]) {
socketHandler(user, 'warning', 'Syntax: ' + cmdChar + 'move DIRECTION');
return;
}
// Convert direction to coordinates.
var curPos = user.player.room;
var newPos = {};
// Parse direction to new position. Returns false on failure.
newPos = posDir(curPos, cmdArray[1].toLowerCase());
/* ACTIVE ONLY FOR NON-BUILDERS
var check = false; // Assume movement not possible.
// Check that the exit exists in the current room.
for (var i=0; i < user.room.exits.length; i++) {
if (user.room.exits[i] == cmdArray[1]) {
check = true;
}
}
if (!check) {
socket.emit('warning', '<i>Exit not found!</i>');
return;
} */
// Check that the exit is open.
// N/A.
// Move.
if (newPos) {
user.player.room = newPos;
socketHandler(user, 'info', 'Moving to position ' + JSON.stringify(newPos) + '.');
loadRoom(user);
return;
}
socketHandler(user, 'warning', 'Exit not found!');
},
/* Asks to move to a new position in the same map.
*/
// emote ACTION (TARGET)
'emote': function (user, cmdArray) {
if (!cmdArray[1]) {
// Get a list of all available emotes.
var emotes = '';
for (emote in world.config.emotes) {
emotes += emote + ', ';
}
emotes = emotes.slice(0, emotes.length-2); // Remove last ', '
socketHandler(user, 'info', 'The emotes available to you are:' + format.newline + emotes + '.');
return;
}
// Find emote.
var curEmote = world.config.emotes[cmdArray[1]];
if (!curEmote) {
socketHandler(user, 'warning', 'Emote ' + cmdArray[1].toLowerCase() + ' not found!');
return;
}
// Options: curEmote.room.me/others (at no one)
// curEmote.self.me/others (at myself)
// curEmote.player.me/player/others (at another player)
// curEmote.target.me/others (at a target)
// Emote to the room at-large.
if (!cmdArray[2]) {
// Show me the emote.
socketHandler(user, 'emote', curEmote.room.me.replace('USER', user.player.name));
// Show other players in the room.
for (var i=0; i < world.watch[strPos(user.player.room)].length; i++) {
var curUser = world.watch[strPos(user.player.room)][i];
// Send with each user socket, except myself.
if (curUser.account.username != user.account.username) {
socketHandler(curUser, 'emote', curEmote.room.others.replace('USER', user.player.name));
}
}
return;
}
// Emote at a target or user.
// In the special case of emoting to myself.
if (caseName(cmdArray[2]) == user.account.username) {
socketHandler(user, 'emote', curEmote.self.me.replace('USER', user.player.name));
// Show other players in the room.
for (var i=0; i < world.watch[strPos(user.player.room)].length; i++) {
var curUser = world.watch[strPos(user.player.room)][i];
// Send with each user socket, except myself.
if (curUser.account.username != user.account.username) {
socketHandler(curUser, 'emote', curEmote.self.others.replace('USER', user.player.name));
}
}
return;
}
// Find the target or user in the room.
var emoteTarget = false;
// Try to find a user.
for (var i=0; i < world.watch[strPos(user.player.room)].length; i++) {
var curUser = world.watch[strPos(user.player.room)][i];
if (curUser.account.username == caseName(cmdArray[2])) {
emoteTarget = curUser.socket; // Refer to the socket.
break;
}
}
// If not found user, then try to find a target.
if (!emoteTarget) {
var arrTargets = world.maps[user.player.map].rooms[strPos(user.player.room)].targets;
for (var i=0; i < arrTargets.length; i++) {
var curTarget = arrTargets[i];
var idInst = strTarget(curTarget);
if (idInst == cmdArray[2]) {
emoteTarget = curTarget;
break;
}
}
}
// Otherwise, leave.
if (!emoteTarget) {
socketHandler(user, 'warning', '<i>Could not find ' + cmdArray[2] + ' here!</i>');
return;
}
// Player emote or target emote.
if (!emoteTarget.instance) {
// Show me the emote.
socketHandler(user, 'emote', curEmote.player.me.replace('USER2', caseName(cmdArray[2])));
// Show the other player the emote.
socketHandler(emoteTarget, 'emote', curEmote.player.player.replace('USER1', user.player.name));
// Show others the emote.
for (var i=0; i < world.watch[strPos(user.player.room)].length; i++) {
var curUser = world.watch[strPos(user.player.room)][i];
// Send with each user socket, except myself and emoteTarget player.
if (curUser.account.username != user.account.username &&
curUser.account.username != caseName(cmdArray[2])) {
socketHandler(curUser, 'emote', curEmote.player.others.replace('USER1', user.player.name)