forked from TennisVisuals/mcp-charting-points-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.js
More file actions
145 lines (132 loc) · 4.61 KB
/
Copy pathanalysis.js
File metadata and controls
145 lines (132 loc) · 4.61 KB
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
!function() {
// module container
var analyze = {};
// Analysis
analyze.serveAnalysis = serveAnalysis;
function serveAnalysis(points, verbose) {
var serve_types = {};
var invalid_serves = 0;
var multiple_serves = 0;
for (var p=0; p < points.length; p++) {
var point = points[p];
if (point.serves.length > 1) multiple_serves += 1;
if (point.first_serve && point.first_serve.serves && point.first_serve.serves.length > 1) multiple_serves += 1;
if (point.serves.length == 0) invalid_serves += 1;
if (point.first_serve && point.first_serve.serves && point.first_serve.serves.length == 0) invalid_serves += 1;
if (point.serves.length == 1) {
if (!serve_types[point.serves[0]]) {
serve_types[point.serves[0]] = 1;
} else {
serve_types[point.serves[0]] += 1;
}
}
}
var analysis = {
serve_types: serve_types,
invalid_serves: invalid_serves,
multiple_serves: multiple_serves
};
return analysis
}
analyze.matchesServeAnalysis = matchesServeAnalysis;
function matchesServeAnalysis(matches, verbose) {
var serve_types = {};
var invalid_serves = 0;
var multiple_serves = 0;
matches.forEach(function(match, i) {
var analysis = serveAnalysis(match.match.points(), verbose);
invalid_serves += analysis.invalid_serves;
multiple_serves += analysis.multiple_serves;
var serve_keys = Object.keys(analysis.serve_types);
serve_keys.forEach(function(s) {
if (!serve_types[s]) {
serve_types[s] = analysis.serve_types[s];
} else {
serve_types[s] += analysis.serve_types[s];
}
});
});
var matches_analysis = {
serve_types: serve_types,
invalid_serves: invalid_serves,
multiple_serves: multiple_serves
};
return matches_analysis
}
analyze.rallyDepth = rallyDepth;
function rallyDepth(points, verbose) {
var no_depth_or_finish = [];
var rally_count = 0;
var return_count = 0;
var return_depth = 0;
var return_finish = 0;
var depth_count = 0;
for (var p=0; p < points.length; p++) {
if (points[p].rally.length) {
return_count += 1;
var shots = points[p].rally;
var ros = shots[0];
var depth = findDepth(ros);
var finish = findTerminator(ros);
if (depth) {
return_depth += 1;
}
if (finish) {
return_finish += 1;
}
if (!depth && !finish) {
no_depth_or_finish.push(ros);
}
if (shots.length > 1) {
for (var r=1; r < shots.length; r++) {
rally_count += 1;
var rally_depth = findDepth(shots[r]);
if (rally_depth) {
depth_count += 1;
if (verbose) console.log(points[p].serves, shots);
}
}
}
}
}
function findDepth(shot) {
for (var i=0; i < shot.length; i++) {
if ('789'.split('').indexOf(shot[i]) >= 0) return true;
}
return false;
}
function findTerminator(shot) {
for (var i=0; i < shot.length; i++) {
if ('*@#'.split('').indexOf(shot[i]) >= 0) return true;
}
return false;
}
var analysis = {
points: points.length,
returns: return_count,
return_finish: return_finish,
return_depth: return_depth,
return_other: no_depth_or_finish,
shots: rally_count,
rally_depth: depth_count
}
return analysis;
}
analyze.matchesWithRallyDepth = matchesWithRallyDepth;
function matchesWithRallyDepth(matches, verbose) {
var rally_shots = 0;
var rally_depth = 0;
var matches_with_rally_depth = [];
matches.forEach(function(match, i) {
var analysis = rallyDepth(match.match.points(), verbose);
rally_shots += analysis.shots;
if (analysis.rally_depth > 0) {
matches_with_rally_depth.push(i);
rally_depth += analysis.rally_depth;
}
});
return { rally_shots: rally_shots, rally_depth: rally_depth, matches: matches_with_rally_depth };
}
if (typeof define === "function" && define.amd) define(analyze); else if (typeof module === "object" && module.exports) module.exports = analyze;
this.analyze = analyze;
}();