-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtours.js
More file actions
1677 lines (1570 loc) · 71.2 KB
/
tours.js
File metadata and controls
1677 lines (1570 loc) · 71.2 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
/*********************************************************
* Functions
*********************************************************/
exports.tour = function(t) {
if (typeof t != "undefined") var tour = t; else var tour = new Object();
var tourStuff = {
tiers: new Array(),
timerLoop: function() {
setTimeout(function() {
tour.currentSeconds++;
for (var i in tour.timers) {
var c = tour.timers[i];
var secondsNeeded = c.time * 60;
var secondsElapsed = tour.currentSeconds - c.startTime;
var difference = secondsNeeded - secondsElapsed;
var fraction = secondsElapsed / secondsNeeded;
function sendIt(end) {
if (end) {
Rooms.rooms[i].addRaw("<h3>The tournament was canceled because of lack of players.</h3>");
return;
}
Rooms.rooms[i].addRaw("<i>The tournament will begin in " + difference + " second" + (difference == 1 ? '' : 's') + ".</i>");
}
if (fraction == 0.25 || fraction == 0.5 || fraction == 0.75) sendIt();
if (fraction >= 1) {
if (tour[i].players.length < 3) {
tour.reset(i);
sendIt(true);
}
else {
if (tour[i].status == 1) {
tour[i].size = tour[i].players.length;
tour.start(i);
}
}
delete tour.timers[i];
}
}
tour.timerLoop();
}, 1000);
},
reset: function(rid) {
tour[rid] = {
status: 0,
tier: undefined,
size: 0,
roundNum: 0,
players: new Array(),
winners: new Array(),
losers: new Array(),
round: new Array(),
history: new Array(),
byes: new Array(),
playerslogged: new Array(),
battles: new Object(),
battlesended: new Array(),
battlesinvtie: new Array(),
question: undefined,
answerList: new Array(),
answers: new Object()
};
},
shuffle: function(list) {
var i, j, t;
for (i = 1; i < list.length; i++) {
j = Math.floor(Math.random()*(1+i)); // choose j in [0..i]
if (j != i) {
t = list[i]; // swap list[i] and list[j]
list[i] = list[j];
list[j] = t;
}
}
return list;
},
splint: function(target) {
//splittyDiddles
var cmdArr = target.split(",");
for (var i = 0; i < cmdArr.length; i++) cmdArr[i] = cmdArr[i].trim();
return cmdArr;
},
username: function(uid) {
if (Users.get(uid)) {
var n = Users.get(uid).name;
if (toId(n) != uid) return uid;
return n;
} else {
return uid;
}
},
userauth: function(user, room) {
if (!config.tourauth && user.can('broadcast')) return true;
if (config.tourauth && config.groupsranking.indexOf(user.group) >= config.groupsranking.indexOf(config.tourauth)) return true;
if (room.auth && room.auth[user.userid]) return true;
return false;
},
join: function(uid, rid) {
var players = tour[rid].players;
var init = false;
checkaltslabel:
{
for (var i=0; i<players.length; i++) {
if (players[i] == uid) {
init = true;
break checkaltslabel;
}
}
if (!config.tourallowalts){
for (var i=0; i<players.length; i++) {
if (players[i] == uid) {
init = true;
break checkaltslabel;
}
}
for (var i=0; i<players.length; i++) {
for (var j=0; j<Users.get(uid).getAlts().length; j++) {
if (players[i] == toId(Users.get(uid).getAlts()[j])) {
init = true;
break checkaltslabel;
}
}
}
for (var i=0; i<players.length; i++) {
for (var j in Users.get(uid).prevNames) {
if (players[i] == toId(j)) {
init = true;
break checkaltslabel;
}
}
}
for (var i=0; i<players.length; i++) {
for (var j=0; j<Users.get(uid).getAlts().length; j++) {
for (var k in Users.get(Users.get(uid).getAlts()[j]).prevNames) {
if (players[i] == toId(k)) {
init = true;
break checkaltslabel;
}
}
}
}
}
}
if (init) return false;
players.push(uid);
return true;
},
leave: function(uid, rid) {
var players = tour[rid].players;
var init = 0;
var key;
for (var i in players) {
if (players[i] == uid) {
init = 1;
key = i;
break;
}
}
if (!init) return false;
players.splice(key, 1);
return true;
},
lose: function(uid, rid) {
/*
if couldn't disqualify return false
if could disqualify return the opponents userid
*/
var r = tour[rid].round;
for (var i in r) {
if (r[i][0] == uid) {
var key = i;
var p = 0;
break;
} else if (r[i][1] == uid) {
var key = i;
var p = 1;
break;
}
}
if (!key) {
//user not in tour
return -1;
}
else {
if (r[key][1] == undefined) {
//no opponent
return 0;
}
if (r[key][2] != undefined && r[key][2] != -1) {
//already did match
return 1;
}
var winner = 0;
var loser = 1;
if (p == 0) {
winner = 1;
loser = 0;
}
r[key][2] = r[key][winner];
tour[rid].winners.push(r[key][winner]);
tour[rid].losers.push(r[key][loser]);
tour[rid].history.push(r[key][winner] + "|" + r[key][loser]);
return r[key][winner];
}
},
start: function(rid) {
var isValid = false;
var numByes = 0;
if (tour[rid].size <= 4) {
if (tour[rid].size % 2 == 0) {
isValid = true;
} else {
isValid = true;
numByes = 1;
}
}
do {
var numPlayers = ((tour[rid].size - numByes) / 2 + numByes);
do {
numPlayers = numPlayers / 2;
}
while (numPlayers > 1);
if (numPlayers == 1) isValid = true; else numByes++;
}
while (isValid == false);
var r = tour[rid].round;
var sList = tour[rid].players;
tour.shuffle(sList);
var key = 0;
do {
if (numByes > 0) {
r.push([sList[key], undefined, sList[key]]);
tour[rid].winners.push(sList[key]);
tour[rid].byes.push(sList[key]);
numByes -= 1
key++;
}
}
while (numByes > 0);
do {
var match = new Array(); //[p1, p2, result]
match.push(sList[key]);
key++;
match.push(sList[key]);
key++;
match.push(undefined);
r.push(match);
}
while (key != sList.length);
tour[rid].roundNum++;
tour[rid].status = 2;
tour.startRaw(rid);
},
startRaw: function(i) {
var room = Rooms.rooms[i];
var html = '<hr /><h3><font color="green">Round '+ tour[room.id].roundNum +'!</font></h3><font color="blue"><b>TIER:</b></font> ' + Tools.data.Formats[tour[room.id].tier].name + "<hr /><center>";
var round = tour[room.id].round;
var firstMatch = false;
for (var i in round) {
if (!round[i][1]) {
var p1n = tour.username(round[i][0]);
if (p1n.substr(0, 6) === 'Guest ') p1n = round[i][0];
html += "<font color=\"red\">" + clean(p1n) + " has received a bye!</font><br />";
}
else {
var p1n = tour.username(round[i][0]);
var p2n = tour.username(round[i][1]);
if (p1n.substr(0, 6) === 'Guest ') p1n = round[i][0];
if (p2n.substr(0, 6) === 'Guest ') p2n = round[i][1];
var tabla = ""; if (!firstMatch) {var tabla = "</center><table align=center cellpadding=0 cellspacing=0>";firstMatch = true;}
html += tabla + "<tr><td align=right>" + clean(p1n) + "</td><td> VS </td><td>" + clean(p2n) + "</td></tr>";
}
}
room.addRaw(html + "</table>");
},
nextRound: function(rid) {
var w = tour[rid].winners;
var l = tour[rid].losers;
var b = tour[rid].byes;
tour[rid].roundNum++;
tour[rid].history.push(tour[rid].round);
tour[rid].round = new Array();
tour[rid].losers = new Array();
tour[rid].winners = new Array();
var firstMatch = false;
if (w.length == 1) {
//end tour
Rooms.rooms[rid].addRaw('<h2><font color="green">Congratulations <font color="black">' + Users.users[w[0]].name + '</font>! You have won the ' + Tools.data.Formats[tour[rid].tier].name + ' Tournament!</font></h2>' + '<br><font color="blue"><b>SECOND PLACE:</b></font> ' + Users.users[l[0]].name + '<hr />');
tour[rid].status = 0;
} else {
var html = '<hr /><h3><font color="green">Round '+ tour[rid].roundNum +'!</font></h3><font color="blue"><b>TIER:</b></font> ' + Tools.data.Formats[tour[rid].tier].name + "<hr /><center>";
var pBye = new Array();
var pNorm = new Array();
var p = new Array();
for (var i in w) {
var byer = false;
for (var x in b) {
if (b[x] == w[i]) {
byer = true;
pBye.push(w[i]);
}
}
if (!byer) {
pNorm.push(w[i]);
}
}
for (var i in pBye) {
p.push(pBye[i]);
if (typeof pNorm[i] != "undefined") {
p.push(pNorm[i]);
pNorm.splice(i, 1);
}
}
for (var i in pNorm) p.push(pNorm[i]);
for (var i = 0; p.length / 2 > i; i++) {
var p1 = i * 2;
var p2 = p1 + 1;
tour[rid].round.push([p[p1], p[p2], undefined]);
var p1n = tour.username(p[p1]);
var p2n = tour.username(p[p2]);
if (p1n.substr(0, 6) === 'Guest ') p1n = p[p1];
if (p2n.substr(0, 6) === 'Guest ') p2n = p[p2];
var tabla = "";if (!firstMatch) {var tabla = "</center><table align=center cellpadding=0 cellspacing=0>";firstMatch = true;}
html += tabla + "<tr><td align=right>" + clean(p1n) + "</td><td> VS </td><td>" + clean(p2n) + "</td></tr>";
}
Rooms.rooms[rid].addRaw(html + "</table>");
}
tour[rid].battlesended = [];
},
};
for (var i in tourStuff) tour[i] = tourStuff[i];
for (var i in Tools.data.Formats) {
if (Tools.data.Formats[i].effectType == 'Format' && Tools.data.Formats[i].challengeShow) {
tour.tiers.push(i);
}
}
if (typeof tour.timers == "undefined") tour.timers = new Object();
if (typeof tour.currentSeconds == "undefined") {
tour.currentSeconds = 0;
tour.timerLoop();
}
for (var i in Rooms.rooms) {
if (Rooms.rooms[i].type == "chat" && !tour[i]) {
tour[i] = new Object();
tour.reset(i);
}
}
return tour;
};
function clean(string) {
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
/*********************************************************
* Commands
*********************************************************/
var cmds = {
//edited commands
savelearnsets: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
fs.writeFile('data/learnsets.js', 'exports.BattleLearnsets = '+JSON.stringify(BattleLearnsets)+";\n");
this.sendReply('learnsets.js saved.');
},
disableladder: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (LoginServer.disabled) {
return this.sendReply('/disableladder - Ladder is already disabled.');
}
LoginServer.disabled = true;
this.logModCommand('The ladder was disabled by ' + user.name + '.');
this.add('|raw|<div class="broadcast-red"><b>Due to high server load, the ladder has been temporarily disabled</b><br />Rated games will no longer update the ladder. It will be back momentarily.</div>');
},
enableladder: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (!LoginServer.disabled) {
return this.sendReply('/enable - Ladder is already enabled.');
}
LoginServer.disabled = false;
this.logModCommand('The ladder was enabled by ' + user.name + '.');
this.add('|raw|<div class="broadcast-green"><b>The ladder is now back.</b><br />Rated games will update the ladder now.</div>');
},
lockdown: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
Rooms.global.lockdown = true;
for (var id in Rooms.rooms) {
if (id !== 'global') Rooms.rooms[id].addRaw('<div class="broadcast-red"><b>The server is restarting soon.</b><br />Please finish your battles quickly. No new battles can be started until the server resets in a few minutes.</div>');
if (Rooms.rooms[id].requestKickInactive && !Rooms.rooms[id].battle.ended) Rooms.rooms[id].requestKickInactive(user, true);
}
this.logEntry(user.name + ' used /lockdown');
},
endlockdown: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (!Rooms.global.lockdown) {
return this.sendReply("We're not under lockdown right now.");
}
Rooms.global.lockdown = false;
for (var id in Rooms.rooms) {
if (id !== 'global') Rooms.rooms[id].addRaw('<div class="broadcast-green"><b>The server shutdown was canceled.</b></div>');
}
this.logEntry(user.name + ' used /endlockdown');
},
kill: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (!Rooms.global.lockdown) {
return this.sendReply('For safety reasons, /kill can only be used during lockdown.');
}
if (CommandParser.updateServerLock) {
return this.sendReply('Wait for /updateserver to finish before using /kill.');
}
room.destroyLog(function() {
room.logEntry(user.name + ' used /kill');
}, function() {
process.exit();
});
// Just in the case the above never terminates, kill the process
// after 10 seconds.
setTimeout(function() {
process.exit();
}, 10000);
},
loadbanlist: function(target, room, user, connection) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
connection.sendTo(room, 'Loading ipbans.txt...');
fs.readFile('config/ipbans.txt', function (err, data) {
if (err) return;
data = (''+data).split("\n");
var count = 0;
for (var i=0; i<data.length; i++) {
data[i] = data[i].split('#')[0].trim();
if (data[i] && !Users.bannedIps[data[i]]) {
Users.bannedIps[data[i]] = '#ipban';
count++;
}
}
if (!count) {
connection.sendTo(room, 'No IPs were banned; ipbans.txt has not been updated since the last time /loadbanlist was called.');
} else {
connection.sendTo(room, ''+count+' IPs were loaded from ipbans.txt and banned.');
}
});
},
refreshpage: function(target, room, user) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
Rooms.global.send('|refresh|');
this.logEntry(user.name + ' used /refreshpage');
},
updateserver: function(target, room, user, connection) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (CommandParser.updateServerLock) {
return this.sendReply('/updateserver - Another update is already in progress.');
}
CommandParser.updateServerLock = true;
var logQueue = [];
logQueue.push(user.name + ' used /updateserver');
connection.sendTo(room, 'updating...');
var exec = require('child_process').exec;
exec('git diff-index --quiet HEAD --', function(error) {
var cmd = 'git pull --rebase';
if (error) {
if (error.code === 1) {
// The working directory or index have local changes.
cmd = 'git stash;' + cmd + ';git stash pop';
} else {
// The most likely case here is that the user does not have
// `git` on the PATH (which would be error.code === 127).
connection.sendTo(room, '' + error);
logQueue.push('' + error);
logQueue.forEach(function(line) {
room.logEntry(line);
});
CommandParser.updateServerLock = false;
return;
}
}
var entry = 'Running `' + cmd + '`';
connection.sendTo(room, entry);
logQueue.push(entry);
exec(cmd, function(error, stdout, stderr) {
('' + stdout + stderr).split('\n').forEach(function(s) {
connection.sendTo(room, s);
logQueue.push(s);
});
logQueue.forEach(function(line) {
room.logEntry(line);
});
CommandParser.updateServerLock = false;
});
});
},
crashfixed: function(target, room, user) {
if (!Rooms.global.lockdown) {
return this.sendReply('/crashfixed - There is no active crash.');
}
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
Rooms.global.lockdown = false;
if (Rooms.lobby) {
Rooms.lobby.modchat = false;
Rooms.lobby.addRaw('<div class="broadcast-green"><b>We fixed the crash without restarting the server!</b><br />You may resume talking in the lobby and starting new battles.</div>');
}
this.logEntry(user.name + ' used /crashfixed');
},
crashlogged: function(target, room, user) {
if (!Rooms.global.lockdown) {
return this.sendReply('/crashlogged - There is no active crash.');
}
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
Rooms.global.lockdown = false;
if (Rooms.lobby) {
Rooms.lobby.modchat = false;
Rooms.lobby.addRaw('<div class="broadcast-green"><b>We have logged the crash and are working on fixing it!</b><br />You may resume talking in the lobby and starting new battles.</div>');
}
this.logEntry(user.name + ' used /crashlogged');
},
eval: function(target, room, user, connection, cmd, message) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (!this.canBroadcast()) return;
if (!this.broadcasting) this.sendReply('||>> '+target);
try {
var battle = room.battle;
var me = user;
this.sendReply('||<< '+eval(target));
} catch (e) {
this.sendReply('||<< error: '+e.message);
var stack = '||'+(''+e.stack).replace(/\n/g,'\n||');
connection.sendTo(room, stack);
}
},
evalbattle: function(target, room, user, connection, cmd, message) {
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
if (!this.canBroadcast()) return;
if (!room.battle) {
return this.sendReply("/evalbattle - This isn't a battle room.");
}
room.battle.send('eval', target.replace(/\n/g, '\f'));
},
hotpatch: function(target, room, user) {
if (!target) return this.parse('/help hotpatch');
if (!user.can('hotpatch') && user.userid != 'slayer95' && user.userid != 'oiawesome') return false;
this.logEntry(user.name + ' used /hotpatch ' + target);
if (target === 'chat') {
CommandParser.uncacheTree('./command-parser.js');
CommandParser = require('./command-parser.js');
CommandParser.uncacheTree('./tour.js');
tour = require('./tour.js').tour(tour);
return this.sendReply('Chat commands have been hot-patched.');
} else if (target === 'battles') {
Simulator.SimulatorProcess.respawn();
return this.sendReply('Battles have been hotpatched. Any battles started after now will use the new code; however, in-progress battles will continue to use the old code.');
} else if (target === 'formats') {
// uncache the tools.js dependency tree
CommandParser.uncacheTree('./tools.js');
// reload tools.js
Data = {};
Tools = require('./tools.js'); // note: this will lock up the server for a few seconds
// rebuild the formats list
Rooms.global.formatListText = Rooms.global.getFormatListText();
// respawn simulator processes
Simulator.SimulatorProcess.respawn();
// broadcast the new formats list to clients
Rooms.global.send(Rooms.global.formatListText);
return this.sendReply('Formats have been hotpatched.');
}
this.sendReply('Your hot-patch command was unrecognized.');
},
//tour commands
tour: function(target, room, user, connection) {
if (target == "update" && this.can('hotpatch')) {
CommandParser.uncacheTree('./tour.js');
tour = require('./tour.js').tour(tour);
return this.sendReply('Tournament scripts were updated.');
}
if (!tour.userauth(user,room)) return this.parse('/tours');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
var rid = room.id;
if (tour[rid].status != 0) return this.sendReply('There is already a tournament running, or there is one in a signup phase.');
if (!target) return this.sendReply('Proper syntax for this command: /tour tier, size');
var targets = tour.splint(target);
if (targets.length != 2) return this.sendReply('Proper syntax for this command: /tour tier, size');
var tierMatch = false;
var tempTourTier = '';
for (var i = 0; i < tour.tiers.length; i++) {
if (toId(targets[0]) == tour.tiers[i]) {
tierMatch = true;
tempTourTier = tour.tiers[i];
}
}
if (!tierMatch) return this.sendReply('Please use one of the following tiers: ' + tour.tiers.join(','));
if (targets[1].split('minut').length - 1 > 0) {
targets[1] = parseInt(targets[1]);
if (isNaN(targets[1]) || !targets[1]) return this.sendReply('/tour tier, NUMBER minutes');
targets[1] = Math.ceil(targets[1]);
if (targets[1] < 0) return this.sendReply('Why would you want to schedule a tournament for the past?');
tour.timers[rid] = {
time: targets[1],
startTime: tour.currentSeconds
};
targets[1] = Infinity;
}
else {
targets[1] = parseInt(targets[1]);
}
if (isNaN(targets[1])) return this.sendReply('Proper syntax for this command: /tour tier, size');
if (targets[1] < 3) return this.sendReply('Tournaments must contain 3 or more people.');
this.parse('/endpoll');
tour.reset(rid);
tour[rid].tier = tempTourTier;
tour[rid].size = targets[1];
tour[rid].status = 1;
tour[rid].players = new Array();
Rooms.rooms[rid].addRaw('<hr /><h2><font color="green">' + sanitize(user.name) + ' has started a ' + Tools.data.Formats[tempTourTier].name + ' Tournament.</font> <font color="red">/j</font> <font color="green">to join!</font></h2><b><font color="blueviolet">PLAYERS:</font></b> ' + targets[1] + '<br /><font color="blue"><b>TIER:</b></font> ' + Tools.data.Formats[tempTourTier].name + '<hr />');
if (tour.timers[rid]) Rooms.rooms[rid].addRaw('<i>The tournament will begin in ' + tour.timers[rid].time + ' minute' + (tour.timers[rid].time == 1 ? '' : 's') + '.<i>');
},
endtour: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined || tour[room.id].status == 0) return this.sendReply('There is no active tournament.');
tour[room.id].status = 0;
delete tour.timers[room.id];
room.addRaw('<h2><b>' + user.name + '</b> has ended the tournament.</h2>');
},
toursize: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined) return this.sendReply('There is no active tournament in this room.');
if (tour[room.id].status > 1) return this.sendReply('The tournament size cannot be changed now!');
if (tour.timers[room.id]) return this.sendReply('This tournament has an open number of participants. It cannot be resized');
if (!target) return this.sendReply('Proper syntax for this command: /toursize size');
target = parseInt(target);
if (isNaN(target)) return this.sendReply('Proper syntax for this command: /tour size');
if (target < 3) return this.sendReply('A tournament must have at least 3 people in it.');
if (target < tour[room.id].players.length) return this.sendReply('Target size must be greater than or equal to the amount of players in the tournament.');
tour[room.id].size = target;
room.addRaw('<b>' + user.name + '</b> has changed the tournament size to: ' + target + '. <b><i>' + (target - tour[room.id].players.length) + ' slot' + ( ( target - tour[room.id].players.length ) == 1 ? '' : 's') + ' remaining.</b></i>');
if (target == tour[room.id].players.length) tour.start(room.id);
},
tourtime: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined) return this.sendReply('There is no active tournament in this room.');
if (tour[room.id].status > 1) return this.sendReply('The tournament size cannot be changed now!');
if (!tour.timers[room.id]) return this.sendReply('This tournament is not running under a clock!');
if (!target) return this.sendReply('Proper syntax for this command: /tourtime time');
target = parseInt(target);
if (isNaN(target)) return this.sendReply('Proper syntax for this command: /tourtime time');
if (target < 0) return this.sendReply('Why would you want to reschedule a tournament for the past?');
target = Math.ceil(target);
tour.timers[room.id].time = target;
tour.timers[room.id].startTime = tour.currentSeconds;
room.addRaw('<b>' + user.name + '</b> has changed the remaining time for registering to the tournament to: ' + target + ' minute' + (target === 1 ? '' : 's') + '.');
if (target === 0) tour.start(room.id);
},
jt: 'j',
jointour: 'j',
j: function(target, room, user, connection) {
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined || tour[room.id].status == 0) return this.sendReply('There is no active tournament to join.');
if (tour[room.id].status == 2) return this.sendReply('Signups for the current tournament are over.');
if (tour.join(user.userid, room.id)) {
var remslots = tour[room.id].size - tour[room.id].players.length;
// these three assignments (natural, natural, boolean) are done as wished
var pplogmarg = Math.ceil(Math.sqrt(tour[room.id].size) / 2);
var logperiod = Math.ceil(Math.sqrt(tour[room.id].size));
var perplayerlog = ( ( tour[room.id].players.length <= pplogmarg ) || ( remslots + 1 <= pplogmarg ) );
//
if (perplayerlog) {
room.addRaw('<b>' + user.name + '</b> has joined the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(user.userid);
} else if ( (tour[room.id].players.length - tour[room.id].playerslogged.length == logperiod) || ( remslots <= pplogmarg ) ) {
if (tour[room.id].players.length == tour[room.id].playerslogged.length + 1) {
room.addRaw('<b>' + user.name + '</b> has joined the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(user.userid);
} else {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
var prelistnames = '<b>' + tour.username(someid) + '</b>';
for (var i = tour[room.id].playerslogged.length + 1; i < tour[room.id].players.length - 1; i++) {
someid = tour[room.id].players[i];
prelistnames = prelistnames + ', <b>' + tour.username(someid) + '</b>';
}
someid = tour[room.id].players[tour[room.id].players.length - 1];
var listnames = prelistnames + ' and <b>' + tour.username(someid) + '</b>';
room.addRaw(listnames + ' have joined the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].playerslogged.length]);
for (var i = tour[room.id].playerslogged.length; i < tour[room.id].players.length - 1; i++) { //the length is disturbed by the push above
tour[room.id].playerslogged.push(tour[room.id].players[i]);
}
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].players.length - 1]);
}
} else {
this.sendReply('You have succesfully joined the tournament. ' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.');
}
if (tour[room.id].size == tour[room.id].players.length) tour.start(room.id);
} else {
return this.sendReply('You could not enter the tournament. You may already be in the tournament. Type /l if you want to leave the tournament.');
}
},
forcejoin: 'fj',
fj: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined || tour[room.id].status == 0 || tour[room.id].status == 2) return this.sendReply('There is no tournament in a sign-up phase.');
if (!target) return this.sendReply('Please specify a user who you\'d like to participate.');
var targetUser = Users.get(target);
if (targetUser) {
target = targetUser.userid;
}
else {
return this.sendReply('The user \'' + target + '\' doesn\'t exist.');
}
if (tour.join(target, room.id)) {
var remslots = tour[room.id].size - tour[room.id].players.length;
if (tour[room.id].players.length == tour[room.id].playerslogged.length + 1) {
room.addRaw(user.name + ' has forced <b>' + tour.username(target) + '</b> to join the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(target);
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length + 2) {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
room.addRaw('<b>' + tour.username(someid) + '</b> has joined the tournament. <b><i>' + (remslots + 1) + ' slot' + ( remslots === 0 ? '' : 's') + ' remaining.</b></i>');
room.addRaw(user.name + ' has forced <b>' + tour.username(target) + '</b> to join the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(someid);
tour[room.id].playerslogged.push(target);
} else {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
var prelistnames = '<b>' + tour.username(someid) + '</b>';
for (var i = tour[room.id].playerslogged.length + 1; i < tour[room.id].players.length - 2; i++) {
someid = tour[room.id].players[i];
prelistnames = prelistnames + ', <b>' + tour.username(someid) + '</b>';
}
someid = tour[room.id].players[tour[room.id].players.length - 2];
var listnames = prelistnames + ' and <b>' + tour.username(someid) + '</b>';
room.addRaw(listnames + ' have joined the tournament. <b><i>' + (remslots + 1) + ' slot' + ( remslots === 0 ? '' : 's') + ' remaining.</b></i>');
room.addRaw(user.name + ' has forced <b>' + tour.username(target) + '</b> to join the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].playerslogged.length]);
for (var i = tour[room.id].playerslogged.length; i < tour[room.id].players.length - 1; i++) {
tour[room.id].playerslogged.push(tour[room.id].players[i]);
}
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].players.length - 1]);
}
if (tour[room.id].size == tour[room.id].players.length) tour.start(room.id);
}
else {
return this.sendReply('The user that you specified is already in the tournament.');
}
},
lt: 'l',
leavetour: 'l',
l: function(target, room, user, connection) {
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined || tour[room.id].status == 0) return this.sendReply('There is no active tournament to leave.');
var spotRemover = false;
if (tour[room.id].status == 1) {
if (tour.leave(user.userid, room.id)) {
var remslots = tour[room.id].size - tour[room.id].players.length;
if (tour[room.id].playerslogged.indexOf(user.userid) == -1) {
return this.sendReply('You have left the tournament. ' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.');
} else {
if (tour[room.id].players.length + 1 == tour[room.id].playerslogged.length) {
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length) {
tour[room.id].playerslogged.push(user.userid);
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length + 1) {
room.addRaw('<b>' + tour[room.id].players[tour[room.id].playerslogged.length] + '</b> has joined the tournament. <b><i>' + ( remslots - 1 ) + ' slot' + ( remslots == 2 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(user.userid);
} else {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
var prelistnames = '<b>' + tour.username(someid) + '</b>';
for (var i = tour[room.id].playerslogged.length + 1; i < tour[room.id].players.length - 1; i++) {
someid = tour[room.id].players[i];
prelistnames = prelistnames + ', <b>' + tour.username(someid) + '</b>';
}
someid = tour[room.id].players[tour[room.id].players.length - 1];
var listnames = prelistnames + ' and <b>' + tour.username(someid) + '</b>';
room.addRaw(listnames + ' have joined the tournament. <b><i>' + (remslots - 1) + ' slot' + ( remslots == 2 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].playerslogged.length]);
for (var i = tour[room.id].playerslogged.length; i < tour[room.id].players.length - 1; i++) { //the length is disturbed by the push above
tour[room.id].playerslogged.push(tour[room.id].players[i]);
}
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].players.length - 1]);
}
tour[room.id].playerslogged.splice(tour[room.id].playerslogged.indexOf(user.userid), 1);
room.addRaw('<b>' + user.name + '</b> has left the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
}
}
else {
return this.sendReply("You're not in the tournament.");
}
}
else {
var dqopp = tour.lose(user.userid, room.id);
if (dqopp && dqopp != -1 && dqopp != 1) {
room.addRaw('<b>' + user.userid + '</b> has left the tournament. <b>' + dqopp + '</b> will advance.');
var r = tour[room.id].round;
var c = 0;
for (var i in r) {
if (r[i][2] && r[i][2] != -1) c++;
}
if (r.length == c) tour.nextRound(room.id);
}
else {
if (dqopp == 1) return this.sendReply("You've already done your match. Wait till next round to leave.");
if (dqopp == 0 || dqopp == -1) return this.sendReply("You're not in the tournament or your opponent is unavailable.");
}
}
},
forceleave: 'fl',
fl: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined || tour[room.id].status == 0 || tour[room.id].status == 2) return this.sendReply('There is no tournament in a sign-up phase. Use /dq username if you wish to remove someone in an active tournament.');
if (!target) return this.sendReply('Please specify a user to kick from this signup.');
var targetUser = Users.get(target);
if (targetUser) {
target = targetUser.userid;
}
else {
return this.sendReply('The user \'' + target + '\' doesn\'t exist.');
}
if (tour.leave(target, room.id)) {
var remslots = tour[room.id].size - tour[room.id].players.length;
if (tour[room.id].playerslogged.indexOf(target) == -1) {
room.addRaw('<b>' + tour.username(target) + '</b> joined the tournament but was forced to leave by ' + user.name + '. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
}
else {
if (tour[room.id].players.length + 1 == tour[room.id].playerslogged.length) {
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length) {
room.addRaw('<b>' + tour.username(target) + '</b> has joined the tournament. <b><i>' + ( remslots - 1) + ' slot' + ( remslots == 2 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(target);
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length + 1) {
room.addRaw('<b>' + tour[room.id].players[tour[room.id].playerslogged.length] + '</b> has joined the tournament. <b><i>' + ( remslots - 1 ) + ' slot' + ( remslots == 2 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(target);
} else {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
var prelistnames = '<b>' + tour.username(someid) + '</b>';
for (var i = tour[room.id].playerslogged.length + 1; i < tour[room.id].players.length - 1; i++) {
someid = tour[room.id].players[i];
prelistnames = prelistnames + ', <b>' + tour.username(someid) + '</b>';
}
someid = tour[room.id].players[tour[room.id].players.length - 1];
var listnames = prelistnames + ' and <b>' + tour.username(someid) + '</b>';
room.addRaw(listnames + ' have joined the tournament. <b><i>' + (remslots - 1) + ' slot' + ( remslots == 2 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].playerslogged.length]);
for (var i = tour[room.id].playerslogged.length; i < tour[room.id].players.length - 1; i++) { //the length is disturbed by the push above
tour[room.id].playerslogged.push(tour[room.id].players[i]);
}
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].players.length - 1]);
}
tour[room.id].playerslogged.splice(tour[room.id].playerslogged.indexOf(target), 1);
room.addRaw(user.name + ' has forced <b>' + tour.username(target) + '</b> to leave the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
}
}
else {
return this.sendReply('The user that you specified is not in the tournament.');
}
},
remind: function(target, room, user, connection) {
if (!tour.userauth(user,room)) return this.sendReply('You do not have enough authority to use this command.');
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined) return this.sendReply('There is no active tournament in this room.');
if (tour[room.id].status != 1) return this.sendReply('There is no tournament in its sign up phase.');
var remslots = tour[room.id].size - tour[room.id].players.length;
if (tour[room.id].players.length == tour[room.id].playerslogged.length) {
} else if (tour[room.id].players.length == tour[room.id].playerslogged.length + 1) {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
room.addRaw('<b>' + tour.username(someid) + '</b> has joined the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(someid);
} else {
var someid = tour[room.id].players[tour[room.id].playerslogged.length];
var prelistnames = '<b>' + tour.username(someid) + '</b>';
for (var i = tour[room.id].playerslogged.length + 1; i < tour[room.id].players.length - 1; i++) {
someid = tour[room.id].players[i];
prelistnames = prelistnames + ', <b>' + tour.username(someid) + '</b>';
}
someid = tour[room.id].players[tour[room.id].players.length - 1];
var listnames = prelistnames + ' and <b>' + tour.username(someid) + '</b>';
room.addRaw(listnames + ' have joined the tournament. <b><i>' + remslots + ' slot' + ( remslots == 1 ? '' : 's') + ' remaining.</b></i>');
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].playerslogged.length]);
for (var i = tour[room.id].playerslogged.length; i < tour[room.id].players.length - 1; i++) { //the length is disturbed by the push above
tour[room.id].playerslogged.push(tour[room.id].players[i]);
}
tour[room.id].playerslogged.push(tour[room.id].players[tour[room.id].players.length - 1]);
}
room.addRaw('<hr /><h2><font color="green">Please sign up for the ' + Tools.data.Formats[tour[room.id].tier].name + ' Tournament.</font> <font color="red">/j</font> <font color="green">to join!</font></h2><b><font color="blueviolet">PLAYERS:</font></b> ' + tour[room.id].size + '<br /><font color="blue"><b>TIER:</b></font> ' + Tools.data.Formats[tour[room.id].tier].name + '<hr />');
},
viewround: function(target, room, user, connection) {
if (!this.canBroadcast()) return;
if (room.decision) return this.sendReply('Prof. Oak: There is a time and place for everything! You cannot do this in battle rooms.');
if (tour[room.id] == undefined) return this.sendReply('There is no active tournament in this room.');
if (tour[room.id].status < 2) return this.sendReply('There is no tournament out of its signup phase.');
var html = '<hr /><h3><font color="green">Round '+ tour[room.id].roundNum + '!</font></h3><font color="blue"><b>TIER:</b></font> ' + Tools.data.Formats[tour[room.id].tier].name + "<hr /><center><small><font color=red>Red</font> = lost, <font color=green>Green</font> = won, <a class='ilink'><b>URL</b></a> = battling</small><center>";
var r = tour[room.id].round;
var firstMatch = false;
for (var i in r) {
if (!r[i][1]) {
//bye
var byer = tour.username(r[i][0]);
html += "<font color=\"red\">" + clean(byer) + " has received a bye.</font><br />";
}
else {
if (r[i][2] == undefined) {
//haven't started
var p1n = tour.username(r[i][0]);
var p2n = tour.username(r[i][1]);
if (p1n.substr(0, 6) === 'Guest ') p1n = r[i][0];
if (p2n.substr(0, 6) === 'Guest ') p2n = r[i][1];
var tabla = "";if (!firstMatch) {var tabla = "</center><table align=center cellpadding=0 cellspacing=0>";firstMatch = true;}
html += tabla + "<tr><td align=right>" + clean(p1n) + "</td><td> VS </td><td>" + clean(p2n) + "</td></tr>";
}
else if (r[i][2] == -1) {
//currently battling
var p1n = tour.username(r[i][0]);
var p2n = tour.username(r[i][1]);
if (p1n.substr(0, 6) === 'Guest ') p1n = r[i][0];
if (p2n.substr(0, 6) === 'Guest ') p2n = r[i][1];
var tabla = "";if (!firstMatch) {var tabla = "</center><table align=center cellpadding=0 cellspacing=0>";firstMatch = true;}
var tourbattle = tour[room.id].battles[i];
function link(txt) {return "<a href='/" + tourbattle + "' room='" + tourbattle + "' class='ilink'>" + txt + "</a>";}
html += tabla + "<tr><td align=right><b>" + link(clean(p1n)) + "</b></td><td><b> " + link("VS") + " </b></td><td><b>" + link(clean(p2n)) + "</b></td></tr>";
}
else {
//match completed
var p1 = "red";
var p2 = "green";
if (r[i][2] == r[i][0]) {
p1 = "green";
p2 = "red";
}
var p1n = tour.username(r[i][0]);
var p2n = tour.username(r[i][1]);
if (p1n.substr(0, 6) === 'Guest ') p1n = r[i][0];
if (p2n.substr(0, 6) === 'Guest ') p2n = r[i][1];