-
Notifications
You must be signed in to change notification settings - Fork 0
/
wot_stats_functions.js
2246 lines (1815 loc) · 68.2 KB
/
wot_stats_functions.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
/*
* Description: Functions defined as plugins for the site, Say Wot? - stats (http://saywotstats.net)
* VersionInfo: Using WOT API v2.X
* Author: Johan "DaWoody" Wedfelt
* AuthorUrl https://github.com/DaWoody
* Feedback/Dev: http://saywotstats.blogspot.se
* License: GNU General Public License, version 3(GPL-3.0) (http://opensource.org/licenses/GPL-3.0)
*
*
*/
jQuery(document).ready(function(){
/*
* Show Tanker Name
*/
$.fn.playerName = function(response, tankerId){
var container = $(this);
var playerName = response.nickname;
container.append('<h1>Tanker: ' + playerName + '</h1>');
}
/*
* Show Player Server
*/
$.fn.printServer = function(server) {
var container = $(this);
container.append('<h1>Server: ' + server + '</h1>');
}
/*
* Prints the headline to the total stats block
*/
$.fn.printTotalStatsHeader = function() {
var container = $(this);
var headline = '<div class="recent_total_stats_headline_div"><h1>Total</h1></div>';
container.prepend(headline);
}
/*
* Prints the headline to the recent stats block
*/
$.fn.printRecentStatsHeader = function() {
var container = $(this);
var headline = '<div class="recent_total_stats_headline_div"><h1>24 Hours</h1></div>';
container.prepend(headline);
}
/*
* Prints the headline to the older stats block
*/
$.fn.printOlderStatsHeader = function() {
var container = $(this);
var headline = '<div class="recent_total_stats_headline_div"><h1>1 Week</h1></div>';
container.prepend(headline);
}
/*
* Calculate Total Time Played WOT
*/
$.fn.calculateTotalTimePlayed = function(response) {
//Define the local version of what we get in.
var container = $(this);
//The name of the player
var tankerName = response.name;
//Amount of battles played
var battles = response.summary.battles_count;
//Based on an average battle time around 5 minutes..
var totalTimeInMinutes = battles*5;
//Getting the time in days..
var timeInDays = totalTimeInMinutes/(60*24);
//Rounding down the days..
var timeInDaysRounded = Math.floor(timeInDays);
//Getting the rest of the time in hours..
var timeInHours = (timeInDays-timeInDaysRounded)*24;
//Rounding down the hours...
var timeInHoursRounded = Math.floor(timeInHours);
var timeInMinutes = Math.round((timeInHours-timeInHoursRounded)*60);
//console.log('Has devoted days:' + timeInDaysRounded + ', hours:' + timeInHoursRounded + ', and ' + timeInMinutes + 'minutes of his life playing WOT ;)');
var timeContainer = $('<h1></h1>');
timeContainer.append('Based on an average of about 5 minutes per game...<br>')
timeContainer.append(tankerName+ ' has devoted ' + timeInDaysRounded + ' days.. '+ timeInHoursRounded + ' hours.. and ' + timeInMinutes + ' minutes of his life playing WOT ;)');
//Now we write our results to the DOM
container.append(timeContainer);
}
/*
* Calculates and shows the average winratio
*/
$.fn.averageWinRate = function(response) {
//We define what we get in
var container = $(this);
//We define what we send to the color code
var statName = 'averageWinRate';
//Get total battles
var totalBattlesPlayed = response.statistics.all.battles;
//Get total wins, current
var totalWins = response.statistics.all.wins;
//The average winrate
var averageWinRate = Math.round((totalWins/totalBattlesPlayed)*1000)/10;
//Lets get the color code for this specific stat
var color = container.printColorToStat(statName,averageWinRate);
//Write it back to the DOM
container.append('<h1>Average winrate: <span style="color:' + color + '">'+ averageWinRate + '%</span></h1>');
}
/*
* Calculates and shows the creation time of the account based on a Unix time stamp
*/
$.fn.getAccountCreationTime = function(response){
//We define what we get in
var container = $(this);
//Defining some variables
var timeCreatedUnix = response.created_at;
// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(timeCreatedUnix*1000);
//Get the year in four digits
var year = date.getFullYear();
//Get the month 1-12
var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
var theMonth = month[date.getMonth()];
var theDate = date.getDate();
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = year + ' - ' + theMonth + ' - ' + theDate + ' - ' + hours + ':' + minutes + ':' + seconds;
container.append('<h1>Account created: ' + formattedTime + '</h1>');
}
/*
* Show average experience
*/
$.fn.averageExperience = function(response) {
//We define what we get in
var container = $(this);
//The average experience
var averageExperience = response.statistics.all.battle_avg_xp;
//Write it back to the DOM
container.append('<h1>Average experience: ' + averageExperience + '</h1>');
}
/*
* Show average damage done
*/
$.fn.averageDamage = function(response) {
//We define what we get in
var container = $(this);
//Total damage
var totalDamage = response.statistics.all.damage_dealt;
//Total amount of battles
var battles = response.statistics.all.battles;
var averageDamage = Math.round(totalDamage/battles);
//Write it back to the DOM
container.append('<h1>Average damage: ' + averageDamage + '</h1>');
}
/*
* Show when the stats was last updated
*/
$.fn.lastUpdated = function(response){
//We define what we get in
var container = $(this);
//Defining some variables
var lastUpdatedUnix = response.updated_at;
// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(lastUpdatedUnix*1000);
//Get the year in four digits
var year = date.getFullYear();
//Get the month 1-12
var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
var theMonth = month[date.getMonth()];
var theDate = date.getDate();
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = year + ' - ' + theMonth + ' - ' + theDate + ' - ' + hours + ':' + minutes + ':' + seconds;
container.append('<h1>Updated: ' + formattedTime + '</h1>');
}
/*
* Show total battles played
*/
$.fn.totalBattlesPlayed = function(response) {
var statName = 'totalBattlesPlayed';
//We define what we get in
var container = $(this);
//Defining some variables
var totalBattlesPlayed = response.statistics.all.battles;
//Lets get the color code for this specific stat
var color = container.printColorToStat(statName,totalBattlesPlayed);
container.append('<h1>Battles played: <span style="color:' + color + '">'+ totalBattlesPlayed + '</span></h1>');
}
/*
* Show hit percentage
*/
$.fn.hitPercentage = function(response) {
//We define what we get in
var container = $(this);
//Defining some variables
var hitPercentage = response.battles.hits_percents;
container.append('<h1>Hit percentage: ' + hitPercentage + '%</h1>');
}
/*
* Show Clan Name and Image
*/
$.fn.clan = function(response, serverAbbreviation){
var container = $(this),
clan=null;
try{
clan = response.clan_id;
}
catch(error){
console.log(error);
}
if(clan!==null){
//http://clans.worldoftanks.eu/media/clans/emblems/cl_562/500020562/emblem_64x64.png
var clanId = clan.toString(),
clanIdLength = clanId.length,
firstPartOfStringInteger = parseInt(clanIdLength, 10) - 3,
secondPartOfString = clanId.substring(firstPartOfStringInteger);
var clanUrl = 'http://clans.worldoftanks' + serverAbbreviation + '/media/clans/emblems/cl_' + secondPartOfString + '/' + clanId + '/emblem_32x32.png';
container.append('<h1>Clan: <img class ="clan_image" src="' + clanUrl + '" /></h1>');
}
else{
container.append('<h1>Clan: Has no clan</h1>');
}
}
$.fn.allPlayerTanks = function(tanks, tankDataArray){
var tanksArray = tankDataArray.data;
var playerTanks = [];
for(var i=0, tanksArrayLength = tanks.length; i<tanksArrayLength; i++){
for(key in tanksArray) {
if(key == tanks[i].tank_id){
var modifiedTankNameArray = (tanksArray[key].name).split(':'),
modifiedTankName = (modifiedTankNameArray[1]).toLowerCase();
var imgUrl = 'http://worldoftanks.eu/static/3.21.0.4/encyclopedia/tankopedia/vehicle/' + tanksArray[key].nation + '-' + modifiedTankName + '.png';
var newTank = {
'tank_id': tanksArray[key].tank_id,
'level': tanksArray[key].level,
'name': tanksArray[key].name,
'nation': tanksArray[key].nation,
'short_name': tanksArray[key].name_i18n,
'battles': tanks[i].statistics.battles,
'wins': tanks[i].statistics.wins,
'mark_of_mastery': tanks[i].mark_of_mastery,
'image_url': imgUrl
};
playerTanks.push(newTank);
}
}
}
return playerTanks;
}
/*
* Show average damage during the past period
*/
$.fn.averageDamagePast = function(response1, response2) {
var container = $(this),
damage24HoursAgo,
totalDamage,
damageLast24Hours,
battlesLast24Hours,
battles24HoursAgo,
totalAmountOfBattles,
averageDamageLast24Hours;
//Total damage 24 hours ago.
try {
damage24HoursAgo = response2.stat.statistics.all.damage_dealt;
}
catch(error){
damage24HoursAgo = 0;
}
//Total amount of battles 24 hours ago
try {
battles24HoursAgo = response2.stat.statistics.all.battles;
}
catch(error){
battles24HoursAgo = 0;
}
//Total damage, according to last update
totalDamage = response1.statistics.all.damage_dealt;
//The accumulated damage the last 24 hours
damageLast24Hours = totalDamage - damage24HoursAgo;
//Total amount of battles, to last update.
totalAmountOfBattles = response1.statistics.all.battles;
//The amount of battles the last 24 hours
battlesLast24Hours = totalAmountOfBattles - battles24HoursAgo;
//Calculate the average damage done last 24 hours.
averageDamageLast24Hours = Math.round(damageLast24Hours/battlesLast24Hours);
container.append('<h1>Average damage: ' + averageDamageLast24Hours + '</h1>');
}
/*
* Show amount of battles played the past 24 hours
*/
$.fn.battlesPlayedPast = function(response1, response2) {
var container = $(this),
battles24HoursAgo,
totalAmountOfBattles,
battlesLast24Hours,
noResponse ="";
//Total amount of battles, to last update.
totalAmountOfBattles = response1.statistics.all.battles;
//Total amount of battles 24 hours ago
try {
battles24HoursAgo = response2.stat.statistics.all.battles;
}
catch(error){
battles24HoursAgo = totalAmountOfBattles;
noResponse = "(No response with data recieved from API, blame it on the boggie ;))";
//console.log("The error from battlesPlayedPast func is:" + error);
}
//The amount of battles the last 24 hours
battlesLast24Hours = totalAmountOfBattles - battles24HoursAgo;
container.append('<h1>Battles played: ' + battlesLast24Hours + '</h1><h4 style="color:#f54747">' + noResponse + '</h4>');
}
/*
* Show the average experience gained per game the past period
*/
$.fn.averageExperiencePast = function(response1, response2) {
//Define what we get in
var container = $(this),
battles24HoursAgo,
totalXpGainedLast24,
totalAmountOfBattles,
battlesLast24Hours,
totalXpGained,
xpGainedPast24,
averageXpGainedPast24;
//First we will get the amount of battles for the past 24 hours
//Total amount of battles, to last update.
totalAmountOfBattles = response1.statistics.all.battles;
//Total amount of battles 24 hours ago
try{
battles24HoursAgo = response2.stat.statistics.all.battles;
}
catch(error){
battles24HoursAgo = totalAmountOfBattles;
//console.log("No battles recorded the 24 last hours... TT. From averageExperiencePast func");
}
//The amount of battles the last 24 hours
battlesLast24Hours = totalAmountOfBattles - battles24HoursAgo;
//Now we will get the total xp gain and subtract how it looked 24 hours ago
//Get the total XP so far
totalXpGained = response1.statistics.all.xp;
//Get the total XP from 24 hours back
try{
totalXpGainedLast24 = response2.stat.statistics.all.xp;
}
catch(error){
totalXpGainedLast24 = totalXpGained;
}
//Subtract the difference to get the xp gained the past 24 hours
xpGainedPast24 = totalXpGained - totalXpGainedLast24;
//Now we calculate the average xp gained per fight the last 24 hours
averageXpGainedPast24 = Math.round(xpGainedPast24/battlesLast24Hours);
//Print it to the DOM
container.append('<h1>Average experience: ' + averageXpGainedPast24 + '</h1>');
}
/*
* Shows average winrate from the past 24 hours
*/
$.fn.averageWinRatePast = function(response1, response2) {
//Define what we get in
var container = $(this),
battles24HoursAgo,
totalWinsLast24,
statName,
totalAmountOfBattles,
battlesLast24Hours,
totalWins,
winsPast24,
winRatioPast24,
color;
//We define what we send to the color code
statName = 'averageWinRate';
//What to do.
//Get amount of battles and amount of wins, divide to get percentage
//First we will get the amount of battles for the past 24 hours
//Total amount of battles, to last update.
totalAmountOfBattles = response1.statistics.all.battles;
//Total amount of battles 24 hours ago
try {
battles24HoursAgo = response2.stat.statistics.all.battles;
}
catch(error){
battles24HoursAgo = totalAmountOfBattles;
//console.log("No battles recorded last 24 hours...");
}
//The amount of battles the last 24 hours
battlesLast24Hours = totalAmountOfBattles - battles24HoursAgo;
//Get amount of wins for the past 24 hours
//Get total wins, current
totalWins = response1.statistics.all.wins;
//Get total wins, 24 hours ago
try {
totalWinsLast24 = response2.stat.statistics.all.wins;
}
catch(error){
totalWinsLast24 = totalWins;
}
//Subtract to get amount of wins for the past 24 hours
winsPast24 = totalWins - totalWinsLast24;
//Divide to get the win percentage from the past 24 hours
winRatioPast24 = (Math.round(((winsPast24/battlesLast24Hours)*1000)))/10;
//Lets get the color code for this specific stat
color = container.printColorToStat(statName,winRatioPast24);
//Print to DOM
container.append('<h1>Average winrate: <span style="color:' + color + '">'+ winRatioPast24 + '%</span></h1>');
}
/*
* Calculates average capture points over all battles
*/
$.fn.averageCapPoints = function(response) {
var container = $(this),
totalCapPoints,
totalAmountOfBattles,
averageCapPoints;
totalCapPoints = response.statistics.all.capture_points;
//Total amount of battles, to last update.
totalAmountOfBattles = response.statistics.all.battles;
if(totalAmountOfBattles === 0){
averageCapPoints = 0;
}
else {
averageCapPoints = Math.round((totalCapPoints/totalAmountOfBattles)*100)/100;
}
container.append('<h1>Average capture points: ' + averageCapPoints + '</h1>');
}
/*
* Calculates average capture points over the last period
*/
$.fn.averageCapPointsPast = function(response1, response2) {
var container = $(this),
totalCapPointsLast24,
totalAmountOfBattles24Last,
capPointsPast24,
totalAmountOfBattles,
battlesPast24,
averageCapPoints,
totalCapPoints;
//Getting the total cap points
totalCapPoints = response1.statistics.all.capture_points;
//Getting total cap points from 24 hours ago
try {
totalCapPointsLast24 = response2.stat.statistics.all.capture_points;
}
catch(error){
totalCapPointsLast24 = totalCapPoints;
}
capPointsPast24 = totalCapPoints - totalCapPointsLast24;
//Total amount of battles, to last update.
totalAmountOfBattles = response1.statistics.all.battles;
//Total amount of battles, 24 hours ago.
try {
totalAmountOfBattles24Last = response2.stat.statistics.all.battles;
}
catch(error){
totalAmountOfBattles24Last = totalAmountOfBattles;
}
//Calculate the battles the past 24 hours
battlesPast24 = totalAmountOfBattles - totalAmountOfBattles24Last;
if(battlesPast24 === 0){
averageCapPoints = 0;
}
else {
averageCapPoints = Math.round((capPointsPast24/battlesPast24)*100)/100;
}
container.append('<h1>Average capture points: ' + averageCapPoints + '</h1>');
}
/*
* Calculate average defense points
*/
$.fn.averageDefPoints = function(response) {
var container = $(this);
//Get the total number of def points
var totalDefPoints = response.statistics.all.dropped_capture_points;
//Total amount of battles, to last update.
var totalAmountOfBattles = response.statistics.all.battles;
if(totalAmountOfBattles === 0){
var averageDefPoints = 0;
}
else {
var averageDefPoints = Math.round((totalDefPoints/totalAmountOfBattles)*100)/100;
}
container.append('<h1>Average defence points: ' + averageDefPoints + '</h1>');
}
/*
* Calculate average defense points the last period
*/
$.fn.averageDefPointsPast = function(response1, response2) {
var container = $(this),
totalDefPoints,
totalDefPoints24Last,
defPointsPast24,
totalAmountOfBattles,
totalAmountOfBattlesLast24,
battlesPast24,
averageDefPoints;
//Get the total number of def points
totalDefPoints = response1.statistics.all.dropped_capture_points;
//Get the total number of def points 24 hours ago
try{
totalDefPoints24Last = response2.stat.statistics.all.dropped_capture_points;
}
catch(error){
totalDefPoints24Last = totalDefPoints;
}
//Calculate the difference
defPointsPast24 = totalDefPoints - totalDefPoints24Last;
//Total amount of battles, to last update
totalAmountOfBattles = response1.statistics.all.battles;
//Total amount of battles, 24 hours ago
try {
totalAmountOfBattlesLast24 = response2.stat.statistics.all.battles;
}
catch(error){
totalAmountOfBattlesLast24 = totalAmountOfBattles;
}
//Calculate the difference
battlesPast24 = totalAmountOfBattles - totalAmountOfBattlesLast24;
if(battlesPast24 === 0){
averageDefPoints = 0;
}
else {
averageDefPoints = Math.round((defPointsPast24/battlesPast24)*100)/100;
}
container.append('<h1>Average defence points: ' + averageDefPoints + '</h1>');
}
/*
* Shows the average tier played in total
*/
$.fn.averageTier = function(response1, response4, playerTanks){
//Defining our container
var container = $(this),
countMatchesIteration,
countMatchesLevelIteration,
tanksArray,
tanksArrayLength,
tankDataArrayLength,
averageTier,
totalAmountOfBattles,
newTankArray = [];
//Defining some variables we need when we iterate through the array
countMatchesIteration = 0;
countMatchesLevelIteration = 0;
totalAmountOfBattles = response1.statistics.all.battles;
var theTankArray = playerTanks;//window.newTankDataArray,
battlesMultipliedWithTier = 0;
for(var i=0, x=theTankArray.length; i<x; i++){
battlesMultipliedWithTier += (theTankArray[i].battles * theTankArray[i].level);
}
//Do our average tier calculation
averageTier = Math.round((battlesMultipliedWithTier/totalAmountOfBattles)*100)/100;
//Print it to the DOM
container.append('<h1>Average tier: ' + averageTier + '</h1>');
}
/*
* Shows the average tier over the last period, decided by the input
*/
$.fn.averageTierPast = function(vehicleTotalObject, response2, tankDataArray){
//Use our global variable tanksDataArray to verify against
//Response2 is our most recent data where we make comparison against
//Build a new array on the fly with most recent tankdata
//Define our first vehicles array
var container = $(this),
tanksArray1 = vehicleTotalObject,
tanksArray2,
theTankDataArray = tankDataArray.data,
tanksArrayJoinedLength,
totalAmountOfBattles=0,
totalAmountOfLevelsWeighted=0;
try{
if(response2.stat!==null){
tanksArray2 = response2.stat.vehicles
}
else {
throw new Error("stat.vehicles doesn't exist currently, cause of backend trouble from the API 2.0, blame it on the russian backend devs ;)");
tanksArray2 = [];
}
}
catch(error){
console.log(error + "Object that got returned just below in console");
console.log(response2);
tanksArray2 = [];
}
//This is the new array with elements from total stats, that also resides in recent stats
var tanksArrayJoined = [];
//This is the array that will hold new vehicle data, only available through total stats..
var tanksArrayNew = [];
//Lets start splitting the different length arrays in two, on with the elements that match and one with the newly added tanks
//Here we will put our data that coincides over both recent and total stats, in a new array
for(var j=0, y=tanksArray2.length; j<y; j++){
var tankId = tanksArray2[j].tank_id,
tankLevel =0,
tankName,
tanksArrayJoinedLength;
for(tank in theTankDataArray){
if(theTankDataArray[tank].tank_id === tankId){
tankLevel = theTankDataArray[tank].level;
tankName = theTankDataArray[tank].name;
}
}
//Get level of tank as a value from the tankDataArray
//Push the tank to a new array which we will work with
//Here we will modify the game value etc if it exist
tanksArrayJoined.push({
'tankId': tanksArray2[j].tank_id,
'battles': tanksArray2[j].battles,
'wins':tanksArray2[j].wins,
'markOfMastery': tanksArray2[j].mark_of_mastery,
'level':tankLevel,
'name': tankName
});
}
tanksArrayJoinedLength = tanksArrayJoined.length;
//Now lets modify the values in our newly created array by, subtracting the values we have from the old values
for(var i=0, x=tanksArray1.length; i<x; i++){
var oldTankId = tanksArray1[i].tank_id,
oldBattles=tanksArray1[i].statistics.battles,
newBattles;
for(var j=0; j<tanksArrayJoinedLength; j++){
if(tanksArrayJoined[j].tankId === oldTankId){
newBattles = oldBattles - tanksArrayJoined[j].battles;
tanksArrayJoined[j].battles = newBattles;
}
}
}
//Now lets do the actual calculation of the average Tier
for(var i=0; i<tanksArrayJoinedLength; i++){
totalAmountOfLevelsWeighted += Number(tanksArrayJoined[i].battles) * Number(tanksArrayJoined[i].level);
totalAmountOfBattles += Number(tanksArrayJoined[i].battles);
}
//Resulting value is here, for average tier
var averageTierPast24 = Math.round((totalAmountOfLevelsWeighted/totalAmountOfBattles)*100)/100;
//Print it to the DOM
container.append('<h1>Average tier: ' + averageTierPast24 + '</h1>');
}
/*
* This will show us the name and the image of the most played vehicle in total
*/
$.fn.favoriteVehicleTotal = function(response, playerTanks) {
//Define what we get in
var container = $(this),
//Define our vehicles array
tanksArray = playerTanks,
mostBattles =0,
tankId,
imgUrl,
vehicleName,
vehicleImgObject;
for(var i=0, x=tanksArray.length; i<x; i++){
if(tanksArray[i].battles>mostBattles){
mostBattles = tanksArray[i].battles;
tankId = tanksArray[i].tank_id;
imgUrl = tanksArray[i].image_url;
vehicleName = tanksArray[i].short_name;
//console.log(imgUrl);
}
}
//Define our battles variable, this will increase as we iterate through
vehicleImgObject = '<img class="vehicle_image" src="' + imgUrl + '">';
//Print it to the DOM
container.append('<h1>Most played: ' + vehicleName + vehicleImgObject);
}
/*
* Shows us the name and image of the most played vehicle from the past 24 hours
*/
$.fn.favoriteVehiclePast = function(response2, playerTanks) {
var container = $(this),
tanksArrayNew = [],
recentTankArray = [],
mostPlayedBattles = 0,
mostPlayedTankId = 0,
imgUrl='',
vehicleImgObject = '<img class="vehicle_image" src="">',
vehicleName='No tank played recently',
recentBattlesTotal,
totalBattles,
recentBattles;
try{
recentTankArray = response2.stat.vehicles;
}
catch(error){
console.log(error);
}
for(var i=0, x=recentTankArray.length; i<x; i++){
for(var j=0, y=playerTanks.length; j<y; j++){
if(recentTankArray[i].tank_id===playerTanks[j].tank_id){
recentBattlesTotal = recentTankArray[i].battles;
totalBattles = playerTanks[j].battles;
recentBattles = totalBattles - recentBattlesTotal;
if(recentBattles>mostPlayedBattles){
mostPlayedBattles = recentBattles;
mostPlayedTankId = playerTanks[j].tank_id;
}
}
}
}
//Nu leta upp bilden på tanken och lägg till urlen i imgUrl
for(var j=0, y=playerTanks.length; j<y; j++){
if(mostPlayedTankId === playerTanks[j].tank_id){
vehicleName = playerTanks[j].short_name;
imgUrl = playerTanks[j].image_url;
}
}
//Define our battles variable, this will increase as we iterate through
vehicleImgObject = '<img class="vehicle_image" src="' + imgUrl + '">';
//Print it to the DOM
container.append('<h1>Most played: ' + vehicleName + vehicleImgObject);
}
/*
* Average Spotted per game total
*/
$.fn.averageSpotted = function(response){
var container = $(this);
//Get the total amount of spots
var totalSpotted = response.statistics.all.spotted;
//Fetching the amount of battles
var totalBattlesPlayed = response.statistics.all.battles;
//Get the average spot, by dividing the spots with battles, and the rounding it for display
var averageSpotted = Math.round((totalSpotted/totalBattlesPlayed)*100)/100;
//Print to DOM
container.append('<h1>Average spotted: ' + averageSpotted + '</h1>');
}
/*
* Average Spotted per game the last period
*/
$.fn.averageSpottedPast = function(response1, response2){
var container = $(this),
totalSpotted,
totalBattlesPlayed,
totalSpottedLast24,
battlesLast24Hours,
battlesPast24,
spottedPast24,
averageSpotted;
//Get the total amount of spots
totalSpotted = response1.statistics.all.spotted;
//Fetching the amount of battles
totalBattlesPlayed = response1.statistics.all.battles;
//Lets get the total spotted as it were 24 hours ago
try {
totalSpottedLast24 = response2.stat.statistics.all.spotted;
}
catch(error){
totalSpottedLast24 = totalSpotted;
}
//Total amount of battles 24 hours ago
try{
battlesLast24Hours = response2.stat.statistics.all.battles;
}
catch(error){
battlesLast24Hours = totalBattlesPlayed;
}
//The amount of battles the last 24 hours
battlesPast24 = totalBattlesPlayed - battlesLast24Hours;
//The amount of spots the last 24 hours
spottedPast24 = totalSpotted - totalSpottedLast24;
//Get the average spot, by dividing the spots with battles
averageSpotted = Math.round((spottedPast24/battlesPast24)*100)/100;
container.append('<h1>Average spotted: ' + averageSpotted + '</h1>');
}
/*
* Average Frags Total
*/
$.fn.averageFrags = function(response){
var container = $(this),
totalBattlesPlayed,
totalFrags,
averageFrags;
//Fetching the amount of battles
totalBattlesPlayed = response.statistics.all.battles;
//Fetching the amount of total frags
totalFrags = response.statistics.all.frags;
//Calculating average frags
averageFrags = Math.round((totalFrags/totalBattlesPlayed)*100)/100;
container.append('<h1>Average kills: ' + averageFrags + '</h1>')
}
/*
* Average Frags Past
*/