-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathballer.js
123 lines (109 loc) · 4.33 KB
/
baller.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
var StatRow = {
initialize: function($row) {
this.row = $row;
return this;
},
calculateFD: function(perGame) {
var games = 1;
if (perGame) {
var games = this.getGames();
}
return ((this.getValue('PTS') + (1.5*this.getValue('AST')) + (2*this.getValue('STL')) + (2*this.getValue('BLK')) - this.getValue('TOV') + (1.2*this.getValue('TRB')))/games).toFixed(2);
},
calculateDK: function(perGame) {
var games = 1;
if (perGame) {
var games = this.getGames();
}
var stats = [this.getValue('PTS')/games, this.getValue('AST')/games, this.getValue('STL')/games, this.getValue('BLK')/games, this.getValue('TRB')/games];
var doubles = stats.map(this.checkDouble);
var doublesSum = doubles.reduce(function (a,b) { return a + b });
var dkBonus = this.checkBonus(doublesSum);
return ((this.getValue('PTS') + (1.5*this.getValue('AST')) + (2*this.getValue('STL')) + (2*this.getValue('BLK')) - (.5*this.getValue('TOV')) + (1.25*this.getValue('TRB')) + (.5*this.getValue('3P')) + dkBonus)/games).toFixed(2);
},
getGames: function() {
return this.getValue('G');
},
median: function (values) {
values.sort( function(a,b) { return a - b; });
var half = Math.floor(values.length/2);
if(values.length % 2)
return values[half];
else
return (Number(values[half - 1]) + Number(values[half]))/2.0;
},
checkDouble: function (stat) {
if ( stat >= 10 ) {
return 1;
} else {
return 0;
}
},
checkBonus: function (x) {
if ( x == 2 ) {
return 1.5;
}
if (x >= 3 ) {
return 3;
}
else {
return 0;
}
},
getIndex: function(category) {
var selector = "th:contains(" + category + ")";
var $table = this.row.parent().parent();
var idx = $table.find('thead tr:not(.over_header)').first().find(selector).index();
return idx;
},
getValue: function(category) {
return Number(this.row.find('td').eq(this.getIndex(category)).text());
}
};
$(document).ready(function() {
$('#per_minute thead tr, #per_game thead tr, #pgl_basic thead tr, #stats thead tr').append('<th>FD</th><th>DK</th>');
$('#per_minute tbody tr, #per_minute tfoot tr, #per_game tbody tr, #per_game tfoot tr').each(function(index){
var $row = $(this);
var statRow = Object.create(StatRow).initialize($row);
var fd = statRow.calculateFD();
var dk = statRow.calculateDK();
$(this).append("<td>" + fd + "</td>" + "<td>" + dk + "</td>");
});
var tableHeading = $('.table_heading h2').text();
$('#stats tbody tr').each(function(index){
var $row = $(this);
var perGame = true;
var statRow = Object.create(StatRow).initialize($row);
var fd = statRow.calculateFD(perGame);
var dk = statRow.calculateDK(perGame);
$(this).append("<td>" + fd + "</td>" + "<td>" + dk + "</td>");
});
var fd_vals = [];
var dk_vals = [];
$("#pgl_basic tbody tr").not(".thead").each(function(index){
var $row = $(this);
var statRow = Object.create(StatRow).initialize($row);
var fd = statRow.calculateFD();
var dk = statRow.calculateDK();
fd_vals.push(fd);
dk_vals.push(dk);
$(this).append("<td>" + fd + "</td>" + "<td>" + dk + "</td>");
});
$("#pgl_basic tbody").append("<tr><td><strong>FD</strong></td><td> Median</td><td>" + StatRow.median(fd_vals) + "</td><td></td><td>Min</td><td>" + Math.min.apply(Math,fd_vals) + "</td><td></td><td>Max</td><td>" + Math.max.apply(Math,fd_vals) + "</td></tr>");
$("#pgl_basic tbody").append("<tr><td><strong>DK</strong></td><td> Median</td><td>" + StatRow.median(dk_vals) + "</td><td></td><td>Min</td><td>" + Math.min.apply(Math,dk_vals) + "</td><td></td><td>Max</td><td>" + Math.max.apply(Math,dk_vals) + "</td></tr>");
var url = document.URL;
if ( url.split('/')[3] == "boxscores" ) {
$('table').each(function() {
if ( $(this).attr('id') != undefined && $(this).attr('id').split('_')[1] == "basic" ) {
$(this).find('thead tr').not('.over_header').append('<th>FD</th><th>DS</th>');
$(this).find('tbody tr, tfoot tr').not('.thead').each(function(index){
var $row = $(this);
var statRow = Object.create(StatRow).initialize($row);
var fd = statRow.calculateFD();
var dk = statRow.calculateDK();
$(this).append("<td>" + fd + "</td>" + "<td>" + dk + "</td>");
});
}
});
}
});