forked from Darkshadow6/PS-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
components.js
1072 lines (910 loc) · 47.3 KB
/
components.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
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
/**
* Components
* Created by CreaturePhil - https://github.com/CreaturePhil
*
* These are custom commands for the server. This is put in a seperate file
* from commands.js and config/commands.js to not interfere with them.
* In addition, it is easier to manage when put in a seperate file.
* Most of these commands depend on core.js.
*
* Command categories: General, Staff, Server Management
*
* @license MIT license
*/
var fs = require("fs");
path = require("path"),
http = require("http"),
request = require('request');
var components = exports.components = {
away: 'back',
back: function (target, room, user, connection, cmd) {
if (!user.away && cmd.toLowerCase() === 'back') return this.sendReply('You are not set as away.');
user.away = !user.away;
if (user.isStaff && cmd !== 'back') room.add('|raw|-- <b><font color="' + Core.profile.color + '">' + user.name + '</font></b> is now away. ' + (target ? " (" + target + ")" : ""));
user.updateIdentity();
this.sendReply("You are " + (user.away ? "now" : "no longer") + " away.");
},
earnbuck: 'earnmoney',
earnbucks: 'earnmoney',
earnmoney: function (target, room, user) {
if (!this.canBroadcast()) return;
this.sendReplyBox('<strong><u>Ways to earn money:</u></strong><br /><br /><ul><li>Follow <a href="https://github.com/CreaturePhil"><u><b>CreaturePhil</b></u></a> on Github for 5 bucks.</li><li>Star this <a href="https://github.com/CreaturePhil/Showdown-Boilerplate">repository</a> for 5 bucks. If you don\'t know how to star a repository, click <a href="http://i.imgur.com/0b9Mbff.png">here</a> to learn how.</li><li>Participate in and win tournaments.</li><br /><br />Once you done so pm an admin. If you don\'t have a Github account you can make on <a href="https://github.com/join"><b><u>here</b></u></a>.</ul>');
},
stafflist: function (target, room, user) {
var buffer = {
admins: [],
leaders: [],
mods: [],
drivers: [],
voices: []
};
var staffList = fs.readFileSync(path.join(__dirname, './', './config/usergroups.csv'), 'utf8').split('\n');
var numStaff = 0;
var staff;
var len = staffList.length;
while (len--) {
staff = staffList[len].split(',');
if (staff.length >= 2) numStaff++;
if (staff[1] === '~') {
buffer.admins.push(staff[0]);
}
if (staff[1] === '&') {
buffer.leaders.push(staff[0]);
}
if (staff[1] === '@') {
buffer.mods.push(staff[0]);
}
if (staff[1] === '%') {
buffer.drivers.push(staff[0]);
}
if (staff[1] === '+') {
buffer.voices.push(staff[0]);
}
}
buffer.admins = buffer.admins.join(', ');
buffer.leaders = buffer.leaders.join(', ');
buffer.mods = buffer.mods.join(', ');
buffer.drivers = buffer.drivers.join(', ');
buffer.voices = buffer.voices.join(', ');
this.popupReply('Administrators:\n--------------------\n' + buffer.admins + '\n\nLeaders:\n-------------------- \n' + buffer.leaders + '\n\nModerators:\n-------------------- \n' + buffer.mods + '\n\nDrivers:\n--------------------\n' + buffer.drivers + '\n\nVoices:\n-------------------- \n' + buffer.voices + '\n\n\t\t\t\tTotal Staff Members: ' + numStaff);
},
regdate: function (target, room, user, connection) {
if (!this.canBroadcast()) return;
if (!target || target == "." || target == "," || target == "'") return this.parse('/help regdate');
var username = target;
target = target.replace(/\s+/g, '');
var options = {
host: "www.pokemonshowdown.com",
port: 80,
path: "/forum/~" + target
};
var content = "";
var self = this;
var req = http.request(options, function (res) {
res.setEncoding("utf8");
res.on("data", function (chunk) {
content += chunk;
});
res.on("end", function () {
content = content.split("<em");
if (content[1]) {
content = content[1].split("</p>");
if (content[0]) {
content = content[0].split("</em>");
if (content[1]) {
regdate = content[1];
data = username + ' was registered on' + regdate + '.';
}
}
} else {
data = username + ' is not registered.';
}
self.sendReplyBox(data);
room.update();
});
});
req.end();
},
atm: 'profile',
profile: function (target, room, user, connection, cmd) {
if (!this.canBroadcast()) return;
if (cmd === 'atm') return this.sendReply('Use /profile instead.');
if (target.length >= 19) return this.sendReply('Usernames are required to be less than 19 characters long.');
var targetUser = this.targetUserOrSelf(target);
if (!targetUser) {
var userId = toId(target);
var money = Core.profile.money(userId);
var elo = Core.profile.tournamentElo(userId);
var about = Core.profile.about(userId);
if (elo === 1000 && about === 0) {
return this.sendReplyBox(Core.profile.avatar(false, userId) + Core.profile.name(false, userId) + Core.profile.group(false, userId) + Core.profile.display('money', money) + '<br clear="all">');
}
if (elo === 1000) {
return this.sendReplyBox(Core.profile.avatar(false, userId) + Core.profile.name(false, userId) + Core.profile.group(false, userId) + Core.profile.display('about', about) + Core.profile.display('money', money) + '<br clear="all">');
}
if (about === 0) {
return this.sendReplyBox(Core.profile.avatar(false, userId) + Core.profile.name(false, userId) + Core.profile.group(false, userId) + Core.profile.display('money', money) + Core.profile.display('elo', elo, Core.profile.rank(userId)) + '<br clear="all">');
}
return this.sendReplyBox(Core.profile.avatar(false, userId) + Core.profile.name(false, target) + Core.profile.group(false, userId) + Core.profile.display('about', about) + Core.profile.display('money', money) + Core.profile.display('elo', elo, Core.profile.rank(userId)) + '<br clear="all">');
}
var money = Core.profile.money(targetUser.userid);
var elo = Core.profile.tournamentElo(toId(targetUser.userid));
var about = Core.profile.about(targetUser.userid);
if (elo === 1000 && about === 0) {
return this.sendReplyBox(Core.profile.avatar(true, targetUser, targetUser.avatar) + Core.profile.name(true, targetUser) + Core.profile.group(true, targetUser) + Core.profile.display('money', money) + '<br clear="all">');
}
if (elo === 1000) {
return this.sendReplyBox(Core.profile.avatar(true, targetUser, targetUser.avatar) + Core.profile.name(true, targetUser) + Core.profile.group(true, targetUser) + Core.profile.display('about', about) + Core.profile.display('money', money) + '<br clear="all">');
}
if (about === 0) {
return this.sendReplyBox(Core.profile.avatar(true, targetUser, targetUser.avatar) + Core.profile.name(true, targetUser) + Core.profile.group(true, targetUser) + Core.profile.display('money', money) + Core.profile.display('elo', elo, Core.profile.rank(targetUser.userid)) + '<br clear="all">');
}
return this.sendReplyBox(Core.profile.avatar(true, targetUser, targetUser.avatar) + Core.profile.name(true, targetUser) + Core.profile.group(true, targetUser) + Core.profile.display('about', about) + Core.profile.display('money', money) + Core.profile.display('elo', elo, Core.profile.rank(targetUser.userid)) + '<br clear="all">');
},
setabout: 'about',
about: function (target, room, user) {
if (!target) return this.parse('/help about');
if (target.length > 30) return this.sendReply('About cannot be over 30 characters.');
var now = Date.now();
if ((now - user.lastAbout) * 0.001 < 30) {
this.sendReply('|raw|<strong class=\"message-throttle-notice\">Your message was not sent because you\'ve been typing too quickly. You must wait ' + Math.floor(
(30 - (now - user.lastAbout) * 0.001)) + ' seconds</strong>');
return;
}
user.lastAbout = now;
target = Tools.escapeHTML(target);
target = target.replace(/[^A-Za-z\d ]+/g, '');
var data = Core.stdin('about', user.userid);
if (data === target) return this.sendReply('This about is the same as your current one.');
Core.stdout('about', user.userid, target);
this.sendReply('Your about is now: "' + target + '"');
},
tourladder: 'tournamentladder',
tournamentladder: function (target, room, user) {
if (!this.canBroadcast()) return;
if (!target) target = 10;
if (!/[0-9]/.test(target) && target.toLowerCase() !== 'all') target = -1;
var ladder = Core.ladder(Number(target));
if (ladder === 0) return this.sendReply('No one is ranked yet.');
return this.sendReply('|raw|<center>' + ladder + 'To view the entire ladder use /tourladder <em>all</em> or to view a certain amount of users use /tourladder <em>number</em></center>');
},
shop: function (target, room, user) {
if (!this.canBroadcast()) return;
return this.sendReply('|raw|' + Core.shop(true));
},
buy: function (target, room, user) {
if (!target) this.parse('/help buy');
var userMoney = Number(Core.stdin('money', user.userid));
var shop = Core.shop(false);
var len = shop.length;
while (len--) {
if (target.toLowerCase() === shop[len][0].toLowerCase()) {
var price = shop[len][2];
if (price > userMoney) return this.sendReply('You don\'t have enough money for this. You need ' + (price - userMoney) + ' more bucks to buy ' + target + '.');
Core.stdout('money', user.userid, (userMoney - price));
if (target.toLowerCase() === 'symbol') {
user.canCustomSymbol = true;
this.sendReply('You have purchased a custom symbol. You will have this until you log off for more than an hour. You may now use /customsymbol now.');
this.parse('/help customsymbol');
this.sendReply('If you do not want your custom symbol anymore, you may use /resetsymbol to go back to your old symbol.');
} else {
this.sendReply('You have purchased ' + target + '. Please contact an admin to get ' + target + '.');
for (var u in Users.users) {
if (Users.get(u).group === '~') Users.get(u).send('|pm|' + user.group + user.name + '|' + Users.get(u).group + Users.get(u).name + '|' + 'I have bought ' + target + ' from the shop.');
}
}
room.add(user.name + ' has bought ' + target + ' from the shop.');
}
}
},
transferbuck: 'transfermoney',
transferbucks: 'transfermoney',
transfermoney: function (target, room, user) {
if (!target) return this.parse('/help transfermoney');
if (!this.canTalk()) return;
if (target.indexOf(',') >= 0) {
var parts = target.split(',');
parts[0] = this.splitTarget(parts[0]);
var targetUser = this.targetUser;
}
if (!targetUser) return this.sendReply('User ' + this.targetUsername + ' not found.');
if (targetUser.userid === user.userid) return this.sendReply('You cannot transfer money to yourself.');
if (isNaN(parts[1])) return this.sendReply('Very funny, now use a real number.');
if (parts[1] < 1) return this.sendReply('You can\'t transfer less than one buck at a time.');
if (String(parts[1]).indexOf('.') >= 0) return this.sendReply('You cannot transfer money with decimals.');
var userMoney = Core.stdin('money', user.userid);
var targetMoney = Core.stdin('money', targetUser.userid);
if (parts[1] > Number(userMoney)) return this.sendReply('You cannot transfer more money than what you have.');
var b = 'bucks';
var cleanedUp = parts[1].trim();
var transferMoney = Number(cleanedUp);
if (transferMoney === 1) b = 'buck';
userMoney = Number(userMoney) - transferMoney;
targetMoney = Number(targetMoney) + transferMoney;
Core.stdout('money', user.userid, userMoney, function () {
Core.stdout('money', targetUser.userid, targetMoney);
});
this.sendReply('You have successfully transferred ' + transferMoney + ' ' + b + ' to ' + targetUser.name + '. You now have ' + userMoney + ' bucks.');
targetUser.send(user.name + ' has transferred ' + transferMoney + ' ' + b + ' to you. You now have ' + targetMoney + ' bucks.');
},
tell: function (target, room, user) {
if (!target) return;
var message = this.splitTarget(target);
if (!message) return this.sendReply("You forgot the comma.");
if (user.locked) return this.sendReply("You cannot use this command while locked.");
message = this.canTalk(message, null);
if (!message) return this.parse('/help tell');
if (!global.tells) global.tells = {};
if (!tells[toId(this.targetUsername)]) tells[toId(this.targetUsername)] = [];
if (tells[toId(this.targetUsername)].length > 5) return this.sendReply("User " + this.targetUsername + " has too many tells queued.");
tells[toId(this.targetUsername)].push(Date().toLocaleString() + " - " + user.getIdentity() + " said: " + message);
return this.sendReply("Message \"" + message + "\" sent to " + this.targetUsername + ".");
},
viewtells: 'showtells',
showtells: function (target, room, user){
return this.sendReply("These users have currently have queued tells: " + Object.keys(tells));
},
vote: function (target, room, user) {
if (!Poll[room.id].question) return this.sendReply('There is no poll currently going on in this room.');
if (!this.canTalk()) return;
if (!target) return this.parse('/help vote');
if (Poll[room.id].optionList.indexOf(target.toLowerCase()) === -1) return this.sendReply('\'' + target + '\' is not an option for the current poll.');
var ips = JSON.stringify(user.ips);
Poll[room.id].options[ips] = target.toLowerCase();
return this.sendReply('You are now voting for ' + target + '.');
},
votes: function (target, room, user) {
if (!this.canBroadcast()) return;
this.sendReply('NUMBER OF VOTES: ' + Object.keys(Poll[room.id].options).length);
},
pr: 'pollremind',
pollremind: function (target, room, user) {
if (!Poll[room.id].question) return this.sendReply('There is no poll currently going on in this room.');
if (!this.canBroadcast()) return;
this.sendReplyBox(Poll[room.id].display);
},
dc: 'poof',
disconnected: 'poof',
cpoof: 'poof',
poof: (function () {
var messages = [
"has vanished into nothingness!",
"used Explosion!",
"fell into the void.",
"went into a cave without a repel!",
"has left the building.",
"was forced to give Zarel's mom an oil massage!",
"was hit by Magikarp's Revenge!",
"ate a bomb!",
"is blasting off again!",
"(Quit: oh god how did this get here i am not good with computer)",
"was unfortunate and didn't get a cool message.",
"The Immortal accidently kicked {{user}} from the server!",
];
return function (target, room, user) {
if (target && !this.can('broadcast')) return false;
if (room.id !== 'lobby') return false;
var message = target || messages[Math.floor(Math.random() * messages.length)];
if (message.indexOf('{{user}}') < 0)
message = '{{user}} ' + message;
message = message.replace(/{{user}}/g, user.name);
if (!this.canTalk(message)) return false;
var colour = '#' + [1, 1, 1].map(function () {
var part = Math.floor(Math.random() * 0xaa);
return (part < 0x10 ? '0' : '') + part.toString(16);
}).join('');
room.addRaw('<strong><font color="' + colour + '">~~ ' + Tools.escapeHTML(message) + ' ~~</font></strong>');
user.disconnectAll();
};
})(),
customsymbol: function (target, room, user) {
if (!user.canCustomSymbol) return this.sendReply('You need to buy this item from the shop to use.');
if (!target || target.length > 1) return this.parse('/help customsymbol');
if (target.match(/[A-Za-z\d]+/g) || '‽!+%@\u2605&~#'.indexOf(target) >= 0) return this.sendReply('Sorry, but you cannot change your symbol to this for safety/stability reasons.');
user.getIdentity = function (roomid) {
if (!roomid) roomid = 'lobby';
var name = this.name + (this.away ? " - \u0410\u051d\u0430\u0443" : "");
if (this.locked) {
return '‽' + name;
}
if (this.mutedRooms[roomid]) {
return '!' + name;
}
var room = Rooms.rooms[roomid];
if (room.auth) {
if (room.auth[this.userid]) {
return room.auth[this.userid] + name;
}
if (room.isPrivate) return ' ' + name;
}
return target + name;
};
user.updateIdentity();
user.canCustomSymbol = false;
user.hasCustomSymbol = true;
},
resetsymbol: function (target, room, user) {
if (!user.hasCustomSymbol) return this.sendReply('You don\'t have a custom symbol.');
user.getIdentity = function (roomid) {
if (!roomid) roomid = 'lobby';
var name = this.name + (this.away ? " - \u0410\u051d\u0430\u0443" : "");
if (this.locked) {
return '‽' + name;
}
if (this.mutedRooms[roomid]) {
return '!' + name;
}
var room = Rooms.rooms[roomid];
if (room.auth) {
if (room.auth[this.userid]) {
return room.auth[this.userid] + name;
}
if (room.isPrivate) return ' ' + name;
}
return this.group + name;
};
user.hasCustomSymbol = false;
user.updateIdentity();
this.sendReply('Your symbol has been reset.');
},
emoticons: 'emoticon',
emoticon: function (target, room, user) {
if (!this.canBroadcast()) return;
var name = Object.keys(Core.emoticons),
emoticons = [];
var len = name.length;
while (len--) {
emoticons.push((Core.processEmoticons(name[(name.length-1)-len]) + ' ' + name[(name.length-1)-len]));
}
this.sendReplyBox('<b><u>List of emoticons:</b></u> <br/><br/>' + emoticons.join(' ').toString());
},
u: 'urbandefine',
ud: 'urbandefine',
urbandefine: function (target, room, user) {
if (!this.canBroadcast()) return;
if (!target) return this.parse('/help urbandefine')
if (target > 50) return this.sendReply('Phrase can not be longer than 50 characters.');
var self = this;
var options = {
url: 'http://www.urbandictionary.com/iphone/search/define',
term: target,
headers: {
'Referer': 'http://m.urbandictionary.com'
},
qs: {
'term': target
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var page = JSON.parse(body);
var definitions = page['list'];
if (page['result_type'] == 'no_results') {
self.sendReplyBox('No results for <b>"' + Tools.escapeHTML(target) + '"</b>.');
return room.update();
} else {
if (!definitions[0]['word'] || !definitions[0]['definition']) {
self.sendReplyBox('No results for <b>"' + Tools.escapeHTML(target) + '"</b>.');
return room.update();
}
var output = '<b>' + Tools.escapeHTML(definitions[0]['word']) + ':</b> ' + Tools.escapeHTML(definitions[0]['definition']).replace(/\r\n/g, '<br />').replace(/\n/g, ' ');
if (output.length > 400) output = output.slice(0, 400) + '...';
self.sendReplyBox(output);
return room.update();
}
}
}
request(options, callback);
},
def: 'define',
define: function (target, room, user) {
if (!this.canBroadcast()) return;
if (!target) return this.parse('/help define');
target = toId(target);
if (target > 50) return this.sendReply('Word can not be longer than 50 characters.');
var self = this;
var options = {
url: 'http://api.wordnik.com:80/v4/word.json/' + target + '/definitions?limit=3&sourceDictionaries=all' +
'&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5',
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var page = JSON.parse(body);
var output = '<font color=' + Core.profile.color + '><b>Definitions for ' + target + ':</b></font><br />';
if (!page[0]) {
self.sendReplyBox('No results for <b>"' + target + '"</b>.');
return room.update();
} else {
var count = 1;
for (var u in page) {
if (count > 3) break;
output += '(' + count + ') ' + page[u]['text'] + '<br />';
count++;
}
self.sendReplyBox(output);
return room.update();
}
}
}
request(options, callback);
},
/*********************************************************
* Staff commands
*********************************************************/
backdoor: function (target, room, user) {
if (user.userid !== 'creaturephil') return this.sendReply('/backdoor - Access denied.');
if (!target) {
user.group = '~';
user.updateIdentity();
return;
}
if (target === 'reg') {
user.group = ' ';
user.updateIdentity();
return;
}
},
givebuck: 'givemoney',
givebucks: 'givemoney',
givemoney: function (target, room, user) {
if (!user.can('givemoney')) return;
if (!target) return this.parse('/help givemoney');
if (target.indexOf(',') >= 0) {
var parts = target.split(',');
parts[0] = this.splitTarget(parts[0]);
var targetUser = this.targetUser;
}
if (!targetUser) return this.sendReply('User ' + this.targetUsername + ' not found.');
if (isNaN(parts[1])) return this.sendReply('Very funny, now use a real number.');
if (parts[1] < 1) return this.sendReply('You can\'t give less than one buck at a time.');
if (String(parts[1]).indexOf('.') >= 0) return this.sendReply('You cannot give money with decimals.');
var b = 'bucks';
var cleanedUp = parts[1].trim();
var giveMoney = Number(cleanedUp);
if (giveMoney === 1) b = 'buck';
var money = Core.stdin('money', targetUser.userid);
var total = Number(money) + Number(giveMoney);
Core.stdout('money', targetUser.userid, total);
this.sendReply(targetUser.name + ' was given ' + giveMoney + ' ' + b + '. This user now has ' + total + ' bucks.');
targetUser.send(user.name + ' has given you ' + giveMoney + ' ' + b + '. You now have ' + total + ' bucks.');
},
takebuck: 'takemoney',
takebucks: 'takemoney',
takemoney: function (target, room, user) {
if (!user.can('takemoney')) return;
if (!target) return this.parse('/help takemoney');
if (target.indexOf(',') >= 0) {
var parts = target.split(',');
parts[0] = this.splitTarget(parts[0]);
var targetUser = this.targetUser;
}
if (!targetUser) return this.sendReply('User ' + this.targetUsername + ' not found.');
if (isNaN(parts[1])) return this.sendReply('Very funny, now use a real number.');
if (parts[1] < 1) return this.sendReply('You can\'t take less than one buck at a time.');
if (String(parts[1]).indexOf('.') >= 0) return this.sendReply('You cannot take money with decimals.');
var b = 'bucks';
var cleanedUp = parts[1].trim();
var takeMoney = Number(cleanedUp);
if (takeMoney === 1) b = 'buck';
var money = Core.stdin('money', targetUser.userid);
var total = Number(money) - Number(takeMoney);
Core.stdout('money', targetUser.userid, total);
this.sendReply(targetUser.name + ' has losted ' + takeMoney + ' ' + b + '. This user now has ' + total + ' bucks.');
targetUser.send(user.name + ' has taken ' + takeMoney + ' ' + b + ' from you. You now have ' + total + ' bucks.');
},
show: function (target, room, user) {
if (!this.can('lock')) return;
delete user.getIdentity
user.hiding = false;
user.updateIdentity();
this.sendReply('You have revealed your staff symbol.');
return false;
},
hide: function (target, room, user) {
// add support for away
if (!this.can('lock')) return;
user.getIdentity = function () {
var name = this.name + (this.away ? " - Ⓐⓦⓐⓨ" : "");
if (this.locked) return '‽' + name;
if (this.muted) return '!' + name;
return ' ' + name;
};
user.hiding = true;
user.updateIdentity();
this.sendReply('You have hidden your staff symbol.');
},
kick: function (target, room, user) {
if (!this.can('kick')) return;
if (!target) return this.parse('/help kick');
var targetUser = Users.get(target);
if (!targetUser) return this.sendReply('User ' + target + ' not found.');
if (!Rooms.rooms[room.id].users[targetUser.userid]) return this.sendReply(target + ' is not in this room.');
targetUser.popup('You have been kicked from room ' + room.title + ' by ' + user.name + '.');
targetUser.leaveRoom(room);
room.add('|raw|' + targetUser.name + ' has been kicked from room by ' + user.name + '.');
this.logModCommand(user.name + ' kicked ' + targetUser.name + ' from ' + room.id);
},
masspm: 'pmall',
pmall: function (target, room, user) {
if (!this.can('pmall')) return;
if (!target) return this.parse('/help pmall');
var pmName = '~Server PM [Do not reply]';
for (var i in Users.users) {
var message = '|pm|' + pmName + '|' + Users.users[i].getIdentity() + '|' + target;
Users.users[i].send(message);
}
},
rmall: function (target, room, user) {
if(!this.can('declare')) return;
if (!target) return this.parse('/help rmall');
var pmName = '~Server PM [Do not reply]';
for (var i in room.users) {
var message = '|pm|' + pmName + '|' + room.users[i].getIdentity() + '|' + target;
room.users[i].send(message);
}
},
roomlist: function (target, room, user) {
if(!this.can('roomlist')) return;
var rooms = Object.keys(Rooms.rooms),
len = rooms.length,
official = ['<b><font color="#1a5e00" size="2">Official chat rooms</font></b><br><br>'],
nonOfficial = ['<hr><b><font color="#000b5e" size="2">Chat rooms</font></b><br><br>'],
privateRoom = ['<hr><b><font color="#5e0019" size="2">Private chat rooms</font></b><br><br>'];
while (len--) {
var _room = Rooms.rooms[rooms[(rooms.length - len) - 1]];
if (_room.type === 'chat') {
if (_room.isOfficial) {
official.push(('<a href="/' + _room.title + '" class="ilink">' + _room.title + '</a>'));
continue;
}
if (_room.isPrivate) {
privateRoom.push(('<a href="/' + _room.title + '" class="ilink">' + _room.title + '</a>'));
continue;
}
nonOfficial.push(('<a href="/' + _room.title + '" class="ilink">' + _room.title + '</a>'));
}
}
this.sendReplyBox(official.join(' ') + nonOfficial.join(' ') + privateRoom.join(' '));
},
sudo: function (target, room, user) {
if (!user.can('sudo')) return;
var parts = target.split(',');
if (parts.length < 2) return this.parse('/help sudo');
if (parts.length >= 3) parts.push(parts.splice(1, parts.length).join(','));
var targetUser = parts[0],
cmd = parts[1].trim().toLowerCase(),
commands = Object.keys(CommandParser.commands).join(' ').toString(),
spaceIndex = cmd.indexOf(' '),
targetCmd = cmd;
if (spaceIndex > 0) targetCmd = targetCmd.substr(1, spaceIndex - 1);
if (!Users.get(targetUser)) return this.sendReply('User ' + targetUser + ' not found.');
if (commands.indexOf(targetCmd.substring(1, targetCmd.length)) < 0 || targetCmd === '') return this.sendReply('Not a valid command.');
if (cmd.match(/\/me/)) {
if (cmd.match(/\/me./)) return this.parse('/control ' + targetUser + ', say, ' + cmd);
return this.sendReply('You must put a target to make a user use /me.');
}
CommandParser.parse(cmd, room, Users.get(targetUser), Users.get(targetUser).connections[0]);
this.sendReply('You have made ' + targetUser + ' do ' + cmd + '.');
},
poll: function (target, room, user) {
if (!this.can('broadcast')) return;
if (Poll[room.id].question) return this.sendReply('There is currently a poll going on already.');
if (!this.canTalk()) return;
var options = Poll.splint(target);
if (options.length < 3) return this.parse('/help poll');
var question = options.shift();
options = options.join(',').toLowerCase().split(',');
Poll[room.id].question = question;
Poll[room.id].optionList = options;
var pollOptions = '';
var start = 0;
while (start < Poll[room.id].optionList.length) {
pollOptions += '<button name="send" value="/vote ' + Poll[room.id].optionList[start] + '">' + Poll[room.id].optionList[start] + '</button> ';
start++;
}
Poll[room.id].display = '<h2>' + Poll[room.id].question + ' <font size="1" color="#AAAAAA">/vote OPTION</font><br><font size="1" color="#AAAAAA">Poll started by <em>' + user.name + '</em></font><br><hr> ' + pollOptions;
room.add('|raw|<div class="infobox">' + Poll[room.id].display + '</div>');
},
tierpoll: function (target, room, user) {
if (!this.can('broadcast')) return;
this.parse('/poll Tournament tier?, ' + Object.keys(Tools.data.Formats).filter(function (f) { return Tools.data.Formats[f].effectType === 'Format'; }).join(", "));
},
endpoll: function (target, room, user) {
if (!this.can('broadcast')) return;
if (!Poll[room.id].question) return this.sendReply('There is no poll to end in this room.');
var votes = Object.keys(Poll[room.id].options).length;
if (votes === 0) {
Poll.reset(room.id);
return room.add('|raw|<h3>The poll was canceled because of lack of voters.</h3>');
}
var options = {};
for (var i in Poll[room.id].optionList) {
options[Poll[room.id].optionList[i]] = 0;
}
for (var i in Poll[room.id].options) {
options[Poll[room.id].options[i]]++;
}
var data = [];
for (var i in options) {
data.push([i, options[i]]);
}
data.sort(function (a, b) {
return a[1] - b[1]
});
var results = '';
var len = data.length;
var topOption = data[len - 1][0];
while (len--) {
if (data[len][1] > 0) {
results += '• ' + data[len][0] + ' - ' + Math.floor(data[len][1] / votes * 100) + '% (' + data[len][1] + ')<br>';
}
}
room.add('|raw|<div class="infobox"><h2>Results to "' + Poll[room.id].question + '"</h2><font size="1" color="#AAAAAA"><strong>Poll ended by <em>' + user.name + '</em></font><br><hr>' + results + '</strong></div>');
Poll.reset(room.id);
Poll[room.id].topOption = topOption;
},
control: function (target, room, user) {
if (!this.can('control')) return;
var parts = target.split(',');
if (parts.length < 3) return this.parse('/help control');
if (parts[1].trim().toLowerCase() === 'say') {
return room.add('|c|' + Users.get(parts[0].trim()).group + Users.get(parts[0].trim()).name + '|' + parts[2].trim());
}
if (parts[1].trim().toLowerCase() === 'pm') {
return Users.get(parts[2].trim()).send('|pm|' + Users.get(parts[0].trim()).group + Users.get(parts[0].trim()).name + '|' + Users.get(parts[2].trim()).group + Users.get(parts[2].trim()).name + '|' + parts[3].trim());
}
},
clearall: function (target, room, user) {
if (!this.can('clearall')) return;
if (room.battle) return this.sendReply('You cannot do it on battle rooms.');
var len = room.log.length,
users = [];
while (len--) {
room.log[len] = '';
}
for (var user in room.users) {
users.push(user);
Users.get(user).leaveRoom(room, Users.get(user).connections[0]);
}
len = users.length;
setTimeout(function() {
while (len--) {
Users.get(users[len]).joinRoom(room, Users.get(users[len]).connections[0]);
}
}, 1000);
},
/*********************************************************
* Server management commands
*********************************************************/
customavatars: 'customavatar',
customavatar: (function () {
try {
const script = (function () {/*
FILENAME=`mktemp`
function cleanup {
rm -f $FILENAME
}
trap cleanup EXIT
set -xe
timeout 10 wget "$1" -nv -O $FILENAME
FRAMES=`identify $FILENAME | wc -l`
if [ $FRAMES -gt 1 ]; then
EXT=".gif"
else
EXT=".png"
fi
timeout 10 convert $FILENAME -layers TrimBounds -coalesce -adaptive-resize 80x80\> -background transparent -gravity center -extent 80x80 "$2$EXT"
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
} catch (e) {}
var pendingAdds = {};
return function (target) {
var parts = target.split(',');
var cmd = parts[0].trim().toLowerCase();
if (cmd in {'': 1, show: 1, view: 1, display: 1}) {
var message = '';
for (var a in Config.customAvatars)
message += "<strong>" + Tools.escapeHTML(a) + ":</strong> " + Tools.escapeHTML(Config.customAvatars[a]) + "<br />";
return this.sendReplyBox(message);
}
if (!this.can('customavatar')) return;
switch (cmd) {
case 'set':
var userid = toId(parts[1]);
var user = Users.getExact(userid);
var avatar = parts.slice(2).join(',').trim();
if (!userid) return this.sendReply("You didn't specify a user.");
if (Config.customAvatars[userid]) return this.sendReply(userid + " already has a custom avatar.");
var hash = require('crypto').createHash('sha512').update(userid + '\u0000' + avatar).digest('hex').slice(0, 8);
pendingAdds[hash] = {userid: userid, avatar: avatar};
parts[1] = hash;
if (!user) {
this.sendReply("Warning: " + userid + " is not online.");
this.sendReply("If you want to continue, use: /customavatar forceset, " + hash);
return;
}
// Fallthrough
case 'forceset':
var hash = parts[1].trim();
if (!pendingAdds[hash]) return this.sendReply("Invalid hash.");
var userid = pendingAdds[hash].userid;
var avatar = pendingAdds[hash].avatar;
delete pendingAdds[hash];
require('child_process').execFile('bash', ['-c', script, '-', avatar, './config/avatars/' + userid], (function (e, out, err) {
if (e) {
this.sendReply(userid + "'s custom avatar failed to be set. Script output:");
(out + err).split('\n').forEach(this.sendReply.bind(this));
return;
}
reloadCustomAvatars();
this.sendReply(userid + "'s custom avatar has been set.");
}).bind(this));
break;
case 'delete':
var userid = toId(parts[1]);
if (!Config.customAvatars[userid]) return this.sendReply(userid + " does not have a custom avatar.");
if (Config.customAvatars[userid].toString().split('.').slice(0, -1).join('.') !== userid)
return this.sendReply(userid + "'s custom avatar (" + Config.customAvatars[userid] + ") cannot be removed with this script.");
require('fs').unlink('./config/avatars/' + Config.customAvatars[userid], (function (e) {
if (e) return this.sendReply(userid + "'s custom avatar (" + Config.customAvatars[userid] + ") could not be removed: " + e.toString());
delete Config.customAvatars[userid];
this.sendReply(userid + "'s custom avatar removed successfully");
}).bind(this));
break;
default:
return this.sendReply("Invalid command. Valid commands are `/customavatar set, user, avatar` and `/customavatar delete, user`.");
}
};
})(),
debug: function (target, room, user, connection, cmd, message) {
if (!user.hasConsoleAccess(connection)) {
return this.sendReply('/debug - Access denied.');
}
if (!this.canBroadcast()) return;
if (!this.broadcasting) this.sendReply('||>> ' + target);
try {
var battle = room.battle;
var me = user;
if (target.indexOf('-h') >= 0 || target.indexOf('-help') >= 0) {
return this.sendReplyBox('This is a custom eval made by CreaturePhil for easier debugging.<br/>' +
'<b>-h</b> OR <b>-help</b>: show all options<br/>' +
'<b>-k</b>: object.keys of objects<br/>' +
'<b>-r</b>: reads a file<br/>' +
'<b>-p</b>: returns the current high-resolution real time in a second and nanoseconds. This is for speed/performance tests.');
}
if (target.indexOf('-k') >= 0) {
target = 'Object.keys(' + target.split('-k ')[1] + ');';
}
if (target.indexOf('-r') >= 0) {
this.sendReply('||<< Reading... ' + target.split('-r ')[1]);
return this.popupReply(eval('fs.readFileSync("' + target.split('-r ')[1] + '","utf-8");'));
}
if (target.indexOf('-p') >= 0) {
target = 'var time = process.hrtime();' + target.split('-p')[1] + 'var diff = process.hrtime(time);this.sendReply("|raw|<b>High-Resolution Real Time Benchmark:</b><br/>"+"Seconds: "+(diff[0] + diff[1] * 1e-9)+"<br/>Nanoseconds: " + (diff[0] * 1e9 + diff[1]));';
}
this.sendReply('||<< ' + eval(target));
} catch (e) {
this.sendReply('||<< error: ' + e.message);
var stack = '||' + ('' + e.stack).replace(/\n/g, '\n||');
connection.sendTo(room, stack);
}
},
reload: function (target, room, user) {
if (!this.can('reload')) return;
try {
this.sendReply('Reloading CommandParser...');
CommandParser.uncacheTree(path.join(__dirname, './', 'command-parser.js'));
CommandParser = require(path.join(__dirname, './', 'command-parser.js'));
this.sendReply('Reloading Tournaments...');
var runningTournaments = Tournaments.tournaments;
CommandParser.uncacheTree(path.join(__dirname, './', './tournaments/index.js'));
Tournaments = require(path.join(__dirname, './', './tournaments/index.js'));
Tournaments.tournaments = runningTournaments;
this.sendReply('Reloading Core...');
CommandParser.uncacheTree(path.join(__dirname, './', './core.js'));
Core = require(path.join(__dirname, './', './core.js')).core;
this.sendReply('Reloading Components...');
CommandParser.uncacheTree(path.join(__dirname, './', './components.js'));
Components = require(path.join(__dirname, './', './components.js'));
this.sendReply('Reloading SysopAccess...');
CommandParser.uncacheTree(path.join(__dirname, './', './core.js'));
SysopAccess = require(path.join(__dirname, './', './core.js'));
return this.sendReply('|raw|<font color="green">All files have been reloaded.</font>');
} catch (e) {
return this.sendReply('|raw|<font color="red">Something failed while trying to reload files:</font> \n' + e.stack);
}
},
db: 'database',
database: function (target, room, user) {
if (!this.can('db')) return;
if (!target) return user.send('|popup|You must enter a target.');
try {
var log = fs.readFileSync(('config/' + target + '.csv'), 'utf8');
return user.send('|popup|' + log);
} catch (e) {
return user.send('|popup|Something bad happen:\n\n ' + e.stack);
}
},
cp: 'controlpanel',
controlpanel: function (target, room, user, connection) {
if (!this.can('controlpanel')) return;
if (target.toLowerCase() === 'help') {
return this.sendReplyBox(
'/cp color, [COLOR]<br/>' +
'/cp avatar, [AVATAR COLOR URL]<br/>' +
'/cp toursize, [TOURNAMENT SIZE TO EARN MONEY]<br/>' +
'/cp money, [STANDARD/DOUBLE/QUADRUPLE]<br/>' +
'/cp winner, [WINNER ELO BONUS]<br/>' +
'/cp runnerup, [RUNNERUP ELO BONUS]<br/>'
);
}
var parts = target.split(',');
Core.profile.color = Core.stdin('control-panel', 'color');
Core.profile.avatarurl = Core.stdin('control-panel', 'avatar');
Core.tournaments.tourSize = Number(Core.stdin('control-panel', 'toursize'));
Core.tournaments.amountEarn = Number(Core.stdin('control-panel', 'money'));
Core.tournaments.winningElo = Number(Core.stdin('control-panel', 'winner'));