-
Notifications
You must be signed in to change notification settings - Fork 7
/
scripts.js
2409 lines (2168 loc) · 106 KB
/
scripts.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
/** URL of most recent script. Currently the master branch of my GitHub. */
var REMOTE_SCRIPT_URL = "https://raw.github.com/sarenji/poserver/master/scripts.js"
/** URL of most recent tiers.yml. Currently the master branch of my GitHub. */
var REMOTE_TIERS_URL = "https://raw.github.com/sarenji/poserver/master/tiers.yml"
/** User authentication constants */
var USER = 0;
var MODERATOR = 1;
var ADMINISTRATOR = 2;
var OWNER = 3;
var AUTH_VALUES = {
OWNER : OWNER,
ADMIN : ADMINISTRATOR,
ADMINISTRATOR : ADMINISTRATOR,
MOD : MODERATOR,
MODERATOR : MODERATOR,
USER : USER
};
var SAY_LEVEL = OWNER;
/** Other constants */
var SCRIPTS_URL = "scripts.js";
var SCRIPTS_BACKUP_URL = "new_" + SCRIPTS_URL;
var MODERATOR_MAX_BAN_LENGTH = 24 * 60 * 60; // in seconds
var MAIN_CHANNEL = "Dragonspiral Tower";
var dreamWorldPokemon = {};
var silence = false;
var battlesStopped = false;
var lagcontrol = false;
var lcperiod = 10;
var lccount = 0;
var lcmax = 2;
var pokemonHash = {};
var counter = 1, pokemonName;
while ((pokemonName = sys.pokemon(counter)) !== "Missingno") {
pokemonHash[pokemonName] = counter;
counter++;
}
var Tournament = (function() {
var TOURNAMENT_INACTIVE = 0;
var TOURNAMENT_SIGNUP = 1;
var TOURNAMENT_ACTIVE = 2;
function Tournament() {
this.initialize();
}
Tournament.prototype.initialize = function() { // Flush all existing data as a safeguard
this.tier = "";
this.state = TOURNAMENT_INACTIVE;
this.round = 0;
this.numSpots = 0;
this.players = [];
this.pairings = [];
this.matches = [];
this.numPlayers = 0;
this.losers = {};
this.channelId = sys.channelId("Tournaments");
};
Tournament.prototype.announce = function() {
var args = toArray(arguments);
args.push(this.channelId);
announce.apply(null, args);
};
Tournament.prototype.announceHTML = function() {
var args = toArray(arguments);
args.push(this.channelId);
announceHTML.apply(null, args);
};
Tournament.prototype.create = function(user, tier, spots) {
var tierList = sys.getTierList();
if (tierList.indexOf(tier) === -1) { // Check for a valid tier
this.announce(user.id, tier + " is not a valid tier.");
this.announce(user.id, "The valid tiers are " + tierList.join(", ") + ".");
return;
}
if (this.state != TOURNAMENT_INACTIVE) { // Check if a tournament is already in progress
this.announce(user.id, "A tournament is already underway!");
return;
}
if (isNaN(spots) === true || spots < 2 || spots > 128) { // Check if number of tour spots is valid
this.announce(user.id, "You must specify a number no less than 2 but no greater than 128.");
return;
}
// initialization
this.initialize();
this.state = TOURNAMENT_SIGNUP;
this.tier = tier;
this.numSpots = parseInt(spots, 10);
this.announce(user.name + " started a tournament!");
var table = "<center><table style=\"width:100%;border-spacing:0;border-collapse:collapse;border:solid #000;border-width:1px;\">";
table += "<tr>";
table += "<th colspan='2' style=\"font-size:1em;border:1px solid #000;padding:3px 7px 2px 7px;text-align:center;border-width:0 1px;padding: .3em;border-width: 1px;background:#6363B0;color:#fff;font-size:.846em;padding:.5em;white-space:nowrap;text-align:center;\">Tournament Announcement</th>";
table += "</tr>";
table += "<tr><td><b>Tier:</b> " + this.tier + "</td>";
table += "<td><b>Players:</b> " + this.numSpots + "</td></tr>";
table += "<tr style=\"background:#ccf;\">";
table += "<td colspan='2'>Go to the #Tournaments channel and type /join to participate!</td>";
table += "</tr>";
table += "</table></center>";
announceHTML(table); // Print out a pretty table
return;
};
Tournament.prototype.join = function(user) {
// check if there's a tournament running first.
if (this.state === TOURNAMENT_INACTIVE) {
this.announce(user.id, "There is no tournament running!");
return;
} else if (this.round > 1) {
this.announce(user.id, "You cannot join a tournament after the first round.");
return;
}
// check if a player is in the wrong tier
if (sys.tier(user.id) !== this.tier) {
this.announce(user.id, "You are in the wrong tier!");
return;
}
// check if player is/was already in tournament.
if (this.players.indexOf(user.name) !== -1) {
this.announce(user.id, "You are already in the tournament!");
return;
} else if (this.losers[user.name]) {
this.announce(user.id, "You already lost in this tournament!");
return;
}
// add player to tournament
this.players.push(user.name);
if (this.isActive() && this.findUnpairedMatches().length > 0) {
// replace a bye
this.announce(user.name + " joined the tournament!");
var substitute = user.name;
var opponent = this.substituteIn(substitute);
this.numPlayers++;
this.announce(substitute + " is now facing " + opponent + "!");
} else if (this.players.length > this.numSpots || (this.isActive() && this.players.length > this.numPlayers)) {
// add a sub
this.announce(user.name + " joined the tournament as substitute #" + (this.players.length - this.numPlayers) + "!");
} else {
this.numPlayers++;
this.announce(user.name + " joined the tournament! Now " + this.players.length + "/" + this.numSpots + " filled.");
}
// start tour when applicable
if (this.players.length === this.numSpots && !this.isActive()) {
this.state = TOURNAMENT_ACTIVE;
this.advanceRound();
}
};
Tournament.prototype.changecount = function(user, newNum) {
if (this.state !== TOURNAMENT_SIGNUP) {
this.announce(user.id, "The tournament is not in the signup stage.");
return;
}
if (isNaN(newNum) === true || newNum < 2 || newNum > 128) {
this.announce(user.id, "You must specify a number no less than 2 but no greater than 128.");
return;
}
this.numSpots = parseInt(newNum, 10);
this.announce("The tournament is now " + newNum + "-player.");
// start tour when applicable
if (this.players.length >= this.numSpots) {
this.state = TOURNAMENT_ACTIVE;
this.advanceRound();
}
};
Tournament.prototype.tick = function(winner, loser) {
if (this.isTourBattle(winner, loser)) {
this.advanceWinner(winner, loser);
if (this.matchesLeft() === 0) {
this.advanceRound();
} else {
if (this.matchesLeft() == 1) {
this.announce(winner + " won a tournament battle against " + loser + ". " + this.matchesLeft() + " match remains.");
} else {
this.announce(winner + " won a tournament battle against " + loser + ". " + this.matchesLeft() + " matches remain.");
}
}
}
};
Tournament.prototype.forceWin = function(user, forcedWinner) {
var index = this.findMatch(forcedWinner);
if (index !== -1) {
var match = this.matches[index];
var loser = match[0] === forcedWinner ? match[1] : match[0];
this.announce(user.name + " forced " + forcedWinner + " to take the win!");
this.advanceWinner(forcedWinner, loser);
if (this.matchesLeft() === 0) {
this.advanceRound();
}
} else {
this.announce(user.id, forcedWinner + " has either lost their battle or did not join the tournament.");
}
};
Tournament.prototype.advanceWinner = function(winner, loser) {
this.removeMatch(winner, loser);
this.removePlayer(loser);
this.losers[loser] = true;
};
Tournament.prototype.advanceRound = function() {
this.round++;
if (this.round > 1) {
this.numSpots = Math.floor(this.numSpots / 2);
}
if (this.numSpots === 0) {
this.announce("We are experiencing a bug. Please notify a moderator."); // This should never happen
} else if (this.numSpots === 1) {
var userName = this.players.shift();
this.state = TOURNAMENT_INACTIVE;
announce(userName + " wins the tournament! Congratulations!");
} else {
this.makeMatchups();
this.viewRound();
}
};
Tournament.prototype.viewRound = function(user) {
if (this.state === TOURNAMENT_INACTIVE) {
announce(user.id, "There is no active tournament.");
return;
} else if (this.state === TOURNAMENT_SIGNUP) {
announce(user.id, "There are " + (this.numSpots - this.players.length) + " spots left for the " + this.tier + " tournament.");
announce(user.id, "");
for (var i = 0; i < this.players.length; i++) {
if (i < this.numSpots) {
announce(user.id, "Players: " + this.players[i]);
} else {
announce(user.id, "Subs: " + this.players[i]);
}
}
return;
}
var table = "<center><table border='1' cellpadding='5'>";
table += "<thead><tr><th colspan='2'>Round " + this.round + " — " + this.tier + "</th></tr></thead><tbody>";
for (var i = 0; i < this.pairings.length; i++) {
var match = this.pairings[i];
table += this.prettyStringMatch(match);
}
table += "</tbody></table></center>";
if (user) {
this.announceHTML(user.id, table);
if (this.players.length > this.numPlayers) {
this.announce(user.id, "Subs: " + this.players.slice(this.numPlayers).join(", "));
}
} else {
this.announceHTML(table);
if (this.players.length > this.numPlayers) {
this.announce("Subs: " + this.players.slice(this.numPlayers).join(", "));
}
}
};
Tournament.prototype.prettyStringMatch = function(match) {
var left = match[0];
var right = match[1];
var leftStyle = "";
var rightStyle = "";
var offline = "background: #bbb";
if (left === undefined) {
left = "<s>bye!</s>";
right = "<b>" + right +"</b>";
leftStyle = rightStyle = offline;
} else if (right === undefined) {
left = "<b>" + left +"</b>";
right = "<s>bye!</s>";
leftStyle = rightStyle = offline;
} else if (this.losers[left]) {
left = "<s>" + left + "</s>";
right = "<b>" + right +"</b>";
leftStyle = rightStyle = offline;
} else if (this.losers[right]) {
left = "<b>" + left +"</b>";
right = "<s>" + right + "</s>";
leftStyle = rightStyle = offline;
} else {
leftStyle = this.getBackgroundStyle(left);
rightStyle = this.getBackgroundStyle(right);
}
return "<tr><td style='" + leftStyle +"'>" + left + "</td><td style='" + rightStyle + "'>" + right + "</td></tr>";
};
Tournament.prototype.getBackgroundStyle = function(playerName) {
if (!sys.id(playerName)) {
return "background: #f33";
} else if (sys.battling(sys.id(playerName))) {
return "background: #9AEDC6";
} else if (sys.tier(sys.id(playerName)) != this.tier) {
return "background: #fcc";
}
return "";
};
Tournament.prototype.makeMatchups = function() {
var len = Math.min(this.players.length, this.numSpots);
var seen = {};
var players = [];
for (var i = 0; i < this.players.length; i++) {
var player = this.players[i];
if (!seen[player]) {
seen[player] = true;
players.push(player);
}
}
this.players = players;
// randomize
while (--len > 0) {
var rand = Math.floor(Math.random() * (len + 1));
var tmp = this.players[len];
this.players[len] = this.players[rand];
this.players[rand] = tmp;
}
len = Math.min(this.numSpots, this.players.length);
this.matches = [];
this.pairings = [];
if (len === 3) {
this.matches.push([this.players[0], this.players[1]]);
this.matches.push([this.players[1], this.players[2]]);
this.matches.push([this.players[0], this.players[2]]);
this.pairings.push([this.players[0], this.players[1]]);
this.pairings.push([this.players[1], this.players[2]]);
this.pairings.push([this.players[0], this.players[2]]);
} else {
for (var i = 0; i < len; i += 2) {
this.matches.push([this.players[i], this.players[i + 1]]);
this.pairings.push([this.players[i], this.players[i + 1]]);
}
if (len % 2 === 1) {
this.matches.push([this.players[len - 1], undefined]);
this.pairings.push([this.players[len - 1], undefined]);
}
}
};
Tournament.prototype.isTourBattle = function(userName1, userName2) {
return this.findMatch(userName1, userName2) !== -1;
};
Tournament.prototype.findMatch = function(userName1, userName2) {
for (var i = 0; i < this.matches.length; i++) {
var match = this.matches[i];
if (!userName2) {
if (match[0] === userName1 || match[1] === userName1) {
return i;
}
} else if ((match[0] === userName1 && match[1] === userName2)
|| (match[0] === userName2 && match[1] === userName1)) {
return i;
}
}
return -1;
};
Tournament.prototype.findMatches = function(userName1, userName2) {
var matches = [];
for (var i = 0; i < this.matches.length; i++) {
var match = this.matches[i];
if (!userName2) {
if (match[0] === userName1 || match[1] === userName1) {
matches.push(match);
}
} else if ((match[0] === userName1 && match[1] === userName2)
|| (match[0] === userName2 && match[1] === userName1)) {
matches.push(match);
}
}
return matches;
};
Tournament.prototype.findUnpairedMatches = function() {
var matches = [];
for (var i = 0; i < this.matches.length; i++) {
var match = this.matches[i];
if (match[0] === undefined || match[1] === undefined) {
matches.push(match);
}
}
return matches;
};
Tournament.prototype.removeMatch = function(userName1, userName2) {
var index = this.findMatch(userName1, userName2);
if (index !== -1) {
this.matches.splice(index, 1);
}
};
Tournament.prototype.matchesLeft = function() {
var numUnpaired = this.findUnpairedMatches().length;
return this.matches.length - numUnpaired;
};
Tournament.prototype.drop = function(user, playerName) {
var index = this.players.indexOf(playerName);
if (index !== -1) {
this.announce(user.name + " dropped " + playerName + " from the tournament!");
this.removePlayer(playerName);
if (this.isActive() && this.matchesLeft() === 0) {
this.advanceRound();
}
} else {
this.announce(user.id, playerName + " is not in the tournament!");
}
}
Tournament.prototype.dropout = function(user) {
var index = this.players.indexOf(user.name);
if (index !== -1) {
this.announce(user.name + " dropped out of the tournament!");
this.removePlayer(user.name);
if (this.isActive() && this.matchesLeft() === 0) {
this.advanceRound();
}
} else {
this.announce(user.id, "You are not in the tournament!");
}
};
Tournament.prototype.removePlayer = function(userName) {
var index = this.players.indexOf(userName);
this.players.splice(index, 1);
if (index < this.numPlayers) {
this.numPlayers--;
}
// find the player's matches.
var matches = this.findMatches(userName);
if (matches.length === 0) {
return;
}
// remove player from matches
this.replaceWith(matches, userName, undefined);
this.replaceWith(this.pairings, userName, undefined);
// either sub or give a bye.
if (this.players.length > this.numPlayers) {
var substitute = this.players[this.numPlayers];
var opponent = this.substituteIn(substitute);
this.numPlayers++;
this.announce(substitute + " will be subbing in for " + userName + "!");
this.announce("New match: " + substitute + " vs. " + opponent + "!");
} else {
var match = matches[0];
var opponent = match[0] === undefined ? match[1] : match[0];
this.announce(opponent + " gets a bye!");
}
};
// returns opponent
Tournament.prototype.substituteIn = function(userName) {
this.replaceWith(this.pairings, undefined, userName, true);
return this.replaceWith(this.matches, undefined, userName, true);
};
Tournament.prototype.substitute = function(userName, substitute) { // Fix this
// remove substitute from list of players if applicable.
var index = this.players.indexOf(substitute);
if (index >= 0) this.players.splice(index, 1);
// replace substitute in list of players
index = this.players.indexOf(userName);
this.players[index] = substitute;
// substitute old player with new
this.replaceWith(this.matches, userName, undefined);
this.replaceWith(this.pairings, userName, undefined);
var opponent = this.substituteIn(substitute);
// announce that a substitute took place
this.announce(substitute + " will be subbing in for " + userName + "!");
this.announce("New match: " + substitute + " vs. " + opponent + "!");
};
Tournament.prototype.replaceWith = function(array, user, withUser, returnNow) {
var opponent = undefined;
for (var i = 0; i < array.length; i++) {
var match = array[i];
if (match[0] === user) {
match[0] = withUser;
opponent = match[1];
if (returnNow) return opponent;
} else if (match[1] === user) {
match[1] = withUser;
opponent = match[0];
if (returnNow) return opponent;
}
}
return opponent;
};
Tournament.prototype.stop = function(user) {
if (this.state !== TOURNAMENT_INACTIVE) {
this.state = TOURNAMENT_INACTIVE;
this.round = 0;
this.announce(user.name+" canceled the tournament!");
} else {
this.announce(user.id, "There is no tournament running!");
}
};
Tournament.prototype.isActive = function() {
return this.state === TOURNAMENT_ACTIVE;
};
return new Tournament();
})();
function User(id) {
this.id = id;
this.ip = sys.ip(id);
this.name = sys.name(id);
this.auth = sys.auth(id);
this.registered = sys.dbRegistered(this.name);
this.muted = false;
this.lastMessages = [];
this.lastMessageTime = 0;
this.idle = sys.away(id);
this.tier = sys.tier(id);
this.channelId = 0;
this.ratedBattles = sys.ratedBattles(this.id) || 0;
var key = makeKey(this.name, "first-seen");
this.firstSeen = getValue(key, getTime());
setValue(key, this.firstSeen);
}
User.prototype.authedFor = function(auth) {
return this.auth >= auth;
}
User.prototype.run = function(command, args, channelId) {
this.channelId = channelId;
if (command in commands) {
commands[command].apply(this, args);
}
}
User.prototype.log = function(message) {
// why is this needed?
if (!this.lastMessages) {
this.lastMessages = [];
}
if (this.cantTalk(message)) {
return false;
}
this.lastMessages.unshift(message);
if (this.lastMessages.length > 5) {
this.lastMessages.pop();
}
this.lastMessageTime = getTime();
return true;
}
User.prototype.cantTalk = function(message) {
if (this.authedFor(MODERATOR)) {
return false;
}
if (timeDelta(this.lastMessageTime) < 50) {
return true;
}
// repeated links
var matches = message.match(/(?:http|https|ftp)\:\/\//gi);
if (matches && matches.length >= 2) {
var key = makeKey(this.name, "chat:last-links");
if (getTime() - getValue(key) < 30 * 1000) {
var banLength = 5 * 60; // 5 mins
ban(this.name, getTime() + banLength * 1000);
announce(this.name + " was automatically banned for " + prettyPrintTime(banLength) + ". (Too many links.)");
} else {
kick(this.name);
announce(this.name + " was automatically kicked for saying one too many links.");
}
setValue(key, getTime());
return true;
}
if (this.lastMessages.length > 0) {
// repeated messages
if (this.lastMessages[0] === message) {
kick(this.name);
return true;
}
}
return false;
}
User.prototype.outranks = function(other) {
if (typeof other === "number") {
return this.auth > other;
} else {
return this.auth > other.auth;
}
}
function getPlayer(player_name) {
var player_id = sys.id(player_name);
return SESSION.users(player_id);
}
// temporary until i figure out a nicer way of doing this.
var help = [
[
"** BASIC USER COMMANDS",
"/ranking -- See your own ranking.",
"/dw -- See a list of released Dream World Pokemon.",
"/tiers -- See a link to Smogon's Tiers.",
"/clearpass -- Clear your own password.",
"/idle -- Sets your status to idle, which blocks challenges. Aliased to /away.",
"/selfkick -- Kicks all \"ghosts\" logged in under your IP.",
"* TOURNAMENT COMMANDS",
"/join -- Enters you into a tournament if one is running.",
"/drop -- Removes yourself from a tournament. Aliased to /dropout.",
"/viewround -- Shows the current round's matchups and subtitutes."
], [
"** DRIVER COMMANDS",
"/kick user -- kicks user from the server. Aliased to /k.",
"/ban user -- bans user for " + MODERATOR_MAX_BAN_LENGTH / 60 / 60 + " hours.",
"/ban user:duration -- duration is in hours. Maximum of " + MODERATOR_MAX_BAN_LENGTH / 60 / 60 + " hours.",
"/b is aliased to /ban.",
"/unban user",
"/mute -- mutes whole server",
"/mute user",
"/mute user:duration",
"/unmute user",
"/wall message",
"/ip user -- returns player's IP address",
"/aliases ip -- returns all alts associated with the given IP",
"/alts user -- essentially combines the above commands",
"/bancheck user -- reports whether the user is banned and, if so, for how long.",
"/addnote user:note -- adds administrative note for registered users (limit 1 per user).",
"/delnote user -- deletes administrative note.",
"/deletenote user is aliased to /delnote.",
"/viewnote user -- views administrative note.",
"/note user is aliased to /note.",
"* TOURNAMENT COMMANDS",
"/tour tier:participants -- Starts a tournament.",
"/drop user -- Removes the user from the tournament. Aliased to /dropout.",
"/stop -- Stops the current tournament. Aliased to /cancel",
"/changecount -- Changes the number of participants in a tournament.",
"/changetier user:tier -- Changes user's tier. Will fail if user does not have a valid team for the tier."
], [
"** MODERATOR COMMANDS",
"/ban user -- bans user for 1 day.",
"/ban user:1y2M3w4d5h6m7s -- bans the user for the entered time.",
"/b is aliased to /ban.",
"/kickall ip -- kicks all users with given IP address.",
"/mute user:1y2M3w4d5h6m7s -- mutes the user for the entered time.",
"/silence -- Silences the entire chat.",
"/unsilence -- Lifts silence.",
"/permaban -- Permanently bans a user. Aliased to /pb and /permban.",
"/ipban ip -- bans by IP address. Only works if the user is still online.",
"/topic -- Changes the topic.",
"/lagcontrol [num[:time]] -- Only let num people log in per time seconds (default: 2:10 or last used).",
"/nolagcontrol -- Disable lag control."
], [
"** ADMINISTRATOR COMMANDS",
"/resetLadder tier -- resets the ratings for the specified tier.",
"/resetPlayerRating name:tier -- resets the rating of the user in the tier to 1000.",
"/pullLogs name:num -- pull all battle logs for user for the past num days and put them on the web server.",
"/clearpass user -- Clear user's password.",
"/destroy channel -- Deletes a channel.",
"/stopBattles -- Prevents new battles from starting (useful if server needs to be restarted).",
"/playerCount -- Prints number of players logged onto the server.",
"/fixRegistry -- If server falls off the registry, run this, and it'll add it back."
]
];
/** All of these commands are run in the context of a User object. */
var commands = {};
function addCommand(commandName, func) {
if (typeof commandName == "string") {
commands[commandName] = func;
} else {
for (var i = 0; i < commandName.length; i++) {
commands[commandName[i]] = func;
}
}
}
function addAuthCommand(auth, commandName, func) {
addCommand(commandName, function() {
if (this.authedFor(auth)) {
func.apply(this, arguments);
}
});
}
function addModCommand(commandName, func) {
addAuthCommand(MODERATOR, commandName, func);
}
function addAdminCommand(commandName, func) {
addAuthCommand(ADMINISTRATOR, commandName, func);
}
function addOwnerCommand(commandName, func) {
addAuthCommand(OWNER, commandName, func);
}
addCommand([ "help", "commands" ], function() {
for (var i = 0; i <= this.auth; i++) {
for (var j = 0; j < help[i].length; j++) {
announce(this.id, help[i][j]);
}
}
});
addOwnerCommand("mod", function(playerName) {
changeAuthIfLessThan(playerName, MODERATOR);
});
addOwnerCommand("admin", function(playerName) {
changeAuthIfLessThan(playerName, ADMINISTRATOR);
});
addOwnerCommand("owner", function(playerName) {
changeAuthIfLessThan(playerName, OWNER);
});
addOwnerCommand(["deauth", "demod", "deadmin", "deowner"], function(playerName) {
changeAuth(playerName, USER);
});
addCommand("auth", function(type, token, newAuth) {
if (type === "group") {
var group = AUTH_VALUES[token.toUpperCase()];
var list = sys.dbAuths();
list = findGroupAuthLevel(group, list).sort();
for (var i = 0, len = list.length; i < len; i++) {
announce(this.id, list[i]);
}
} else if (type === "user") {
if (newAuth) {
if (this.authedFor(OWNER)) {
changeAuth(token, newAuth);
announce(this.id, "You set " + token + "'s authority level to " + newAuth + ".");
} else {
announce(this.id, "You're not allowed to set other people's auth!");
}
} else {
var auth = getAuth(token);
announce(this.id, token + "'s authority level is " + auth + ".");
}
} else {
announce(this.id, "Invalid arguments.");
}
});
addModCommand("say", function() {
if (this.authedFor(SAY_LEVEL)) {
var stuff = toArray(arguments).join(":");
sys.sendAll(stuff, this.channelId);
announce(this.name + " said, '" + stuff + "'", sys.channelId("Staff"));
} else {
announce(this.id, "Current say level is higher than your level.");
}
});
addModCommand("saylevel", function(level) {
if (!level) {
announce(this.id, "Current say level is " + SAY_LEVEL);
} else if (this.authedFor(SAY_LEVEL)) {
level = parseInt(level, 10);
level = Math.min(level, OWNER);
SAY_LEVEL = level;
announce(this.id, "Say level set to " + level);
}
});
addCommand([ "idle", "away"], function() {
this.idle = !this.idle;
var status = this.idle ? "idle" : "available";
sys.changeAway(this.id, this.idle);
announce(this.id, "You are now " + status + ".");
});
addCommand("ranking", function(player_name) {
var player = getPlayer(player_name) || this;
var rank = sys.ranking(player.id);
var tier = sys.tier(player.id);
if (rank) {
var possessive = player_name ? player_name + "'s" : "Your";
announce(this.id, possessive + " rank in " + tier + " is " + rank
+ "/" + sys.totalPlayersByTier(tier) + " ["
+ sys.ladderRating(player.id) + " points / "
+ sys.ratedBattles(player.id) +" battles]!");
} else {
var noun = player_name ? player_name + " is" : "You are";
announce(this.id, noun + " not ranked in " + tier + " yet!");
}
});
addModCommand([ "kick", "k" ], function(player_name, reason) {
var player = getPlayer(player_name);
if (this.outranks(player)) {
var message = this.name + " kicked " + player_name + ".";
if (reason) {
message += " (" + reason + ")";
}
announce(message);
kick(player_name);
}
});
addAdminCommand("kickall", function(ip) {
var players = sys.playerIds();
var players_length = players.length;
for (var i = 0; i < players_length; ++i) {
var current_player = players[i];
if (ip == sys.ip(current_player)) {
sys.kick(current_player);
}
}
});
addModCommand("ip", function(player_name) {
if (sys.id(player_name) === undefined && sys.dbIp(player_name) === undefined) {
announce(this.id, "Invalid user name.");
} else if (sys.loggedIn(sys.id(player_name)) === true) {
announce(this.id, player_name + " is logged in with ip address " + sys.ip(sys.id(player_name)));
} else {
announce(this.id, player_name + " last logged on from ip address " + sys.dbIp(player_name));
}
});
addModCommand("aliases",function(ip) {
announce(this.id, ip + " is associated with the following usernames:" + sys.aliases(ip));
});
addModCommand("alts", function(userName) {
if (sys.id(userName) === undefined && sys.dbIp(userName) === undefined) {
announce(this.id, "Invalid user name.");
} else if (sys.loggedIn(sys.id(userName)) === true) {
announce(this.id, "Player " + userName + " (IP " + sys.ip(sys.id(userName)) + ") is associated with the following: " + sys.aliases(sys.ip(sys.id(userName))) + ".");
} else {
announce(this.id, "Player " + userName + " (IP " + sys.dbIp(userName) + ") is associated with the following: " + sys.aliases(sys.dbIp(userName)) + ".");
}
});
addModCommand("bancheck", function(playerName) {
var expireKey = makeKey(playerName, "ban:expires");
var ip = sys.dbIp(playerName);
if (ip === undefined) {
announce(this.id, "No such user!");
} else if (getValue(expireKey)) {
if ((parseInt(getValue(expireKey), 10)-getTime()) > 0) {
announce(this.id, playerName+" is banned for the next "+prettyPrintTime((parseInt(getValue(expireKey), 10)-getTime())/1000));
} else {
announce(this.id, playerName+" was banned, but the ban has expired.");
}
} else {
announce(this.id, playerName+" is not banned!");
}
});
addModCommand("playerCount", function(){
announce(this.id, "There are "+sys.numPlayers()+" players logged in right now.");
});
addOwnerCommand("reload", function() {
var id = this.id;
var name = this.name.toLowerCase();
if (name != "antar" && name != "sarenji" && name != "aldaron" && name != "haunter" && name != "desolate") {//only admins with remote access may use this script
announce(id, "You are not authorized to use this command.");
return;
}
var old_scripts = sys.getFileContent(SCRIPTS_URL);
sys.writeToFile(SCRIPTS_BACKUP_URL, old_scripts);
if (arguments.length > 0) {
var scriptURL = toArray(arguments).join(":");
sys.webCall(scriptURL, function(res) {
sys.changeScript(res, true);
sys.writeToFile(SCRIPTS_URL, res);
announce(id, "Script reloaded!");
});
} else {
sys.system("curl -k -o " + SCRIPTS_URL + " " + REMOTE_SCRIPT_URL);
var new_scripts = sys.getFileContent(SCRIPTS_URL);
sys.changeScript(new_scripts, true);
announce(id, "Script reloaded!");
}
});
addOwnerCommand("rollback", function() {
var id = this.id;
var name = this.name.toLowerCase();
if (name != "antar" && name != "sarenji" && name != "aldaron" && name != "haunter" && name != "desolate") {//only admins with remote access may use this script
announce(id, "You are not authorized to use this command.");
return;
}
var old_scripts = sys.getFileContent(SCRIPTS_BACKUP_URL);
sys.writeToFile(SCRIPTS_URL, old_scripts);
sys.changeScript(old_scripts, true);
announce(id, "Scripts rolled back!");
});
addOwnerCommand("reloadtiers", function() {
announce(this.id, "Fetching tiers.yml...");
sys.system("curl -k -o tiers.yml " + REMOTE_TIERS_URL);
announce(this.id, "Compiling tiers.yml into tiers.xml...");
// NOTE: Specific to the windows server.
// This just runs ruby tiers_compiler.rb
//sys.system("tiers_compiler.bat");
sys.system("./tiers_compiler.rb");
announce(this.id, "Reloading tiers.xml on server...");
sys.reloadTiers();
});
addOwnerCommand("resetLadder", function(tier) {
sys.resetLadder(tier);
announce(this.name + " reset the ladder for " + tier);
});
addOwnerCommand("resetPlayerRating", function(player,tier) {
sys.changeRating(player, tier, 1000);
announce(this.name + " reset the rating of " + player + " in "+tier);
});
addOwnerCommand("pullLogs", function(playerName,days) {
if (sys.id(playerName) === undefined && sys.dbIp(playerName) === undefined) {//make sure user exists
announce(this.id, "Invalid user name.");
return;
}
days = parseInt(days); //sanitize
if (days < 0) {
announce(this.id, "Invalid date range.");
return;
}
sys.system("./pullLogs.sh \""+playerName+"\" "+days+" &");
announce(this.id, "Log pull for "+playerName+" initiated.");
announce(this.id, "Look for the logs in http://po.smogon.com/Admin/battleLogs/"+playerName);
});
addModCommand("wall", function() {
var message = toArray(arguments).join(":");
announce(this.name + ": " + message);
});
addModCommand([ "ban", "b" ], function(player_name, length, reason) {
var auth = parseInt(sys.dbAuth(player_name), 10);
var ip;
if (sys.loggedIn(sys.id(player_name)) === true) {
ip = sys.ip(sys.id(player_name));
} else {
ip = sys.dbIp(player_name);
}
var message;
if (this.outranks(auth)) {
if (length && /^(\d+[mshdyMw]?)+$/.test(length)) {
length = parseLength(length);
} else {
reason = length;
length = MODERATOR_MAX_BAN_LENGTH;
}
// limit mod bans
if (this.auth === MODERATOR) {
length = Math.min(length, MODERATOR_MAX_BAN_LENGTH);
}
message = player_name + " (IP:"+ip+") was banned by " + this.name + " for " + prettyPrintTime(length) + ".";
if (reason) {
message += " (" + reason + ")";
}
sys.appendToFile("bans.txt", timeStamp()+message+"\n");
announce(message);
ban(player_name, getTime() + length * 1000);
}
});
addAdminCommand("silence", function() {
silence = true;