-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiltseek.js
1649 lines (1452 loc) · 47.1 KB
/
tiltseek.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
// JavaScript Document
var champList;
var userUsername;
var userAccountId;
var userSummonerId;
var currentGame;
var stats;
var matchLists = [];
var matches = [];
var summonersUsername = [];
var summonersAccountId = [];
var summonersSummonerId = [];
var summonersChampIds = [];
var summonersMastery = [];
var summonersLeague = [];
//final data
var losingStreak = []; //losing streak
var masteryPoints = []; //champion mastery points
var winRate = []; //winrate in current ranked season
var wins = []; //wins in current ranked season
var losses = []; //losses in current ranked season
var timeSincePlayed = []; //time since champion was last played by the summoner in days
var aggressiveness = []; //score from 0 to 1 of a player's aggressiveness in lane
var warding = []; //score from 0 to 1 of a player's warding
var campScore = []; //score estimating how much a player should be camped
//set match history length per user (max 100)
var matchHistoryLength = 20;
var matchesLoaded = 0;
var apiVersion = "9.2.1";
//async function list
var runList = [
getAPIVersion,
timeComp,
loadUser,
timeComp,
loadCurrentGame,
timeComp,
loadChampList,
timeComp,
loadSummoners,
timeComp,
loadMatchLists,
timeComp,
loadMatches,
timeComp,
loadStats,
timeComp,
loadMastery,
timeComp,
loadLeague,
timeComp,
processData,
timeComp,
loadDisplay,
timeComp
];
var timeCompare = Date.now();
//run first async item
runList[0](runList, 0);
function timeComp(runList, index) {
current = Date.now();
console.log(current - timeCompare);
timeCompare = Date.now();
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
//load administrator message
getAdminMsg().then(
function success(data) {
console.log(data);
if (data != "" && getCookie("lastAdminMsg") != data) {
console.log(document.getElementById("adminmsg").textContent);
document.getElementById("adminmsgtext").textContent = data;
document.getElementById("adminmsg").style.opacity = 1;
} else {
document.getElementById("adminmsg").style.display = "none";
}
},
function fail(data) {
console.log("failed to retrieve admin message");
document.getElementById("adminmsg").style.display = "none";
}
);
document.getElementById("adminmsgx").addEventListener("click", function(event) {
document.getElementById("adminmsg").style.opacity = 0;
document.getElementById("adminmsg").style.display = "none";
setCookie("lastAdminMsg", document.getElementById("adminmsgtext").textContent, 30);
});
function getAdminMsg() {
var promiseObj = new Promise(function (resolve, reject) {
$.ajax({
url: '/adminMsg',
success: function (data) {
if (data.error == null) {
resolve(data);
} else {
reject(data.error);
}
},
error: function () {
console.log("Oops! Ajax messed up.");
}
});
});
return promiseObj;
}
//gets parameter from current url
function getQuery(q) {
return decodeURIComponent((window.location.search.match(new RegExp('[?&]' + q + '=([^&]+)')) || [, null])[1]);
}
document.getElementById("textfield").value = getQuery("username");
function getRegionID(theRegion) {
var regions = ["NA", "EUW", "EUNE", "BR", "TR", "RU", "LAN", "LAS", "OCE", "KR", "JP"];
var regionIDs = ["na1", "euw1", "eun1", "br1", "tr1", "ru", "la1", "la2", "oc1", "kr", "jp1"];
return regionIDs[regions.indexOf(theRegion)];
}
function getRegion(theRegionID) {
var regions = ["NA", "EUW", "EUNE", "BR", "TR", "RU", "LAN", "LAS", "OCE", "KR", "JP"];
var regionIDs = ["na1", "euw1", "eun1", "br1", "tr1", "ru", "la1", "la2", "oc1", "kr", "jp1"];
return regions[regionIDs.indexOf(theRegionID)];
}
var region = getRegion(getQuery("region"));
//region select button event listeners
var regionObjs = document.getElementsByClassName("region-button");
for (var i = 0; i < regionObjs.length; i++) {
var regions = document.getElementById("region-select").getElementsByTagName("li");
if (regions[i].textContent == region) {
document.getElementById(regions[i].textContent).style.backgroundColor = "#111";
} else {
document.getElementById(regions[i].textContent).style.backgroundColor = "#333";
}
(function (index) {
regionObjs[index].addEventListener("click", function () {
region = this.textContent;
setCookie('region', region, 30);
console.log(getRegionID(region));
var regions = document.getElementById("region-select").getElementsByTagName("li");
for (var i = 0; i < regions.length; i++) {
if (regions[i].textContent == region) {
document.getElementById(regions[i].textContent).style.backgroundColor = "#111";
} else {
document.getElementById(regions[i].textContent).style.backgroundColor = "#333";
}
}
});
})(i);
}
function selectText(textField) {
textField.focus();
textField.select();
}
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;';
}
//Enter key handler for textbox
document.getElementById("textfield").addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("submit-button").click();
}
});
document.getElementById("submit-button").addEventListener("click", function(event) {
if (document.getElementById("textfield").value != "") {
window.location.href = "tiltseek.html?username=" + document.getElementById("textfield").value + "®ion=" + getRegionID(region);
} else {
document.getElementById("textfield").className = "textInputError";
document.getElementById("submit-button").blur();
}
});
function displayError(errorMsg) {
document.getElementById("loader").style.display = "none";
document.getElementById("lookup").style.display = "block";
document.getElementById("logo").style.display = "block";
document.getElementById("errorMsg").style.display = "block";
document.getElementById("errorMsg").textContent = errorMsg;
document.getElementById("errorMsg").textContent = errorMsg;
document.getElementById("fadeIn").style.opacity = 1;
document.getElementById("textfield").select();
}
function handleError(errorNum, message404) {
if (errorNum == 400) {
console.log("Bad request");
displayError("Bad request. Either you did something weird or you found a bug. Maybe both.");
} else if (errorNum == 429) {
console.log("Rate limit exceeded");
displayError("NOOOOO the dreaded Reddit hug of death. Rate limits have been exceeded. Check back in a few hours.");
} else if (errorNum == 503) {
console.log("Rito servers ded");
displayError("Rito servers didn't respond. Looks like you're on your own this game.");
} else if (errorNum == 404) {
console.log("404");
displayError(message404);
} else if (errorNum == 778) {
console.log("Invalid Region")
displayError("A HUGE thank you to user Derpthemeus for helping me discover and patch this security flaw. Email bugs to contact@tiltseeker.com");
} else {
displayError("Oops... something happened. Error: " + errorNum);
}
}
//Load the current user summoner name from the URL parameters
function getAPIVersion(runList, index) {
let url = 'https://ddragon.leagueoflegends.com/api/versions.json';
fetch(url)
.then(res => res.json())
.then((versions) => {
apiVersion = versions[0]
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
})
.catch(err => {
console.log("fail: Riot's Data Dragon service is down. Cannot fetch current game version.");
handleError(404, "Riot's Data Dragon service is down. Cannot fetch current game version."); });
}
//Load the current user summoner name from the URL parameters
function loadUser(runList, index) {
getUserInfoByName(getQuery("username")).then(
function success(data) {
userUsername = decodeURIComponent(data.name);
userAccountId = data.accountId;
userSummonerId = data.id;
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "User not found. Check if the the region is correct.");
}
);
}
//Load the current game info for the summoner given in URL parameters
function loadCurrentGame(runList, index) {
getCurrentGame(userSummonerId).then(
function success(data) {
currentGame = data;
console.log(data);
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Player not in game. Check if the the region is correct.");
}
);
}
//Loads the static champ data from the server
function loadChampList(runList, index) {
getChampList().then(
function success(data) {
champList = data;
console.log(data);
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Servers under heavy load. Failed to load champion data.");
}
);
}
//Load the summoners in the current game
function loadSummoners(runList, index) {
var totalLoaded = 0;
for (let i = 0; i < currentGame.participants.length; i++) {
getSummoners(currentGame.participants[i].summonerName).then(
function success(data) {
summonersUsername[i] = data.name;
summonersAccountId[i] = data.accountId;
summonersSummonerId[i] = data.id;
totalLoaded++;
percentDone = 10 * totalLoaded / summonersUsername.length;
document.getElementById("loadingBar").style.width = percentDone + "%";
//last one complete
if (totalLoaded == currentGame.participants.length) {
console.log(summonersUsername);
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Couldn't load the summoners in the current game. Looks like we still have bugs to squish.");
}
)
}
// getSummoners(currentGame.participants).then(
// function success(data) {
// //sort data properly
// for (var j = 0; j < currentGame.participants.length; j++) {
// for (var i = 0; i < data.length; i++) {
// if (currentGame.participants[j].summonerName == data[i].name) {
// summonersUsername.push(data[i].name);
// summonersAccountId.push(data[i].accountId);
// summonersSummonerId.push(data[i].id);
// break;
// }
// }
// }
//
// //run next async function
// if (runList[index + 1]) {
// runList[index + 1](runList, index + 1);
// }
// },
// function fail(data) {
// console.log("fail: " + data);
// handleError(data, "Couldn't load the summoners in the current game. Looks like we still have bugs to squish.");
// }
// );
}
//Load all the users matchlists
function loadMatchLists(runList, index) {
var totalLoaded = 0;
for (let i = 0; i < summonersAccountId.length; i++) {
getMatchLists(summonersAccountId[i]).then(
function success(data) {
matchLists[i] = data.matches;
totalLoaded++;
percentDone = 10 + 10 * totalLoaded / summonersUsername.length;
document.getElementById("loadingBar").style.width = percentDone + "%";
//last one complete
if (totalLoaded == summonersAccountId.length) {
console.log(matchLists);
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Failed to load players' matchlists. Looks like you found a bug.");
}
);
}
// getMatchLists(summonersAccountId).then(
// function success(data) {
// for (var i = 0; i < data.length; i++) {
// matchLists.push(data[i].matches);
// }
// console.log(matchLists);
// //run next async function
// if (runList[index + 1]) {
// runList[index + 1](runList, index + 1);
// }
// },
// function fail(data) {
// console.log("fail: " + data);
// handleError(data, "Failed to load players' matchlists. Looks like you found a bug.");
// }
// );
}
//Load all the users matches
function loadMatches(runList, index) {
//intialize matches variable
for (var i = 0; i < summonersAccountId.length; i++) {
matches[i] = [];
}
for (var i = 0; i < summonersAccountId.length; i++) {
for (var j = 0; j < matchHistoryLength; j++) {
}
}
var totalLoaded = 0;
var promises = [];
for (let i = 0; i < summonersAccountId.length; i++) {
for (let j = 0; j < matchHistoryLength && j < matchLists[i].length; j++) {
promises.push(getMatch(matchLists[i][j].gameId).then(
function success(data) {
matches[i][j] = data;
totalLoaded++;
percentDone = 20 + 60 * totalLoaded / (summonersUsername.length * matchHistoryLength);
document.getElementById("loadingBar").style.width = percentDone + "%";
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Failed to load players' historical matches. Looks like you found a bug!");
}
));
}
}
Promise.all(promises).then(() => {
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
});
//loop through all users and their matches
// var i = 0;
// var j = 0;
// myLoop(runList, index);
// function myLoop(runList, index) {
// console.log(i + " " + j);
// getMatch(matchLists[i][j].gameId).then(
// function success(data) {
// matchesLoaded++;
// percentDone = 100 * matchesLoaded / (summonersUsername.length * matchHistoryLength);
// document.getElementById("loadingBar").style.width = percentDone + "%";
// matches[i].push(data);
// if (j < matchHistoryLength - 1) {
// j++;
// if (matchLists[i][j]) {
// myLoop(runList, index);
// } else {
// matchesLoaded += (matchHistoryLength - j);
// i++;
// j = 0;
// myLoop(runList, index);
// }
// } else if (i < matchLists.length - 1 && j >= matchHistoryLength - 1) {
// i++;
// j = 0;
// myLoop(runList, index);
// } else if (i >= matchLists.length - 1 && j >= matchHistoryLength - 1) {
// console.log(matches);
// //run next async function
// if (runList[index + 1]) {
// runList[index + 1](runList, index + 1);
// }
// }
// },
// function fail(data) {
// console.log("fail: " + data);
// handleError(data, "Failed to load players' historical matches. Looks like you found a bug!");
// }
// );
// }
}
//Load stats data for champs in current game and historical matches
function loadStats(runList, index) {
var champIds = [];
//current game
for (var i = 0; i < currentGame.participants.length; i++) {
if (champIds.indexOf(currentGame.participants[i].championId) == -1) {
champIds.push(currentGame.participants[i].championId);
}
}
//historical matches
for (var i = 0; i < matches.length; i++) {
for (var j = 0; j < matches[i].length; j++) {
var participant = 0;
for (var k = 0; k < matches[i][j].participantIdentities.length; k++) {
if (summonersSummonerId[i] == matches[i][j].participantIdentities[k].player.summonerId) {
participant = k;
break;
}
}
if (champIds.indexOf(matches[i][j].participants[participant].championId) == -1) {
champIds.push(matches[i][j].participants[participant].championId);
}
}
}
console.log(champIds);
getStats(champIds).then(
function success(data) {
console.log(data);
stats = data;
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Servers seem to be under heavy load. Please wait a few minutes.");
}
);
}
//Load champion masteries for summoner's current champions
function loadMastery(runList, index) {
for (var i = 0; i < currentGame.participants.length; i++) {
summonersChampIds.push(currentGame.participants[i].championId);
}
var totalLoaded = 0;
for (let i = 0; i < summonersSummonerId.length; i++) {
getMastery(summonersSummonerId[i],summonersChampIds[i]).then(
function success(data) {
summonersMastery[i] = data;
totalLoaded++;
percentDone = 80 + 10 * totalLoaded / summonersUsername.length;
document.getElementById("loadingBar").style.width = percentDone + "%";
//last one complete
if (totalLoaded == summonersSummonerId.length) {
console.log(summonersMastery);
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Failed to load players' champion masteries. Looks like you found a bug!");
}
);
}
// getMastery(summonersSummonerId,summonersChampIds).then(
// function success(data) {
// console.log(data);
// summonersMastery = data;
//
// //run next async function
// if (runList[index + 1]) {
// runList[index + 1](runList, index + 1);
// }
// },
// function fail(data) {
// console.log("fail: " + data);
// handleError(data, "Failed to load players' champion masteries. Looks like you found a bug!");
// }
// );
}
//Load summoner ranked league stats
function loadLeague(runList, index) {
var totalLoaded = 0;
for (let i = 0; i < summonersSummonerId.length; i++) {
getLeague(summonersSummonerId[i]).then(
function success(data) {
summonersLeague[i] = data;
totalLoaded++;
percentDone = 90 + 10 * totalLoaded / summonersUsername.length;
document.getElementById("loadingBar").style.width = percentDone + "%";
//last one complete
if (totalLoaded == summonersSummonerId.length) {
console.log(summonersLeague);
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
},
function fail(data) {
console.log("fail: " + data);
handleError(data, "Failed to load players' ranked data. Looks like you found a bug!");
}
);
}
// getLeague(summonersSummonerId).then(
// function success(data) {
// console.log(data);
//
// summonersLeague = data;
//
// //run next async function
// if (runList[index + 1]) {
// runList[index + 1](runList, index + 1);
// }
// },
// function fail(data) {
// console.log("fail: " + data);
// handleError(data, "Failed to load players' ranked data. Looks like you found a bug!");
// }
// );
}
//Process all the data that has been collected
function processData(runList, index) {
//+losing streak
//+champion mastery points
//+winrate
//+played champ recently
//+aggressive laner
//+good warder
//+gets first tower
//+champion's mobility (might have to hard code numbers)
//set participants in matchlists
for (var i = 0; i < matches.length; i++) {
for (var j = 0; j < matches[i].length; j++) {
for (var k = 0; k < matches[i][j].participantIdentities.length; k++) {
if (summonersSummonerId[i] == matches[i][j].participantIdentities[k].player.summonerId) {
//set participant for future use to improve speed
matches[i][j]['myParticipant'] = k;
break;
}
}
}
}
//calculate losingStreak
for (var i = 0; i < matches.length; i++) {
losingStreak.push(0);
var compareTime = (new Date).getTime();
for (var j = 0; j < matches[i].length; j++) {
//check if previous match was greater than 3 hours ago
if (compareTime - matches[i][j].gameCreation > 10800000) {
break;
}
//find the participant
var participant = matches[i][j].myParticipant;
if (matches[i][j].participants[participant].stats.win == true) {
break;
}
compareTime = matches[i][j].gameCreation;
losingStreak[i]++;
}
}
//set up masteryPoints
for (var i = 0; i < summonersMastery.length; i++) {
masteryPoints.push(summonersMastery[i].championPoints);
//set mastery to 0 if no mastery is found
if (isNaN(masteryPoints[i])) { masteryPoints[i] = 0;}
}
//calculate wins, losses, and winRate ("not enough games" for summoners with low games)
for (var i = 0; i < summonersLeague.length; i++) {
if (summonersLeague[i][0]) {
wins.push(summonersLeague[i][0].wins);
losses.push(summonersLeague[i][0].losses);
} else {
wins.push(0);
losses.push(0);
}
if (wins[i] + losses[i] >= 30) {
winRate.push(wins[i]/(wins[i]+losses[i]));
} else {
winRate.push("not enough games");
}
}
//calculate timeSincePlayed
for (var i = 0; i < summonersMastery.length; i++) {
if (isNaN(summonersMastery[i].lastPlayTime)) {
timeSincePlayed.push("Never played");
} else {
timeSincePlayed.push(((new Date).getTime()-summonersMastery[i].lastPlayTime)/(1000*60*60*24));
}
//set timeSincePlayed to 10000 if no mastery is found
if (isNaN(timeSincePlayed[i])) { timeSincePlayed[i] = 10000;}
}
//calculate aggressiveness
for (var i = 0; i < matches.length; i++) {
var totalPlayerInteraction = 0; //total player kills + deaths + assists in game history
var totalAvgInteraction = 0; //total average kills + deaths + assists for champs played
for (var j = 0; j < matches[i].length; j++) {
var interaction = 0; //Kills + Deaths + Assists
var avgInteraction = 0;
//find the participant
var participant = matches[i][j].myParticipant;
//the player
interaction += matches[i][j].participants[participant].stats.kills;
interaction += matches[i][j].participants[participant].stats.deaths;
interaction += matches[i][j].participants[participant].stats.assists;
console.log(stats)
console.log(matches[i][j].participants[participant].championId)
console.log('derp')
if (stats[matches[i][j].participants[participant].championId]) {
//champion average
avgInteraction += stats[matches[i][j].participants[participant].championId].kills/stats[matches[i][j].participants[participant].championId].total;
avgInteraction += stats[matches[i][j].participants[participant].championId].deaths/stats[matches[i][j].participants[participant].championId].total;;
avgInteraction += stats[matches[i][j].participants[participant].championId].assists/stats[matches[i][j].participants[participant].championId].total;;
}
//add to total
totalPlayerInteraction += interaction;
totalAvgInteraction += avgInteraction;
}
function sigmoid(t) {
//the 10 is a constant that adjusts how sensitive the function is. The higher it is, the less sensitive it is
return 1/(1+Math.pow(Math.E, -t/10));
}
aggressiveness.push(sigmoid((totalPlayerInteraction-totalAvgInteraction)/matches[i].length));
}
//calculate warding
for (var i = 0; i < matches.length; i++) {
var totalPlayerWards = 0; //total player kills + deaths + assists in game history
var totalAvgWards = 0; //total average kills + deaths + assists for champs played
for (var j = 0; j < matches[i].length; j++) {
var wards = 0; //Kills + Deaths + Assists
var avgWards = 0;
//find the participant
var participant = matches[i][j].myParticipant;
//the player
if (matches[i][j].participants[participant].stats.wardsPlaced) {
wards += matches[i][j].participants[participant].stats.wardsPlaced;
} else {
continue;
}
if (stats[matches[i][j].participants[participant].championId]) {
//champion average
avgWards += stats[matches[i][j].participants[participant].championId].wardsPlaced/stats[matches[i][j].participants[participant].championId].total;
}
//add to total
totalPlayerWards += wards;
totalAvgWards += avgWards;
}
function sigmoid(t) {
//the 10 is a constant that adjusts how sensitive the function is. The higher it is, the less sensitive it is
return 1/(1+Math.pow(Math.E, -t/10));
}
warding.push(sigmoid((totalPlayerWards-totalAvgWards)/matches[i].length));
}
//calculate campScore
for (var i = 0; i < summonersUsername.length; i++) {
//set campScore from 50 to 100 based on losing streak run through sigmoid
campScore.push(100*sigmoid(losingStreak[i],1));
//multiply campScore by champSkillEvaluation
campScore[i] = campScore[i] * champSkillEvaluation(timeSincePlayed[i],masteryPoints[i]);
//multiply campScore by winRateMultiplier
if (!isNaN(winRate[i])) {
campScore[i] = campScore[i] * winRateMultiplier(winRate[i]);
} else {
//if no winrate data assume 50%
campScore[i] = campScore[i] * winRateMultiplier(0.5);
}
//set campScore to text if data couldn't be calculated
if (isNaN(campScore[i])) {
campScore[i] = "Not Enough Data";
}
//a function to get a multiplier from winrate
//uses winrate (x)
//function to insert into desmos.com y=1-\frac{0.5}{1+e^{-30\left(x-0.45\right)}}
function winRateMultiplier(x) {
return 1-0.5/(1+Math.pow(Math.E, -30*(x-0.45)));
}
//a function to estimate skill on a champion from 0.6 to 1
//uses time since last played (x) and champion mastery (z)
//function to insert into desmos.com y=1-.4e^{\frac{-x}{50}}\left(1-0.9e^{-\frac{z}{50000}}\right)
function champSkillEvaluation(x,z) {
return 1-0.4*Math.pow(Math.E, -x/50)*(1-0.9*Math.pow(Math.E, -z/50000));
}
function sigmoid(t,sensitivity) {
//the sensitivity adjusts how sensitive the function is. The higher it is, the less sensitive it is
return 1/(1+Math.pow(Math.E, -t/sensitivity));
}
}
console.log(campScore);
console.log(warding);
console.log(aggressiveness);
console.log(timeSincePlayed);
console.log(winRate);
console.log(masteryPoints);
for (var i = 0; i < summonersUsername.length; i++) {
console.log(summonersUsername[i] + ":\t" + champList.data[champIdToKey(currentGame.participants[i].championId)].name + ":\t" + campScore[i])
}
//run next async function
if (runList[index + 1]) {
runList[index + 1](runList, index + 1);
}
}
function champIdToKey(theId) {
var objKeys = Object.keys(champList.data);
for (var i = 0; i < objKeys.length; i++) {
if (champList.data[objKeys[i]].key == theId) {
return champList.data[objKeys[i]].id;
}
}
return null;
}
//Display everything
function loadDisplay(runList, index) {
function getRed(myNum) {
if (myNum < 0.5) {
return 255;
} else {
return Math.max(0, 2*(1-myNum)*255);
}
}
function getGreen(myNum) {
if (myNum > 0.5) {
return 255;
} else {
return Math.max(0, 2*myNum*255);
}
}
//y=1-\frac{1}{e^{0.7x}}
function losingStreakSigmoid(x) {
return 1-1/Math.pow(Math.E, 0.7*x);
}
//y=1-\frac{1}{1+e^{25\left(0.5-x\right)}}
function winRateSigmoid(x) {
return 1-1/(1+Math.pow(Math.E, 25*(0.5-x)));
}
//y=\frac{1}{e^{\frac{x}{100000}}}
function masterySigmoid(x) {
return 1/Math.pow(Math.E, x/100000);
}
//y=1-\frac{1}{e^{\frac{x}{25}^{.5}}}
function daysSincePlayedSigmoid(x) {
return 1-1/Math.pow(Math.E, Math.pow(x/25,0.5));
}
//y=\frac{1}{1+e^{10\left(0.5-x\right)}}
function aggrSigmoid(x) {
return 1/(1+Math.pow(Math.E, 10*(0.5-x)));
}
//y=1-\frac{1}{1+e^{10\left(0.5-x\right)}}
function wardSigmoid(x) {
return 1-1/(1+Math.pow(Math.E, 10*(0.5-x)));
}
//y=\frac{1}{1+e^{\frac{\left(30-x\right)}{10}}}
function campScoreSigmoid(x) {
return 1/(1+Math.pow(Math.E, (30-x)/10));
}
//make loader disappear
document.getElementById("loader").style.display = "none";
document.getElementById("teamName1").style.display = "block";
document.getElementById("teamName2").style.display = "block";
//set damage bars
//First team bar
var magicDmg = 0;
var physicalDmg = 0;
var trueDmg = 0;
for (var i = 0; i < summonersChampIds.length/2; i++) {
magicDmg += stats[summonersChampIds[i]].magicDamage;
physicalDmg += stats[summonersChampIds[i]].physicalDamage;
trueDmg += stats[summonersChampIds[i]].trueDamage;
}
var totalDmg = magicDmg + physicalDmg + trueDmg;
var temp = document.getElementsByTagName("template")[1].content.querySelector("div");
var a = document.importNode(temp, true);
a.querySelectorAll("div")[0].style.width = 100*magicDmg/totalDmg + "%";
a.querySelectorAll("div")[1].style.width = 100*physicalDmg/totalDmg + "%";
a.querySelectorAll("div")[2].style.width = 100*trueDmg/totalDmg + "%";
a.querySelectorAll("span")[1].textContent = "Magic: " + Math.round(1000*magicDmg/totalDmg)/10 + "%" + "\r\n" + "Physical: " + Math.round(1000*physicalDmg/totalDmg)/10 + "%" + "\r\n" + "True: " + Math.round(1000*trueDmg/totalDmg)/10 + "%";
document.getElementById("damageBar1").appendChild(a);
//Second team bar
var magicDmg = 0;
var physicalDmg = 0;
var trueDmg = 0;
for (var i = summonersChampIds.length/2; i < summonersChampIds.length; i++) {
if (stats[summonersChampIds[i]]) {
magicDmg += stats[summonersChampIds[i]].magicDamage;
physicalDmg += stats[summonersChampIds[i]].physicalDamage;
trueDmg += stats[summonersChampIds[i]].trueDamage;
}
}
var totalDmg = magicDmg + physicalDmg + trueDmg;
var temp = document.getElementsByTagName("template")[1].content.querySelector("div");
var a = document.importNode(temp, true);
a.querySelectorAll("div")[0].style.width = 100*magicDmg/totalDmg + "%";
a.querySelectorAll("div")[1].style.width = 100*physicalDmg/totalDmg + "%";
a.querySelectorAll("div")[2].style.width = 100*trueDmg/totalDmg + "%";
a.querySelectorAll("span")[1].textContent = "Magic: " + Math.round(1000*magicDmg/totalDmg)/10 + "%" + "\r\n" + "Physical: " + Math.round(1000*physicalDmg/totalDmg)/10 + "%" + "\r\n" + "True: " + Math.round(1000*trueDmg/totalDmg)/10 + "%";
document.getElementById("damageBar2").appendChild(a);
function loadChampDisplay(theElement, playerNum) {
var temp = document.getElementsByTagName("template")[0].content.querySelector("div");
var a = document.importNode(temp, true);
//picture
a.querySelectorAll("img")[0].src = "https://ddragon.leagueoflegends.com/cdn/" + apiVersion + "/img/champion/" + champIdToKey(summonersChampIds[playerNum]) + ".png";
//username
a.querySelectorAll("div")[0].textContent = summonersUsername[playerNum];
//make font smaller if username is long
//losingStreak
a.querySelectorAll("div")[2].textContent = losingStreak[playerNum];
a.querySelectorAll("div")[2].style.color = "rgb(" + getRed(losingStreakSigmoid(losingStreak[playerNum])) + "," + getGreen(losingStreakSigmoid(losingStreak[playerNum])) + ",0)";
a.querySelectorAll("div")[2].style.fontWeight = "300";
//winrate
a.querySelectorAll("div")[4].style.whiteSpace = "pre"
a.querySelectorAll("div")[4].textContent = Math.round(10000*winRate[playerNum])/100 + "% \r\n" + wins[playerNum] + "W/" + losses[playerNum] + "L";
a.querySelectorAll("div")[4].style.color = "rgb(" + getRed(winRateSigmoid(winRate[playerNum])) + "," + getGreen(winRateSigmoid(winRate[playerNum])) + ",0)";
a.querySelectorAll("div")[4].style.fontWeight = "300";
if (isNaN(winRate[playerNum])) {
a.querySelectorAll("div")[4].textContent = "Not Enough \r\n Games";
a.querySelectorAll("div")[4].style.color = "#b2b2b2";
}
//mastery
a.querySelectorAll("div")[6].textContent = masteryPoints[playerNum];
a.querySelectorAll("div")[6].style.color = "rgb(" + getRed(masterySigmoid(masteryPoints[playerNum])) + "," + getGreen(masterySigmoid(masteryPoints[playerNum])) + ",0)";
a.querySelectorAll("div")[6].style.fontWeight = "300";
//daysSincePlayed
a.querySelectorAll("div")[8].textContent = Math.round(timeSincePlayed[playerNum]) + " days ago";
a.querySelectorAll("div")[8].style.color = "rgb(" + getRed(daysSincePlayedSigmoid(timeSincePlayed[playerNum])) + "," + getGreen(daysSincePlayedSigmoid(timeSincePlayed[playerNum])) + ",0)";
a.querySelectorAll("div")[8].style.fontWeight = "300";
if (timeSincePlayed[playerNum] == 10000) {
a.querySelectorAll("div")[8].textContent = "Never Played";
}
//agr