forked from smogon/pokemon-showdown-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-teambuilder.js
3413 lines (3176 loc) · 125 KB
/
client-teambuilder.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
(function (exports, $) {
// this is a useful global
var teams;
var TeambuilderRoom = exports.TeambuilderRoom = exports.Room.extend({
type: 'teambuilder',
title: 'Teambuilder',
initialize: function () {
teams = Storage.teams;
// left menu
this.$el.addClass('ps-room-light').addClass('scrollable');
if (!Storage.whenTeamsLoaded.isLoaded) {
Storage.whenTeamsLoaded(this.update, this);
}
this.update();
},
focus: function () {
if (this.curTeam) {
this.curTeam.iconCache = '!';
this.curTeam.gen = this.getGen(this.curTeam.format);
Storage.activeSetList = this.curSetList;
}
},
blur: function () {
if (this.saveFlag) {
this.saveFlag = false;
app.user.trigger('saveteams');
}
},
events: {
// team changes
'change input.teamnameedit': 'teamNameChange',
'click button.formatselect': 'selectFormat',
'change input[name=nickname]': 'nicknameChange',
// details
'change .detailsform input': 'detailsChange',
'click .changeform' : 'altForm',
'click .altform' : 'altForm',
// stats
'keyup .statform input.numform': 'statChange',
'input .statform input[type=number].numform': 'statChange',
'change select[name=nature]': 'natureChange',
'change select[name=ivspread]': 'ivSpreadChange',
// teambuilder events
'click .utilichart a': 'chartClick',
'keydown .chartinput': 'chartKeydown',
'keyup .chartinput': 'chartKeyup',
'focus .chartinput': 'chartFocus',
'blur .chartinput': 'chartChange',
// drag/drop
'click .team': 'edit',
'click .selectFolder': 'selectFolder',
'mouseover .team': 'mouseOverTeam',
'mouseout .team': 'mouseOutTeam',
'dragstart .team': 'dragStartTeam',
'dragend .team': 'dragEndTeam',
'dragenter .team': 'dragEnterTeam',
'dragenter .folder .selectFolder': 'dragEnterFolder',
'dragleave .folder .selectFolder': 'dragLeaveFolder',
'dragexit .folder .selectFolder': 'dragExitFolder',
// clipboard
'click .teambuilder-clipboard-data .result': 'clipboardResultSelect',
'click .teambuilder-clipboard-data': 'clipboardExpand',
'blur .teambuilder-clipboard-data': 'clipboardShrink'
},
dispatchClick: function (e) {
e.preventDefault();
e.stopPropagation();
if (this[e.currentTarget.value]) this[e.currentTarget.value](e);
},
back: function () {
if (this.exportMode) {
if (this.curTeam) {
this.curTeam.team = Storage.packTeam(this.curSetList);
Storage.saveTeam(this.curTeam);
}
this.exportMode = false;
} else if (this.curSet) {
app.clearGlobalListeners();
this.curSet = null;
Storage.saveTeam(this.curTeam);
} else if (this.curTeam) {
this.curTeam.team = Storage.packTeam(this.curSetList);
this.curTeam.iconCache = '';
var team = this.curTeam;
this.curTeam = null;
Storage.activeSetList = this.curSetList = null;
Storage.saveTeam(team);
} else {
return;
}
app.user.trigger('saveteams');
this.update();
},
// the teambuilder has three views:
// - team list (curTeam falsy)
// - team view (curTeam exists, curSet falsy)
// - set view (curTeam exists, curSet exists)
curTeam: null,
curTeamLoc: 0,
curSet: null,
curSetLoc: 0,
// curFolder will have '/' at the end if it's a folder, but
// it will be alphanumeric (so guaranteed no '/') if it's a
// format
// Special values:
// '' - show all
// 'gen7' - show teams with no format
// '/' - show teams with no folder
curFolder: '',
curFolderKeep: '',
exportMode: false,
update: function () {
teams = Storage.teams;
if (this.curTeam) {
this.ignoreEVLimits = (this.curTeam.gen < 3);
if (this.curSet) {
return this.updateSetView();
}
return this.updateTeamView();
}
return this.updateTeamInterface();
},
/*********************************************************
* Team list view
*********************************************************/
deletedTeam: null,
deletedTeamLoc: -1,
updateTeamInterface: function () {
this.deletedSet = null;
this.deletedSetLoc = -1;
var buf = '';
if (this.exportMode) {
if (this.curFolder) {
buf = '<div class="pad"><button name="back"><i class="fa fa-chevron-left"></i> List</button></div>';
buf += '<div class="teamedit"><textarea readonly class="textbox" rows="17">' + Tools.escapeHTML(Storage.exportFolder(this.curFolder)) + '</textarea></div>';
} else {
buf = '<div class="pad"><button name="back"><i class="fa fa-chevron-left"></i> List</button> <button name="saveBackup" class="savebutton"><i class="fa fa-floppy-o"></i> Save</button></div>';
buf += '<div class="teamedit"><textarea class="textbox" rows="17">' + Tools.escapeHTML(Storage.exportAllTeams()) + '</textarea></div>';
}
this.$el.html(buf);
this.$('.teamedit textarea').focus().select();
return;
}
if (!Storage.whenTeamsLoaded.isLoaded) {
if (!Storage.whenTeamsLoaded.isStalled) {
buf = '<div class="pad"><p>lol zarel this is a horrible teambuilder</p>';
buf += '<p>that\'s because we\'re not done loading it...</p></div>';
} else {
buf = '<div class="pad"><p>We\'re having some trouble loading teams securely.</p>';
buf += '<p>This is sometimes caused by antiviruses like Avast and BitDefender.</p>';
buf += '<p><strong>If you\'re using Firefox and an antivirus:</strong> Your antivirus is trying to scan your teams, and a recent Firefox update doesn\'t let it. Turn off HTTPS scanning in your antivirus or uninstall your antivirus, and your teams will come back.</p>';
buf += '<p>You can use the teambuilder insecurely, but any teams you\'ve saved securely won\'t be there.</p>';
buf += '<p><button class="button" name="insecureUse">Use teambuilder insecurely</button></p></div>';
}
this.$el.html(buf);
return;
}
// folderpane
buf = '<div class="folderpane">';
buf += '</div>';
// teampane
buf += '<div class="teampane">';
buf += '</div>';
this.$el.html(buf);
this.updateFolderList();
this.updateTeamList();
},
insecureUse: function () {
Storage.whenTeamsLoaded.load();
this.updateTeamInterface();
},
updateFolderList: function () {
var buf = '<div class="folderlist"><div class="folderlistbefore"></div>';
buf += '<div class="folder' + (!this.curFolder ? ' cur"><div class="folderhack3"><div class="folderhack1"></div><div class="folderhack2"></div>' : '">') + '<div class="selectFolder" data-value="all"><em>(all)</em></div></div>' + (!this.curFolder ? '</div>' : '');
var folderTable = {};
var folders = [];
if (Storage.teams) for (var i = -2; i < Storage.teams.length; i++) {
if (i >= 0) {
var folder = Storage.teams[i].folder;
if (folder && !((folder + '/') in folderTable)) {
folders.push('Z' + folder);
folderTable[folder + '/'] = 1;
if (!('/' in folderTable)) {
folders.push('Z~');
folderTable['/'] = 1;
}
}
}
var format;
if (i === -2) {
format = this.curFolderKeep;
} else if (i === -1) {
format = this.curFolder;
} else {
format = Storage.teams[i].format;
if (!format) format = 'gen7';
}
if (!format) continue;
if (format in folderTable) continue;
folderTable[format] = 1;
if (format.slice(-1) === '/') {
folders.push('Z' + (format.slice(0, -1) || '~'));
if (!('/' in folderTable)) {
folders.push('Z~');
folderTable['/'] = 1;
}
continue;
}
if (format === 'gen7') {
folders.push('A~');
continue;
}
switch (format.slice(0, 4)) {
case 'gen1': format = 'G' + format.slice(4); break;
case 'gen2': format = 'F' + format.slice(4); break;
case 'gen3': format = 'E' + format.slice(4); break;
case 'gen4': format = 'D' + format.slice(4); break;
case 'gen5': format = 'C' + format.slice(4); break;
case 'gen6': format = 'B' + format.slice(4); break;
case 'gen7': format = 'A' + format.slice(4); break;
default: format = 'X' + format; break;
}
folders.push(format);
}
folders.sort();
var gen = '';
var formatFolderBuf = '<div class="foldersep"></div>';
formatFolderBuf += '<div class="folder"><div class="selectFolder" data-value="+"><i class="fa fa-plus"></i><em>(add format folder)</em></div></div>';
for (var i = 0; i < folders.length; i++) {
var format = folders[i];
var newGen;
switch (format.charAt(0)) {
case 'G': newGen = '1'; break;
case 'F': newGen = '2'; break;
case 'E': newGen = '3'; break;
case 'D': newGen = '4'; break;
case 'C': newGen = '5'; break;
case 'B': newGen = '6'; break;
case 'A': newGen = '7'; break;
case 'X': newGen = 'X'; break;
case 'Z': newGen = '/'; break;
}
if (gen !== newGen) {
gen = newGen;
if (gen === '/') {
buf += formatFolderBuf;
formatFolderBuf = '';
buf += '<div class="foldersep"></div>';
buf += '<div class="folder"><h3>Folders</h3></div>';
} else if (gen === 'X') {
buf += '<div class="folder"><h3>???</h3></div>';
} else {
buf += '<div class="folder"><h3>Gen ' + gen + '</h3></div>';
}
}
if (gen === '/') {
formatName = format.slice(1);
format = formatName + '/';
if (formatName === '~') {
formatName = '(uncategorized)';
format = '/';
} else {
formatName = Tools.escapeHTML(formatName);
}
buf += '<div class="folder' + (this.curFolder === format ? ' cur"><div class="folderhack3"><div class="folderhack1"></div><div class="folderhack2"></div>' : '">') + '<div class="selectFolder" data-value="' + format + '"><i class="fa ' + (this.curFolder === format ? 'fa-folder-open' : 'fa-folder') + (format === '/' ? '-o' : '') + '"></i>' + formatName + '</div></div>' + (this.curFolder === format ? '</div>' : '');
continue;
}
var formatName = format.slice(1);
if (formatName === '~') formatName = '';
format = 'gen' + newGen + formatName;
if (format.length === 4) formatName = '(uncategorized)';
// folders are <div>s rather than <button>s because in theory it has
// less weird interactions with HTML5 drag-and-drop
buf += '<div class="folder' + (this.curFolder === format ? ' cur"><div class="folderhack3"><div class="folderhack1"></div><div class="folderhack2"></div>' : '">') + '<div class="selectFolder" data-value="' + format + '"><i class="fa ' + (this.curFolder === format ? 'fa-folder-open-o' : 'fa-folder-o') + '"></i>' + formatName + '</div></div>' + (this.curFolder === format ? '</div>' : '');
}
buf += formatFolderBuf;
buf += '<div class="foldersep"></div>';
buf += '<div class="folder"><div class="selectFolder" data-value="++"><i class="fa fa-plus"></i><em>(add folder)</em></div></div>';
buf += '<div class="folderlistafter"></div></div>';
this.$('.folderpane').html(buf);
},
updateTeamList: function (resetScroll) {
var teams = Storage.teams;
var buf = '';
// teampane
buf += this.clipboardHTML();
var filterFormat = '';
// filterFolder === undefined: show teams in any folder
// filterFolder === '': show only teams that don't have a folder
var filterFolder;
if (!this.curFolder) {
buf += '<h2>Hi</h2>';
buf += '<p>Did you have a good day?</p>';
buf += '<p><button class="button" name="greeting" value="Y"><i class="fa fa-smile-o"></i> Yes, my day was pretty good</button> <button class="button" name="greeting" value="N"><i class="fa fa-frown-o"></i> No, it wasn\'t great</button></p>';
buf += '<h2>All teams</h2>';
} else {
if (this.curFolder.slice(-1) === '/') {
filterFolder = this.curFolder.slice(0, -1);
if (filterFolder) {
buf += '<h2><i class="fa fa-folder-open"></i> ' + filterFolder + ' <button class="button small" style="margin-left:5px" name="renameFolder"><i class="fa fa-pencil"></i> Rename</button> <button class="button small" style="margin-left:5px" name="promptDeleteFolder"><i class="fa fa-times"></i> Remove</button></h2>';
} else {
buf += '<h2><i class="fa fa-folder-open-o"></i> Teams not in any folders</h2>';
}
} else {
filterFormat = this.curFolder;
buf += '<h2><i class="fa fa-folder-open-o"></i> ' + filterFormat + '</h2>';
}
}
var newButtonText = "New Team";
if (filterFolder) newButtonText = "New Team in folder";
if (filterFormat && filterFormat !== 'gen7') {
newButtonText = "New " + Tools.escapeFormat(filterFormat) + " Team";
}
buf += '<p><button name="newTop" class="button big"><i class="fa fa-plus-circle"></i> ' + newButtonText + '</button></p>';
buf += '<ul class="teamlist">';
var atLeastOne = false;
try {
if (!window.localStorage && !window.nodewebkit) buf += '<li>== CAN\'T SAVE ==<br /><small>Your browser doesn\'t support <code>localStorage</code> and can\'t save teams! Update to a newer browser.</small></li>';
} catch (e) {
buf += '<li>== CAN\'T SAVE ==<br /><small><code>Cookies</code> are disabled so you can\'t save teams! Enable them in your browser settings.</small></li>';
}
if (Storage.cantSave) buf += '<li>== CAN\'T SAVE ==<br /><small>You hit your browser\'s limit for team storage! Please backup them and delete some of them. Your teams won\'t be saved until you\'re under the limit again.</small></li>';
if (!teams.length) {
if (this.deletedTeamLoc >= 0) {
buf += '<li><button name="undoDelete"><i class="fa fa-undo"></i> Undo Delete</button></li>';
}
buf += '<li><p><em>you don\'t have any teams lol</em></p></li>';
} else {
for (var i = 0; i < teams.length + 1; i++) {
if (i === this.deletedTeamLoc) {
if (!atLeastOne) atLeastOne = true;
buf += '<li><button name="undoDelete"><i class="fa fa-undo"></i> Undo Delete</button></li>';
}
if (i >= teams.length) break;
var team = teams[i];
if (team && !team.team && team.team !== '') {
team = null;
}
if (!team) {
buf += '<li>Error: A corrupted team was dropped</li>';
teams.splice(i, 1);
i--;
if (this.deletedTeamLoc && this.deletedTeamLoc > i) this.deletedTeamLoc--;
continue;
}
if (filterFormat && filterFormat !== (team.format || 'gen7')) continue;
if (filterFolder !== undefined && filterFolder !== team.folder) continue;
if (!atLeastOne) atLeastOne = true;
var formatText = '';
if (team.format) {
formatText = '[' + team.format + '] ';
}
if (team.folder) formatText += team.folder + '/';
// teams are <div>s rather than <button>s because Firefox doesn't
// support dragging and dropping buttons.
buf += '<li><div name="edit" data-value="' + i + '" class="team" draggable="true">' + formatText + '<strong>' + Tools.escapeHTML(team.name) + '</strong><br /><small>';
buf += Storage.getTeamIcons(team);
buf += '</small></div><button name="edit" value="' + i + '"><i class="fa fa-pencil"></i>Edit</button><button name="delete" value="' + i + '"><i class="fa fa-trash"></i>Delete</button></li>';
}
if (!atLeastOne) {
if (filterFolder) {
buf += '<li><p><em>you don\'t have any teams in this folder lol</em></p></li>';
} else {
buf += '<li><p><em>you don\'t have any ' + this.curFolder + ' teams lol</em></p></li>';
}
}
}
buf += '</ul>';
if (atLeastOne) {
buf += '<p><button name="new" class="button"><i class="fa fa-plus-circle"></i> ' + newButtonText + '</button></p>';
}
if (window.nodewebkit) {
buf += '<button name="revealFolder" class="button"><i class="fa fa-folder-open"></i> Reveal teams folder</button> <button name="reloadTeamsFolder" class="button"><i class="fa fa-refresh"></i> Reload teams files</button> <button name="backup" class="button"><i class="fa fa-upload"></i> Backup/Restore all teams</button>';
} else if (this.curFolder) {
buf += '<button name="backup" class="button"><i class="fa fa-upload"></i> Backup all teams from this folder</button>';
} else if (atLeastOne) {
buf += '<p><strong>Clearing your cookies (specifically, <code>localStorage</code>) will delete your teams.</strong></p>';
buf += '<button name="backup" class="button"><i class="fa fa-upload"></i> Backup/Restore all teams</button>';
buf += '<p>If you want to clear your cookies or <code>localStorage</code>, you can use the Backup/Restore feature to save your teams as text first.</p>';
} else {
buf += '<button name="backup" class="button"><i class="fa fa-upload"></i> Restore teams from backup</button>';
}
var $pane = this.$('.teampane');
$pane.html(buf);
if (resetScroll) {
$pane.scrollTop(0);
} else if (this.teamScrollPos) {
$pane.scrollTop(this.teamScrollPos);
this.teamScrollPos = 0;
}
},
greeting: function (answer, button) {
var buf = '<p><strong>' + $(button).html() + '</p></strong>';
if (answer === 'N') {
buf += '<p>Aww, that\'s too bad. :( I hope playing on Pokémon Showdown today can help cheer you up!</p>';
} else if (answer === 'Y') {
buf += '<p>Cool! I just added some pretty cool teambuilder features, so I\'m pretty happy, too. Did you know you can drag and drop teams to different format-folders? You can also drag and drop them to and from your computer (works best in Chrome).</p>';
buf += '<p><button class="button" name="greeting" value="W"><i class="fa fa-question-circle"></i> Wait, who are you? Talking to a teambuilder is weird.</button></p>';
} else if (answer === 'W') {
buf += '<p>Oh, I\'m Zarel! I made a Credits button for this...</p>';
buf += '<div class="menugroup"><p><button class="button mainmenu4" name="credits"><i class="fa fa-info-circle"></i> Credits</button></p></div>';
buf += '<p>Isn\'t it pretty? Matches your background and everything. It used to be in the Main Menu but we had to get rid of it to save space.</p>';
buf += '<p>Speaking of, you should try <button class="button" name="background"><i class="fa fa-picture-o"></i> changing your background</button>.';
buf += '<p><button class="button" name="greeting" value="B"><i class="fa fa-hand-pointer-o"></i> You might be having too much fun with these buttons and icons</button></p>';
} else if (answer === 'B') {
buf += '<p>I paid good money for those icons! I need to get my money\'s worth!</p>';
buf += '<p><button class="button" name="greeting" value="WR"><i class="fa fa-exclamation-triangle"></i> Wait, really?</button></p>';
} else if (answer === 'WR') {
buf += '<p>No, they were free. That just makes it easier to get my money\'s worth. Let\'s play rock paper scissors!</p>';
buf += '<p><button class="button" name="greeting" value="RR"><i class="fa fa-hand-rock-o"></i> Rock</button> <button class="button" name="greeting" value="RP"><i class="fa fa-hand-paper-o"></i> Paper</button> <button class="button" name="greeting" value="RS"><i class="fa fa-hand-scissors-o"></i> Scissors</button> <button class="button" name="greeting" value="RL"><i class="fa fa-hand-lizard-o"></i> Lizard</button> <button class="button" name="greeting" value="RK"><i class="fa fa-hand-spock-o"></i> Spock</button></p>';
} else if (answer[0] === 'R') {
buf += '<p>I play laser, I win. <i class="fa fa-hand-o-left"></i></p>';
buf += '<p><button class="button" name="greeting" value="YC"><i class="fa fa-thumbs-o-down"></i> You can\'t do that!</button></p>';
} else if (answer === 'SP') {
buf += '<p>Okay, sure. I warn you, I\'m using the same RNG that makes Stone Edge miss for you.</p>';
buf += '<p><button class="button" name="greeting" value="SP3"><i class="fa fa-caret-square-o-right"></i> I want to play Rock Paper Scissors</button> <button class="button" name="greeting" value="SP5"><i class="fa fa-caret-square-o-right"></i> I want to play Rock Paper Scissors Lizard Spock</button></p>';
} else if (answer === 'SP3') {
buf += '<p><button class="button" name="greeting" value="PR3"><i class="fa fa-hand-rock-o"></i> Rock</button> <button class="button" name="greeting" value="PP3"><i class="fa fa-hand-paper-o"></i> Paper</button> <button class="button" name="greeting" value="PS3"><i class="fa fa-hand-scissors-o"></i> Scissors</button></p>';
} else if (answer === 'SP5') {
buf += '<p><button class="button" name="greeting" value="PR5"><i class="fa fa-hand-rock-o"></i> Rock</button> <button class="button" name="greeting" value="PP5"><i class="fa fa-hand-paper-o"></i> Paper</button> <button class="button" name="greeting" value="PS5"><i class="fa fa-hand-scissors-o"></i> Scissors</button> <button class="button" name="greeting" value="PL5"><i class="fa fa-hand-lizard-o"></i> Lizard</button> <button class="button" name="greeting" value="PK5"><i class="fa fa-hand-spock-o"></i> Spock</button></p>';
} else if (answer[0] === 'P') {
var rpsChart = {
R: 'rock',
P: 'paper',
S: 'scissors',
L: 'lizard',
K: 'spock'
};
var rpsWinChart = {
SP: 'cuts',
SL: 'decapitates',
PR: 'covers',
PK: 'disproves',
RL: 'crushes',
RS: 'crushes',
LK: 'poisons',
LP: 'eats',
KS: 'smashes',
KR: 'vaporizes'
};
var my = ['R', 'P', 'S', 'L', 'K'][Math.floor(Math.random() * Number(answer[2]))];
var your = answer[1];
buf += '<p>I play <i class="fa fa-hand-' + rpsChart[my] + '-o"></i> ' + rpsChart[my] + '!</p>';
if ((my + your) in rpsWinChart) {
buf += '<p>And ' + rpsChart[my] + ' ' + rpsWinChart[my + your] + ' ' + rpsChart[your] + ', so I win!</p>';
} else if ((your + my) in rpsWinChart) {
buf += '<p>But ' + rpsChart[your] + ' ' + rpsWinChart[your + my] + ' ' + rpsChart[my] + ', so you win...</p>';
} else {
buf += '<p>We played the same thing, so it\'s a tie.</p>';
}
if (!this.rpsScores || !this.rpsScores.length) {
this.rpsScores = ['pi', '$3.50', '9.80665 m/s<sup>2</sup>', '28°C', '百万点', '<i class="fa fa-bitcoin"></i>0.0000174', '<s>priceless</s> <i class="fa fa-cc-mastercard"></i> MasterCard', '127.0.0.1', 'C−, see me after class'];
}
var score = this.rpsScores.splice(Math.floor(Math.random() * this.rpsScores.length), 1)[0];
buf += '<p>Score: ' + score + '</p>';
buf += '<p><button class="button" name="greeting" value="SP' + answer[2] + '"><i class="fa fa-caret-square-o-right"></i> I demand a rematch!</button></p>';
} else if (answer === 'YC') {
buf += '<p>Okay, then I play peace sign <i class="fa fa-hand-peace-o"></i>, everyone signs a peace treaty, ending the war and ushering in a new era of prosperity.</p>';
buf += '<p><button class="button" name="greeting" value="SP"><i class="fa fa-caret-square-o-right"></i> I wanted to play for real...</button></p>';
}
$(button).parent().replaceWith(buf);
},
credits: function () {
app.addPopup(CreditsPopup);
},
background: function () {
app.addPopup(CustomBackgroundPopup);
},
selectFolder: function (format) {
if (format && format.currentTarget) {
var e = format;
format = $(e.currentTarget).data('value');
e.preventDefault();
if (format === '+') {
e.stopImmediatePropagation();
var self = this;
app.addPopup(FormatPopup, {format: '', sourceEl: e.currentTarget, selectType: 'teambuilder', onselect: function (newFormat) {
self.selectFolder(newFormat);
}});
return;
}
if (format === '++') {
e.stopImmediatePropagation();
var self = this;
// app.addPopupPrompt("Folder name:", "Create folder", function (newFormat) {
// self.selectFolder(newFormat + '/');
// });
app.addPopup(PromptPopup, {message: "Folder name:", button: "Create folder", sourceEl: e.currentTarget, callback: function (name) {
name = $.trim(name);
if (name.indexOf('/') >= 0 || name.indexOf('\\') >= 0) {
app.addPopupMessage("Names can't contain slashes, since they're used as a folder separator.");
name = name.replace(/[\\\/]/g, '');
}
if (!name) return;
self.selectFolder(name + '/');
}});
return;
}
} else {
this.curFolderKeep = format;
}
this.curFolder = (format === 'all' ? '' : format);
this.updateFolderList();
this.updateTeamList(true);
},
renameFolder: function () {
if (!this.curFolder) return;
if (this.curFolder.slice(-1) !== '/') return;
var oldFolder = this.curFolder.slice(0, -1);
var self = this;
app.addPopup(PromptPopup, {message: "Folder name:", button: "Rename folder", value: oldFolder, callback: function (name) {
name = $.trim(name);
if (name.indexOf('/') >= 0 || name.indexOf('\\') >= 0) {
app.addPopupMessage("Names can't contain slashes, since they're used as a folder separator.");
name = name.replace(/[\\\/]/g, '');
}
if (!name) return;
if (name === oldFolder) return;
for (var i = 0; i < Storage.teams.length; i++) {
var team = Storage.teams[i];
if (team.folder !== oldFolder) continue;
team.folder = name;
if (window.nodewebkit) Storage.saveTeam(team);
}
if (!window.nodewebkit) Storage.saveTeams();
self.selectFolder(name + '/');
}});
},
promptDeleteFolder: function () {
app.addPopup(DeleteFolderPopup, {folder: this.curFolder, room: this});
},
deleteFolder: function (format, addName) {
if (format.slice(-1) !== '/') return;
var oldFolder = format.slice(0, -1);
if (this.curFolderKeep === oldFolder) {
this.curFolderKeep = '';
}
for (var i = 0; i < Storage.teams.length; i++) {
var team = Storage.teams[i];
if (team.folder !== oldFolder) continue;
team.folder = '';
if (addName) team.name = oldFolder + ' ' + team.name;
if (window.nodewebkit) Storage.saveTeam(team);
}
if (!window.nodewebkit) Storage.saveTeams();
this.selectFolder('/');
},
show: function () {
Room.prototype.show.apply(this, arguments);
var $teamwrapper = this.$('.teamwrapper');
var width = $(window).width();
if (!$teamwrapper.length) return;
if (width < 640) {
var scale = (width / 640);
$teamwrapper.css('transform', 'scale(' + scale + ')');
$teamwrapper.addClass('scaled');
} else {
$teamwrapper.css('transform', 'none');
$teamwrapper.removeClass('scaled');
}
},
// button actions
revealFolder: function () {
Storage.revealFolder();
},
reloadTeamsFolder: function () {
Storage.nwLoadTeams();
},
edit: function (i) {
this.teamScrollPos = this.$('.teampane').scrollTop();
if (i && i.currentTarget) {
i = $(i.currentTarget).data('value');
}
i = +i;
this.curTeam = teams[i];
this.curTeam.iconCache = '!';
this.curTeam.gen = this.getGen(this.curTeam.format);
Storage.activeSetList = this.curSetList = Storage.unpackTeam(this.curTeam.team);
this.curTeamIndex = i;
this.update();
},
"delete": function (i) {
i = +i;
this.deletedTeamLoc = i;
this.deletedTeam = teams.splice(i, 1)[0];
for (var room in app.rooms) {
var selection = app.rooms[room].$('button.teamselect').val();
if (!selection || selection === 'random') continue;
var obj = app.rooms[room].id === "" ? app.rooms[room] : app.rooms[room].tournamentBox;
if (i < obj.curTeamIndex) {
obj.curTeamIndex--;
} else if (i === obj.curTeamIndex) {
obj.curTeamIndex = -1;
}
}
Storage.deleteTeam(this.deletedTeam);
app.user.trigger('saveteams');
this.updateTeamList();
},
undoDelete: function () {
if (this.deletedTeamLoc >= 0) {
teams.splice(this.deletedTeamLoc, 0, this.deletedTeam);
for (var room in app.rooms) {
var selection = app.rooms[room].$('button.teamselect').val();
if (!selection || selection === 'random') continue;
var obj = app.rooms[room].id === "" ? app.rooms[room] : app.rooms[room].tournamentBox;
if (this.deletedTeamLoc < obj.curTeamIndex + 1) {
obj.curTeamIndex++;
} else if (obj.curTeamIndex === -1) {
obj.curTeamIndex = this.deletedTeamLoc;
}
}
var undeletedTeam = this.deletedTeam;
this.deletedTeam = null;
this.deletedTeamLoc = -1;
Storage.saveTeam(undeletedTeam);
app.user.trigger('saveteams');
this.update();
}
},
saveBackup: function () {
Storage.deleteAllTeams();
Storage.importTeam(this.$('.teamedit textarea').val(), true);
teams = Storage.teams;
Storage.saveAllTeams();
for (var room in app.rooms) {
var selection = app.rooms[room].$('button.teamselect').val();
if (!selection || selection === 'random') continue;
var obj = app.rooms[room].id === "" ? app.rooms[room] : app.rooms[room].tournamentBox;
obj.curTeamIndex = 0;
}
this.back();
},
"new": function () {
var format = this.curFolder;
var folder = '';
if (format && format.charAt(format.length - 1) === '/') {
folder = format.slice(0, -1);
format = '';
}
var newTeam = {
name: 'Untitled ' + (teams.length + 1),
format: format,
team: '',
folder: folder,
iconCache: ''
};
teams.push(newTeam);
this.edit(teams.length - 1);
},
newTop: function () {
var format = this.curFolder;
var folder = '';
if (format && format.charAt(format.length - 1) === '/') {
folder = format.slice(0, -1);
format = '';
}
var newTeam = {
name: 'Untitled ' + (teams.length + 1),
format: format,
team: '',
folder: folder,
iconCache: ''
};
teams.unshift(newTeam);
for (var room in app.rooms) {
var selection = app.rooms[room].$('button.teamselect').val();
if (!selection || selection === 'random') continue;
var obj = app.rooms[room].id === "" ? app.rooms[room] : app.rooms[room].tournamentBox;
obj.curTeamIndex++;
}
this.edit(0);
},
"import": function () {
if (this.exportMode) return this.back();
this.exportMode = true;
if (!this.curTeam) {
this['new']();
} else {
this.update();
}
},
backup: function () {
this.curTeam = null;
this.curSetList = null;
this.exportMode = true;
this.update();
},
// drag and drop
// because of a bug in Chrome and Webkit:
// https://code.google.com/p/chromium/issues/detail?id=410328
// we can't use CSS :hover
mouseOverTeam: function (e) {
e.currentTarget.className = 'team team-hover';
},
mouseOutTeam: function (e) {
e.currentTarget.className = 'team';
},
dragStartTeam: function (e) {
var target = e.currentTarget;
var dataTransfer = e.originalEvent.dataTransfer;
dataTransfer.effectAllowed = 'copyMove';
dataTransfer.setData("text/plain", "Team " + e.currentTarget.dataset.value);
var team = Storage.teams[e.currentTarget.dataset.value];
var filename = team.name;
if (team.format) filename = '[' + team.format + '] ' + filename;
filename = $.trim(filename).replace(/[\\\/]+/g, '') + '.txt';
var urlprefix = "data:text/plain;base64,";
if (document.location.protocol === 'https:') {
// Chrome is dumb and doesn't support data URLs in HTTPS
urlprefix = "https://play.pokemonshowdown.com/action.php?act=dlteam&team=";
}
var contents = Storage.exportTeam(team.team).replace(/\n/g, '\r\n');
var downloadurl = "text/plain:" + filename + ":" + urlprefix + encodeURIComponent(window.btoa(unescape(encodeURIComponent(contents))));
console.log(downloadurl);
dataTransfer.setData("DownloadURL", downloadurl);
app.dragging = e.currentTarget;
app.draggingRoom = this.id;
app.draggingLoc = parseInt(e.currentTarget.dataset.value, 10);
var elOffset = $(e.currentTarget).offset();
app.draggingOffsetX = e.originalEvent.pageX - elOffset.left;
app.draggingOffsetY = e.originalEvent.pageY - elOffset.top;
this.finalOffset = null;
setTimeout(function () {
$(e.currentTarget).parent().addClass('dragging');
}, 0);
},
dragEndTeam: function (e) {
this.finishDrop();
},
finishDrop: function () {
var teamEl = app.dragging;
app.dragging = null;
var originalLoc = parseInt(teamEl.dataset.value, 10);
if (isNaN(originalLoc)) {
throw new Error("drag failed");
}
var newLoc = Math.floor(app.draggingLoc);
if (app.draggingLoc < originalLoc) newLoc += 1;
var team = Storage.teams[originalLoc];
var edited = false;
if (newLoc !== originalLoc) {
Storage.teams.splice(originalLoc, 1);
Storage.teams.splice(newLoc, 0, team);
for (var room in app.rooms) {
var selection = app.rooms[room].$('button.teamselect').val();
if (!selection || selection === 'random') continue;
var obj = app.rooms[room].id === "" ? app.rooms[room] : app.rooms[room].tournamentBox;
if (originalLoc === obj.curTeamIndex) {
obj.curTeamIndex = newLoc;
} else if (originalLoc > obj.curTeamIndex && newLoc <= obj.curTeamIndex) {
obj.curTeamIndex++;
} else if (originalLoc < obj.curTeamIndex && newLoc >= obj.curTeamIndex) {
obj.curTeamIndex--;
}
}
edited = true;
}
// possibly half-works-around a hover issue in
this.$('.teamlist').css('pointer-events', 'none');
$(teamEl).parent().removeClass('dragging');
var format = this.curFolder;
if (app.draggingFolder) {
var $folder = $(app.draggingFolder);
app.draggingFolder = null;
var $plusOneFolder = $folder.find('.plusonefolder');
$folder.removeClass('active');
if (!$plusOneFolder.length) {
$folder.prepend('<strong style="float:right;margin-right:3px;padding:0 2px;border-radius:3px;background:#CC8500;color:white" class="plusonefolder">+1</strong>');
} else {
var count = Number($plusOneFolder.text().substr(1)) + 1;
$plusOneFolder.text('+' + count);
}
format = $folder.data('value');
if (format.slice(-1) === '/') {
team.folder = format.slice(0, -1);
} else {
team.format = format;
}
edited = true;
this.updateTeamList();
} else {
if (format.slice(-1) === '/') {
format = format.slice(0, -1);
if (format && format.slice(0, 3) !== 'gen') format = 'gen6' + format;
team.folder = format;
edited = true;
}
this.updateTeamList();
}
if (edited) {
Storage.saveTeam(team);
app.user.trigger('saveteams');
}
// We're going to try to animate the team settling into its new position
if (this.finalOffset) {
// event.pageY and event.pageX are buggy on literally every browser:
// in Chrome:
// event.pageX|pageY is the position of the bottom left corner of the draggable, instead
// of the mouse position
// in Safari:
// window.innerHeight * 2 - window.outerHeight - event.pageY is the mouse position
// No, I don't understand what's going on, either, but unsurprisingly this fails utterly
// if the page is zoomed.
// in Firefox:
// event.pageX|pageY are straight-up unsupported
// if you don't believe me, uncomment and see for yourself:
// console.log('x,y = ' + [e.originalEvent.x, e.originalEvent.y]);
// console.log('screenX,screenY = ' + [e.originalEvent.screenX, e.originalEvent.screenY]);
// console.log('clientX,clientY = ' + [e.originalEvent.clientX, e.originalEvent.clientY]);
// console.log('pageX,pageY = ' + [e.originalEvent.pageX, e.originalEvent.pageY]);
// Because of this, we're just going to steal the values from the drop event, where
// everything is sane.
var $newTeamEl = this.$('.team[data-value=' + newLoc + ']');
if (!$newTeamEl.length) return;
var finalPos = $newTeamEl.offset();
$newTeamEl.css('transform', 'translate(' + (this.finalOffset[0] - finalPos.left) + 'px, ' + (this.finalOffset[1] - finalPos.top) + 'px)');
setTimeout(function () {
$newTeamEl.css('transition', 'transform 0.15s');
// it's 2015 and Safari doesn't support unprefixed transition!!!
$newTeamEl.css('-webkit-transition', '-webkit-transform 0.15s');
$newTeamEl.css('transform', 'translate(0px, 0px)');
});
}
},
dragEnterTeam: function (e) {
if (!app.dragging) return;
var $draggingLi = $(app.dragging).parent();
this.dragLeaveFolder();
if (e.currentTarget === app.dragging) {
e.preventDefault();
return;
}
var hoverLoc = parseInt(e.currentTarget.dataset.value, 10);
if (app.draggingLoc > hoverLoc) {
// dragging up
$(e.currentTarget).parent().before($draggingLi);
app.draggingLoc = parseInt(e.currentTarget.dataset.value, 10) - 0.5;
} else {
// dragging down
$(e.currentTarget).parent().after($draggingLi);
app.draggingLoc = parseInt(e.currentTarget.dataset.value, 10) + 0.5;
}
},
dragEnterFolder: function (e) {
if (!app.dragging) return;
this.dragLeaveFolder();
if (e.currentTarget === app.draggingFolder) {
return;
}
var format = e.currentTarget.dataset.value;
if (format === '+' || format === '++' || format === 'all' || format === this.curFolder) {
return;
}
if (parseInt(app.dragging.dataset.value, 10) >= Storage.teams.length && format.slice(-1) !== '/') {
// dragging a team file, already has a known format
return;
}
app.draggingFolder = e.currentTarget;
$(app.draggingFolder).addClass('active');
// amusing note: using .detach() instead of .hide() will make `dragend` not fire
$(app.dragging).parent().hide();
},
dragLeaveFolder: function (e) {
// sometimes there's a race condition and dragEnter happens before dragLeave
if (e && e.currentTarget !== app.draggingFolder) return;
if (!app.dragging || !app.draggingFolder) return;
$(app.draggingFolder).removeClass('active');
app.draggingFolder = null;
$(app.dragging).parent().show();
},
defaultDragEnterTeam: function (e) {
var dataTransfer = e.originalEvent.dataTransfer;
if (!dataTransfer) return;
if (dataTransfer.types.indexOf && dataTransfer.types.indexOf('Files') === -1) return;
if (dataTransfer.types.contains && !dataTransfer.types.contains('Files')) return;
if (dataTransfer.files[0] && dataTransfer.files[0].name.slice(-4) !== '.txt') return;
// We're dragging a file! It might be a team!
if (app.curFolder && app.curFolder.slice(-1) !== '/') {
this.selectFolder('all');
}
this.$('.teamlist').append('<li class="dragging"><div class="team" data-value="' + Storage.teams.length + '"></div></li>');
app.dragging = this.$('.dragging .team')[0];
app.draggingRoom = this.id;
app.draggingLoc = Storage.teams.length;
app.draggingOffsetX = 180;
app.draggingOffsetY = 25;
},
defaultDropTeam: function (e) {
if (e.originalEvent.dataTransfer.files && e.originalEvent.dataTransfer.files[0]) {
var file = e.originalEvent.dataTransfer.files[0];
var name = file.name;
if (name.slice(-4) !== '.txt') {
app.dragging = null;
this.updateTeamList();
app.addPopupMessage("Your file is not a valid team. Team files are .txt files.");
return;
}
var reader = new FileReader();
var self = this;
reader.onload = function (e) {
var team;
try {
team = Storage.packTeam(Storage.importTeam(e.target.result));
} catch (err) {
app.addPopupMessage("Your file is not a valid team.");
self.updateTeamList();
return;
}
var name = file.name;
if (name.slice(name.length - 4).toLowerCase() === '.txt') {
name = name.substr(0, name.length - 4);
}
var format = '';
var bracketIndex = name.indexOf(']');
if (bracketIndex >= 0) {
format = name.substr(1, bracketIndex - 1);
if (format && format.slice(0, 3) !== 'gen') format = 'gen6' + format;
name = $.trim(name.substr(bracketIndex + 1));
}
Storage.teams.push({
name: name,
format: format,
team: team,
folder: '',
iconCache: ''
});
self.finishDrop();
};
reader.readAsText(file);
}
this.finalOffset = [e.originalEvent.pageX - app.draggingOffsetX, e.originalEvent.pageY - app.draggingOffsetY];
},
/*********************************************************
* Team view
*********************************************************/
updateTeamView: function () {
this.curChartName = '';
this.curChartType = '';