-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
803 lines (770 loc) · 36.9 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.1.3/slate/bootstrap.min.css">
<link rel="stylesheet" href="./css/component-chosen.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/image-select/2.0/ImageSelect.css">
<link rel="preload" href="assets/bignoodletoo.ttf" as="font" type="font/ttf" crossorigin="anonymous">
<title>Composition Cheat Sheet Builder</title>
<style>
@font-face {
font-family: 'BigNoodleTooOblique';
src: url('assets/bignoodletoo.ttf');
}
</style>
</head>
<body>
<form>
<div class="container">
<div class="container-fluid">
<br/>
<br/>
<h1>Composition Cheat Sheet Builder</h1>
<div class="row">
<br/>
</div>
<div class="row">
<div class='col-6 col-md-6 mt-4'>
<label for="image-title"><input type="checkbox" class="form-check-input"
id="image-title-enabled" name="image-title-enabled"
onchange="toggleTitle(); return false" checked>Image
title</label>
<input type="text" name="image-title" id="image-title" value="Composition Cheat Sheet"
class="form-control">
</div>
</div>
<div class="row">
<br/>
</div>
<div class="row" id="row-anchor"></div>
<div class="row">
<div class='col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-4'>
<div class="btn-group">
<a href='#' id='addComp' onclick='addComp(); return false' class='btn btn-primary btn-sm'>Add
composition</a>
<a href='#' class="btn btn-primary btn-sm" onclick="generateImage();">Generate image</a>
</div>
</div>
</div>
<div class="row">
<br/>
</div>
</div>
</div>
<template id="comp-template">
<div class="row">
<div class='col-md-auto'>
<div class="btn-group-vertical">
<label for="comp[i]-name">Composition name</label>
<input type="text" class="form-control form-control-sm" id="comp[i]-name" name="comp[i]-name"
value="Composition [i]">
</div>
</div>
<div class='col-md-auto'>
<label for="hero-player"><input type="checkbox" class="form-check-input"
id="comp[i]-player-enabled" name="comp[i]-player-enabled"
onchange="togglePlayer([i]); return false">Player names</label>
</div>
</div>
<div id="comp[i]" class="row">
<div id="comp[i]-hero1">
</div>
<div id="comp[i]-hero2">
</div>
<div id="comp[i]-hero3">
</div>
<div id="comp[i]-hero4">
</div>
<div id="comp[i]-hero5">
</div>
<div id="comp[i]-hero6">
</div>
</div>
<div class="row">
<div class="col-6 col-md-6 mt-4">
<label for="comp[i]-desc"><input type="checkbox" class="form-check-input" id="comp[i]-desc-enabled"
name="comp[i]-desc-enabled"
onchange="toggleDesc([i]); return false" checked>
Description</label>
<div class="card">
<textarea class="form-control" id="comp[i]-desc" name="comp[i]-desc" rows="4"></textarea>
</div>
</div>
<div class="col-2 col-md-2 mt-2" id="comp[i]-map1-anchor"><br/>
<label for="comp[i]-map">
<input type="checkbox" class="form-check-input" id="comp[i]-map-enabled"
name="comp[i]-map-enabled" onchange="toggleMap([i]); return false">
Maps
<a href="#" id="comp[i]-add-map" onclick="addMap([i]); return false" hidden>+ Add map</a>
</label><br/>
<div class="card">
<img src="./assets/maps/Blizzard World.jpg" id="comp[i]-map1-image" width="100%" class="rounded"
alt="">
<div id="comp[i]-map1-select">
</div>
</div>
</div>
</div>
<div class="row">
<br/>
</div>
</template>
<template id="add-map-template">
<div class="col-2 col-md-2 mt-2" id="comp[i]-map[j]-anchor"><br/>
<label for="comp[i]-map">
<a href="#" id="comp[i]-add-map" onclick="removeMap([i]); return false">- Remove</a>
</label><br/>
<div class="card">
<img src="./assets/maps/Blizzard World.jpg" id="comp[i]-map[j]-image" width="100%" class="rounded">
<div id="comp[i]-map[j]-select">
</div>
</div>
</div>
</template>
<template id="comp-column-template">
<div class="col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-4">
<div class="card">
<div id="comp[i]-hero-player" style="display: none;">
<input type="text" id="comp[i]-player-names" name="comp[i]-player-names[]"
class="form-control form-control-sm" maxlength="12">
</div>
<div id="comp[i]-img[j]-anchor">
<img src="./assets/heroes/Ana.png" id="comp[i]-img[j]" width="100%">
</div>
<a href="#" id="comp[i]-addHero[j]" onclick="addHero([i], [j]); return false" class="nav-link">+
Add</a>
<div id="hero-select"></div>
</div>
</div>
</template>
<template id="single-hero-img-template">
<img src="./assets/heroes/[hero1].png" id="comp[i]-img[j]" width="100%">
</template>
<template id="double-hero-img-template">
<img src="./assets/heroes/[hero1].png" id="comp[i]-img[j]" width="50%">
<div align="right">
<img src="./assets/heroes/[hero2].png" id="comp[i]-img[j]" width="50%" align="right">
</div>
</template>
<template id="triple-hero-img-template">
<div align="center">
<img src="./assets/heroes/[hero1].png" id="comp[i]-img[j]" width="50%">
</div>
<img src="./assets/heroes/[hero2].png" id="comp[i]-img[j]" width="50%">
<img src="./assets/heroes/[hero3].png" id="comp[i]-img[j]" width="50%" align="right">
</template>
<template id="quadra-hero-img-template">
<img src="./assets/heroes/[hero2].png" id="comp[i]-img[j]" width="50%">
<img src="./assets/heroes/[hero1].png" id="comp[i]-img[j]" width="50%" align="right">
<img src="./assets/heroes/[hero3].png" id="comp[i]-img[j]" width="50%">
<img src="./assets/heroes/[hero4].png" id="comp[i]-img[j]" width="50%" align="right">
</template>
<template id="add-hero-template">
<a href="#" id="comp[i]-addHero[j]" onclick="addHero([i], [j]); return false" class="nav-link">+ Add</a>
</template>
<template id="remove-hero-template">
<a href="#" id="comp[i]-removeHero[j]" onclick="removeHero([i], [j]); return false" class="nav-link">-
Remove</a>
</template>
<template id="hero-select-template">
<label for="comp[i]-hero[j]"></label><select data-placeholder="Ana" name="comp[i]-hero[j][]"
id="comp[i]-hero[j]"
onchange="showHero([i], [j]); return false"
class="form-control form-control-chosen">
<option data-img-src="./assets/heroes/No Hero.png" value="No Hero">No Hero</option>
<option data-img-src="./assets/heroes/Ana.png" value="Ana" selected>Ana</option>
<option data-img-src="./assets/heroes/Ashe.png" value="Ashe">Ashe</option>
<option data-img-src="./assets/heroes/Baptiste.png" value="Baptiste">Baptiste</option>
<option data-img-src="./assets/heroes/Bastion.png" value="Bastion">Bastion</option>
<option data-img-src="./assets/heroes/Brigitte.png" value="Brigitte">Brigitte</option>
<option data-img-src="./assets/heroes/DVa.png" value="DVa">DVa</option>
<option data-img-src="./assets/heroes/Doomfist.png" value="Doomfist">Doomfist</option>
<option data-img-src="./assets/heroes/Echo.png" value="Echo">Echo</option>
<option data-img-src="./assets/heroes/Genji.png" value="Genji">Genji</option>
<option data-img-src="./assets/heroes/Hanzo.png" value="Hanzo">Hanzo</option>
<option data-img-src="./assets/heroes/Junkrat.png" value="Junkrat">Junkrat</option>
<option data-img-src="./assets/heroes/Lucio.png" value="Lucio">Lucio</option>
<option data-img-src="./assets/heroes/McCree.png" value="McCree">McCree</option>
<option data-img-src="./assets/heroes/Mei.png" value="Mei">Mei</option>
<option data-img-src="./assets/heroes/Mercy.png" value="Mercy">Mercy</option>
<option data-img-src="./assets/heroes/Moira.png" value="Moira">Moira</option>
<option data-img-src="./assets/heroes/Orisa.png" value="Orisa">Orisa</option>
<option data-img-src="./assets/heroes/Pharah.png" value="Pharah">Pharah</option>
<option data-img-src="./assets/heroes/Reaper.png" value="Reaper">Reaper</option>
<option data-img-src="./assets/heroes/Reinhardt.png" value="Reinhardt">Reinhardt</option>
<option data-img-src="./assets/heroes/Roadhog.png" value="Roadhog">Roadhog</option>
<option data-img-src="./assets/heroes/Sigma.png" value="Sigma">Sigma</option>
<option data-img-src="./assets/heroes/Soldier 76.png" value="Soldier 76">Soldier 76</option>
<option data-img-src="./assets/heroes/Sombra.png" value="Sombra">Sombra</option>
<option data-img-src="./assets/heroes/Symmetra.png" value="Symmetra">Symmetra</option>
<option data-img-src="./assets/heroes/Torbjorn.png" value="Torbjorn">Torbjorn</option>
<option data-img-src="./assets/heroes/Tracer.png" value="Tracer">Tracer</option>
<option data-img-src="./assets/heroes/Widowmaker.png" value="Widowmaker">Widowmaker</option>
<option data-img-src="./assets/heroes/Winston.png" value="Winston">Winston</option>
<option data-img-src="./assets/heroes/Wrecking Ball.png" value="Wrecking Ball">Wrecking Ball</option>
<option data-img-src="./assets/heroes/Zarya.png" value="Zarya">Zarya</option>
<option data-img-src="./assets/heroes/Zenyatta.png" value="Zenyatta">Zenyatta</option>
</select>
</template>
<template id="map-select-template">
<select data-placeholder="Blizzard World" name="comp[i]-map[]" id="comp[i]-map[j]"
onchange="showMap([i], [j]); return false" class="form-control form-control-chosen" disabled>
<option value="Adlersbrunn">Adlersbrunn</option>
<option value="Ayutthaya">Ayutthaya</option>
<option value="Black Forest">Black Forest</option>
<option value="Blizzard World" selected>Blizzard World</option>
<option value="Busan">Busan</option>
<option value="Castillo">Castillo</option>
<option value="Dorado">Dorado</option>
<option value="Ecopoint Antartica">Ecopoint Antartica</option>
<option value="Eichenwalde">Eichenwalde</option>
<option value="Hanamura">Hanamura</option>
<option value="Havana">Havana</option>
<option value="Hollywood">Hollywood</option>
<option value="Horizon Lunar Colony">Horizon Lunar Colony</option>
<option value="Ilios">Ilios</option>
<option value="Junkertown">Junkertown</option>
<option value="Kings Row">Kings Row</option>
<option value="Lijiang Tower">Lijiang Tower</option>
<option value="Necropolis">Necropolis</option>
<option value="Nepal">Nepal</option>
<option value="Numbani">Numbani</option>
<option value="Oasis">Oasis</option>
<option value="Paris">Paris</option>
<option value="Rialto">Rialto</option>
<option value="Route 66">Route 66</option>
<option value="Temple of Anubis">Temple of Anubis</option>
<option value="Volskaya Industries">Volskaya Industries</option>
<option value="Watchpoint Gibraltar">Watchpoint Gibraltar</option>
</select>
</template>
<template id="premade-comp-select-template">
<div class="row">
<div class="col-6 mt-4 btn-group" role="group">
<button type="button" class="btn btn-primary btn-sm" onclick="importComp([i]); return false">Import
Comp
</button>
<button type="button" class="btn btn-primary btn-sm" onclick="saveCompToFile([i]); return false">
Export Comp
</button>
<select name="comp[i]-premade" id="comp[i]-premade" onchange="loadComp([i]); return false"
class="form-control form-control-chosen">
</select><br/>
</div>
<input type="file" accept="application/json" name="comp[i]-file" id="comp[i]-file"
style="visibility: hidden;" onchange="loadCompFromCustomFile([i]); return false">
</div>
</template>
<template id="premade-comp-select-options-template">
<option value="[comp-name]">[comp-name]</option>
</template>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/chosen-js@1.8.7/chosen.jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/image-select/2.0/ImageSelect.jquery.js"></script>
<!-- Optional JavaScript -->
<script>
function generateImage(){
$("canvas").remove();
let compAmount = $("div.row[id^=comp]").length;
let width = 900;
let height = 0;
let canvas = $("<canvas/>", { id: "canvas"});
canvas.attr("width", width);
canvas.attr("height", height);
$(".container-fluid").append(canvas);
let context = $("canvas")[0].getContext("2d");
context.font = `30px BigNoodleTooOblique`;
height = calculateHeight(compAmount, context);
canvas.attr("height", height);
context.fillStyle = "black";
context.fillRect(0, 0, width, height);
let x = 20;
let y = 0;
if($(`#image-title-enabled`).is(":checked")){
writeText($(`#image-title`).val(), context, "white", 100, x, 100, "left");
y += 100;
}
for (let i = 1; i <= compAmount; i++) {
generateComp(context, i, x, y);
y += 80;
if($(`#comp${i}-player-enabled`).is(":checked")){
y += 40;
}
y += 140;
if($(`#comp${i}-desc-enabled`).is(":checked")){
let text = $(`#comp${i}-desc`).val();
y += calculateTextBoxHeight(text, context, 30, 700, 30);
console.log(calculateTextBoxHeight(text, context, 30, 700, 30));
y += 40;
}
console.log(y);
}
setTimeout(function() { window.scrollTo(0,document.body.scrollHeight); }, 100);
}
function calculateHeight(compAmount, context){
let height = 0;
if($(`#image-title-enabled`).is(":checked")){
height += 100;
}
for (let i = 1; i <= compAmount; i++) {
let compHeight = 80;
if($(`#comp${i}-player-enabled`).is(":checked")){
compHeight += 40;
}
compHeight += 140;
if($(`#comp${i}-desc-enabled`).is(":checked")){
let text = $(`#comp${i}-desc`).val();
compHeight += calculateTextBoxHeight(text, context, 30, 700, 30);
console.log(calculateTextBoxHeight(text, context, 30, 700, 30));
compHeight += 40;
}
height += compHeight;
console.log(height);
}
return height;
}
function generateComp(context, pos, x, y){
let j = 1;
let oX = x;
let oY = y;
let cX = oX;
let cY = oY;
writeText($(`#comp${pos}-name`).val(), context, "white", 80, cX, cY + 80, "left");
if($(`#comp${pos}-map-enabled`).is(":checked")){
cX = 740;
$(`select[name^="comp${pos}-map"]`).each(function(){
placeImage(`./assets/maps/icons/${$(this).val()}.png`, context, cX, cY + 10, 80);
cX -= 65;
});
}
if($(`#comp${pos}-player-enabled`).is(":checked")){
cY += 120;
} else{
cY += 80;
}
for (let i = 0; i < 6; i++) {
cX = (140 * i) + 20;
let select = `select[name^="comp${pos}-hero${i + 1}"]`;
if($(`#comp${pos}-player-enabled`).is(":checked")){
let playerName = $(`input[name^="comp${pos}-player-names"]`).eq(i).val();
writeText(playerName, context, "white", 22, cX + 67.5, cY, "center");
}
switch($(select).length) {
case 1:
placeImage(`./assets/heroes/${$(select).val()}.png`, context, cX, cY, 135.5);
break;
case 2:
j = 1;
$(select).each(function() {
switch(j){
case 1:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 45, cY + 40, 90);
break;
case 2:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 5, cY, 90);
break;
default:
// Nope
}
j++;
});
break;
case 3:
j = 1;
$(select).each(function() {
switch(j){
case 1:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 50, cY + 40, 90);
break;
case 2:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX, cY + 40, 90);
break;
case 3:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 25, cY - 5, 90);
break;
default:
// Nope
}
j++;
});
break;
case 4:
j = 1;
$(select).each(function() {
switch(j){
case 1:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 35, cY + 50, 80);
break;
case 2:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX - 10, cY + 50, 80);
break;
case 3:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 15, cY + 10, 80);
break;
case 4:
placeImage(`./assets/heroes/${$(this).val()}.png`, context, cX + 65, cY + 10, 80);
break;
default:
// Nope
}
j++;
});
break;
default:
// Shouldn't happen
}
}
cY += 140;
if($(`#comp${pos}-desc-enabled`).is(":checked")){
let text = $(`#comp${pos}-desc`).val();
cX = 20;
cY += 20;
writeTextBox(text, context, "white", 30, cX, cY, "left", 700, 30);
cY += calculateTextBoxHeight(text, context, 30, 700, 30);
cY += 20;
}
}
function writeText(text, context, color, size, x, y, textAlign){
context.font = `${size}px BigNoodleTooOblique`;
context.textAlign = textAlign;
context.fillStyle = color;
context.fillText(text, x, y);
}
function calculateTextBoxHeight(text, context, size, maxWidth, lineHeight){
let words = text.split(' ');
let line = '';
let height = 0;
for(let n = 0; n < words.length; n++) {
let testLine = line + words[n] + ' ';
let metrics = context.measureText(testLine);
let testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
line = words[n] + ' ';
height += lineHeight;
} else {
line = testLine;
}
}
height += lineHeight;
return height;
}
function writeTextBox(text, context, color, size, x, y, textAlign, maxWidth, lineHeight) {
context.font = `${size}px BigNoodleTooOblique`;
context.textAlign = textAlign;
context.fillStyle = color;
let words = text.split(' ');
let line = '';
for(let n = 0; n < words.length; n++) {
let testLine = line + words[n] + ' ';
let metrics = context.measureText(testLine);
let testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
} else {
line = testLine;
}
}
context.fillText(line, x, y);
}
function placeImage(src, context, x, y, size){
let image = new Image();
image.onload = function(){
context.drawImage(image, x, y, size, size);
};
image.src = src;
}
function showHero(i, j) {
let heroAmount = $("select#comp" + i + "-hero" + j).length;
let imageAnchor = $("#comp" + i + "-img" + j + "-anchor");
imageAnchor.html("");
switch (heroAmount) {
case 1:
imageAnchor.html($("#single-hero-img-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
break;
case 2:
imageAnchor.html($("#double-hero-img-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
break;
case 3:
imageAnchor.html($("#triple-hero-img-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
break;
case 4:
imageAnchor.html($("#quadra-hero-img-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
break;
default:
}
let k = 0;
$("select#comp" + i + "-hero" + j + " option:selected").each(function () {
let hero = $(this).val();
switch (k) {
case 0:
imageAnchor.html(imageAnchor.html().replace(/\[hero1]/g, hero));
break;
case 1:
imageAnchor.html(imageAnchor.html().replace(/\[hero2]/g, hero));
break;
case 2:
imageAnchor.html(imageAnchor.html().replace(/\[hero3]/g, hero));
break;
case 3:
imageAnchor.html(imageAnchor.html().replace(/\[hero4]/g, hero));
break;
default:
}
k++;
});
}
function showMap(i, j) {
let map = $("select#comp" + i + "-map" + j).val();
$("#comp" + i + "-map" + j + "-image").attr("src", "./assets/maps/" + map + ".jpg");
}
function addHero(i, j) {
if (compHeroes[i][j - 1] === 1) {
$("#comp" + i + "-addHero" + j).after($("#hero-select-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
} else {
$("#comp" + i + "-removeHero" + j).after($("#hero-select-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
}
compHeroes[i][j - 1]++;
if (compHeroes[i][j - 1] === 1) {
$("#comp" + i + "-removeHero" + j).remove();
}
if (compHeroes[i][j - 1] === 2) {
$("#comp" + i + "-addHero" + j).after($("#remove-hero-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
}
if (compHeroes[i][j - 1] === 4) {
$("#comp" + i + "-addHero" + j).remove();
}
$("[id^=comp" + i + "].form-control-chosen").chosen();
showHero(i, j);
}
function removeHero(i, j) {
let selectedHero = $("select#comp" + i + "-hero" + j);
selectedHero.last().chosen("destroy");
selectedHero.last().remove();
compHeroes[i][j - 1]--;
if (compHeroes[i][j - 1] === 1) {
$("#comp" + i + "-removeHero" + j).remove().trigger("chosen:updated");
}
if (compHeroes[i][j - 1] === 3) {
$("#comp" + i + "-removeHero" + j).before($("#add-hero-template").html().replace(/\[i]/g, i).replace(/\[j]/g, j));
}
showHero(i, j);
}
function addMap(i) {
$("#comp" + i + "-map" + compMaps[i] + "-anchor").after($("#add-map-template").html().replace(/\[i]/g, i).replace(/\[j]/g, compMaps[i] + 1));
compMaps[i]++;
$("#comp" + i + "-map" + compMaps[i] + "-select").replaceWith($("#map-select-template").html().replace(/\[i]/g, i).replace(/\[j]/g, compMaps[i]));
let compMapAttr = $("#comp" + i + "-map" + compMaps[i]);
compMapAttr.attr("disabled", !compMapAttr.attr("disabled"));
$("[id^=comp" + i + "].form-control-chosen").chosen();
}
function removeMap(i) {
$("#comp" + i + "-map" + compMaps[i] + "-anchor").remove();
compMaps[i]--;
}
function addComp() {
compAmount++;
$("#row-anchor").before($("#comp-template").html().replace(/\[i]/g, compAmount));
let j = 0;
let compDiv = $("#comp" + compAmount);
compDiv.find("div").each(function () {
j++;
$(this).replaceWith($("#comp-column-template").html().replace(/\[i]/g, compAmount).replace(/\[j]/g, j));
});
j = 0;
compDiv.find("div #hero-select").each(function () {
j++;
$(this).replaceWith($("#hero-select-template").html().replace(/\[i]/g, compAmount).replace(/\[j]/g, j));
});
$("#comp" + compAmount + "-map1-select").replaceWith($("#map-select-template").html().replace(/\[i]/g, compAmount).replace(/\[j]/g, 1));
compHeroes[compAmount] = [1, 1, 1, 1, 1, 1];
compMaps[compAmount] = 1;
if (compAmount > 1) {
location.href = "#comp" + compAmount;
}
loadCompSelectionBox(compAmount);
$("[id^=comp" + compAmount + "].form-control-chosen").chosen();
}
function loadComp(i) {
let name = $("#comp" + i + "-premade").children("option:selected").val();
if (name !== "Load premade comp") {
loadCompFromFile(i, name);
}
}
function importComp(i) {
$("#comp" + i + "-file").click();
}
function loadCompFromCustomFile(i) {
let reader = new FileReader();
reader.onload = function () {
let json = JSON.parse(reader.result);
processCompFile(i, json);
};
reader.readAsText($("#comp" + i + "-file")[0].files[0]);
}
function loadCompFromFile(i, name) {
let file = "./comps/" + name + ".json";
$.getJSON(file, function (json) {
processCompFile(i, json);
});
}
function processCompFile(i, json) {
$("#comp" + i + "-name").attr("value", json.name);
$("#comp" + i + "-desc").html(json.description);
let j = 1;
$.each(json.comp, function (key, value) {
let heroAmount = 1;
if ($.isArray(value)) {
heroAmount = value.length;
}
let compSelect = $("select#comp" + i + "-hero" + j);
let difference = compSelect.length - heroAmount;
if (difference < 0) {
for (let k = 0; k < Math.abs(difference); k++) {
addHero(i, j);
}
} else if (difference > 0) {
for (let k = 0; k < difference; k++) {
removeHero(i, j);
}
}
if ($.isArray(value)) {
for (let k = 0; k < heroAmount; k++) {
$("select#comp" + i + "-hero" + j).eq(k).val(value[k]);
}
} else {
let compSelect = $("select#comp" + i + "-hero" + j);
if (compSelect.length > 1) {
for (let k = 0; k <= compSelect.length; k++) {
removeHero(i, j);
}
}
compSelect.eq(0).val(value);
}
showHero(i, j);
j++;
});
compMaps[i] = 1;
let compSelect = $("select#comp" + i + "-hero" + j);
if (compSelect.length > 1) {
for (let k = 2; k <= $("[id^=comp" + i + "-map] select").length; k++) {
$("comp" + i + "-map" + k + "-anchor").remove();
}
}
let playerEnabled = $("input#comp" + i + "-player-enabled");
if (json.playernames !== undefined) {
if (!playerEnabled.is(":checked")) {
playerEnabled.prop('checked', true);
togglePlayer(i);
}
$.each(json.playernames, function (key, value) {
$("input#comp" + i + "-player-names").eq(key).val(value);
});
} else {
if (playerEnabled.is(":checked")) {
playerEnabled.prop('checked', false);
togglePlayer(i);
}
}
let l = 1;
$.each(json.maps, function (key, value) {
if (l > 1) {
addMap(i);
} else {
let checkMapComp = $("#comp" + i + "-map-enabled");
if (!checkMapComp.prop("checked")) {
checkMapComp.prop("checked", true);
toggleMap(i);
}
}
$("#comp" + i + "-map" + l).val(value);
showMap(i, l);
l++;
});
let formControlChosen = $("[id^=comp" + i + "].form-control-chosen");
formControlChosen.chosen();
formControlChosen.trigger("chosen:updated");
}
function saveCompToFile(i) {
let finalResult = {};
finalResult["name"] = $("#comp" + i + "-name").val();
let comp = [];
let playerNames = [];
for (let j = 1; j <= 6; j++) {
let compSelect = $("select#comp" + i + "-hero" + j);
if (compSelect.length > 1) {
let subHeroes = [];
for (let k = 0; k < compSelect.length; k++) {
subHeroes.push(compSelect.eq(k).val());
}
comp.push(subHeroes);
} else {
comp.push($("select#comp" + i + "-hero" + j).eq(0).val());
}
playerNames.push($("input#comp" + i + "-player-names").eq(j - 1).val())
}
let maps = [];
for (let l = 0; l < $("[id^=comp" + 1 + "-map] select").length; l++) {
maps.push($("[id^=comp" + 1 + "-map] select").eq(l).val());
}
finalResult["playernames"] = playerNames;
finalResult["comp"] = comp;
finalResult["description"] = $("#comp" + i + "-desc").val();
if ($("#comp" + i + "-map-enabled").val()) {
finalResult["maps"] = maps;
}
let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(finalResult, null, 4));
let linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', finalResult["name"] + ".json");
linkElement.click();
}
function loadCompSelectionBox(i) {
$("#comp" + i).before($("#premade-comp-select-template").html().replace(/\[i]/g, i));
$("#comp" + i + "-premade").html($("#premade-comp-select-options-template").html().replace(/\[comp-name]/g, "Load premade comp"));
$.getJSON("./comps/getcomps.php", function (json) {
$.each(json, function (key, value) {
$("#comp" + i + "-premade").find("option").last().after($("#premade-comp-select-options-template").html().replace(/\[comp-name]/g, value));
});
$("#comp" + i + "-premade").trigger("chosen:updated");
});
}
function toggleDesc(i) {
let compDesc = $("#comp" + i + "-desc");
compDesc.attr("disabled", !compDesc.attr("disabled"));
}
function togglePlayer(i) {
let compPlayerNames = $("div #comp" + i + "-hero-player");
compPlayerNames.toggle();
}
function toggleTitle() {
$("#image-title").attr("disabled", !$("#image-title").attr("disabled"));
}
function toggleMap(i) {
let compMapMatch = $("[id^=comp" + i + "-map] select");
let compAddMap = $("#comp" + i + "-add-map");
compMapMatch.attr("disabled", !compMapMatch.attr("disabled")).trigger("chosen:updated");
compAddMap.attr("hidden", !compAddMap.attr("hidden"));
}
var compAmount = 0;
var compHeroes = [];
var compMaps = [];
$.ajaxSetup({'cache': false});
addComp();
</script>
</form>
</body>
</html>