-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtracker.js
More file actions
263 lines (216 loc) · 7.88 KB
/
tracker.js
File metadata and controls
263 lines (216 loc) · 7.88 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
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
const Athlete = require('./athlete')
const Cache = require('./cache')
const cache = new Cache()
function get(token, config) {
const key = config.key || `tracker-${token}`
return cache.get({
key,
keepAlive: true,
createFn: () => new Tracker(token, config)
})
}
class Tracker {
constructor(token, config) {
this._athlete = new Athlete(token, config)
this.lastPoint = null
this.lastTime = null
this.current = []
}
athlete() {
return this._athlete.athlete()
}
activities() {
return this._athlete.activities()
}
activity(activityId) {
return this._athlete.activity(activityId)
}
segments() {
return this._athlete.segments()
}
effort(segmentId) {
return this._athlete.effort(segmentId)
}
route(segmentId) {
return this._athlete.route(segmentId)
}
segmentWithEffort(segment) {
return this._athlete.effort(segment.id)
.then(effort => Object.assign({}, segment, { positions: effort }))
}
active(point) {
return this.segments().then(segments => {
if (!point) return []
const nearStartSegments = segments
.filter(s => this.isPointInRegion(point, s.start))
.filter(s => this.isNearStart(point, s))
// Pre-load segment effort
nearStartSegments.forEach(s => { this.effort(s.id) })
// Filter for where we're actually crossing the start line
const startingSegments = nearStartSegments
.filter(s => this.isCrossingStartPoint(point, s))
return Promise.all(startingSegments.map(s => this.setupSegmentPromise(point, s)))
.then(newSegments => {
const current = this.current.concat(newSegments)
current.forEach(s => this.setSegmentStatus(point, s))
this.current = current.filter(s => !this.shouldRemove(s))
this.lastPoint = point
this.lastTime = dateNow()
return this.current
.filter(s => s.lastIndex > 1)
.map(toJson)
})
})
}
isPointInRegion(p1, p2) {
return this.distance(p1, p2) < 10000000
}
isNearStart(point, segment) {
if (this.lastPoint && point) {
return this.isPointClose(point, segment.start)
}
return false
}
isCrossingStartPoint(point, segment) {
const crossing = this.getPointCrossing(point, segment.start)
return (crossing && this.distance(crossing.point, segment.start) < 2000)
}
getPointCrossing(point, endPoint) {
if (this.lastPoint && point) {
const lastToThis = this.distance(this.lastPoint, point)
const lastToStart = this.distance(this.lastPoint, endPoint)
const thisToStart = this.distance(point, endPoint)
if (lastToStart < lastToThis && thisToStart < lastToThis) {
const ratio = lastToStart / lastToThis
const midPoint = {
x: this.lastPoint.x + (point.x - this.lastPoint.x) * ratio,
y: this.lastPoint.y + (point.y - this.lastPoint.y) * ratio
}
return {
point: midPoint,
time: this.lastTime + (dateNow() - this.lastTime) * ratio
}
}
}
return null
}
setupSegmentPromise(point, segment) {
return this.segmentWithEffort(segment)
.then(s => {
if (s.positions && s.positions.length > 2) {
const crossing = this.getPointCrossing(point, s.start)
return Object.assign({}, s, {
inProgress: true,
startTime: crossing.time,
lastIndex: 0,
lastIndexTime: crossing.time
})
} else {
// No effort for this segment
return segment
}
})
}
setSegmentStatus(point, segment) {
this.setSegmentPRPosition(segment);
const points = segment.positions
if (segment.finished || !points || points.length < 2) return
let index = Math.min(segment.lastIndex + 1, points.length - 1)
const distFn = index => this.distance(point, points[index])
const nextNearest = index => {
const nearest = { index: index, dist: distFn(index) }
for(var n = index; n < index + 5; n++) {
if ( n < points.length) {
const dist = distFn(n)
if (dist < nearest.dist) {
nearest.index = n
nearest.dist = dist
}
}
}
return nearest.index
}
let next = nextNearest(index)
while(next > index) {
index = next
next = nextNearest(index)
}
if (distFn(index - 1) < this.distance(points[index - 1], points[index])) {
// Haven't passed this point yet
index--
}
if (index >= points.length - 2) {
const endCrossing = this.getPointCrossing(point, segment.end)
if (endCrossing) {
// Crossed the finish line
segment.inProgress = false
segment.finished = true
segment.finishTime = endCrossing.time
return
}
}
const crossing = this.getPointCrossing(point, points[index])
if (crossing) {
segment.lastIndex = index
segment.lastIndexTime = crossing.time
let timeToPoint = segment.lastIndexTime - segment.startTime
segment.difference = timeToPoint / 1000 - points[index].time
} else {
if (distFn(index) > 30000) {
segment.inProgress = false
}
}
}
setSegmentPRPosition(segment) {
const points = segment.positions
if (!points || points.length < 2 || !segment.startTime) return
segment.pr = {
time: points[points.length-1].time
};
const elapsed = dateNow() - segment.startTime;
let index = 0;
while(index < points.length && points[index].time * 1000 < elapsed) {
index++;
}
const previous = points[index - 1];
if (index < points.length) {
const next = points[index];
const pointTime = elapsed - previous.time * 1000;
const ratio = pointTime / ((next.time - previous.time) * 1000);
segment.pr.x = previous.x + (next.x - previous.x) * ratio;
segment.pr.y = previous.y + (next.y - previous.y) * ratio;
}
}
shouldRemove(segment) {
return !(segment.inProgress
|| (segment.finished && dateNow() - segment.finishTime < 5000))
}
isPointClose(p1, p2) {
return this.distance(p1, p2) < 10000
}
distance(p1, p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2))
}
}
function dateNow() {
return Date.now()
}
function toJson(s) {
return {
id: s.id,
name: s.name,
start: s.start,
end: s.end,
startTime: toISOTime(s.startTime),
endTime: toISOTime(s.endTime),
finished: s.finished,
difference: s.difference,
pr: s.pr
}
}
function toISOTime(time) {
return time ? (new Date(time)).toISOString() : undefined;
}
module.exports = {
get
}