forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.js
More file actions
463 lines (398 loc) · 12.9 KB
/
location.js
File metadata and controls
463 lines (398 loc) · 12.9 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
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
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as spherical from './spherical.js';
import * as sort from './sort.js';
const PRESENTS_OVER_WATER = .3;
const PRESENTS_IN_CITY = 1 - PRESENTS_OVER_WATER;
/**
* SantaLocation represents a single Santa destination (e.g. Sydney).
*
* This constructor wraps a destination response from the server with
* additional data and functions, like references to previous and next stops
* within the route.
*
* @constructor
* @param {!Object} destination Destination response from the server.
* @param {!Array<!SantaLocation>} array The route array.
* @param {number} index Index of this location in the array.
* @export
*/
export class SantaLocation {
constructor(destination, array, index) {
// We copy fields from destination onto this object. The fields are-
// >> id, arrival, departure, presentsDelivered, population, city, region, location, details
/** @export {string} */
this.id = destination['id'];
if (!this.id) {
throw new Error('got no ID for SantaLocation');
}
/**
* Santa's arrival time. Millis since unix epoch (suitable for use with Date constructor).
* @export {number}
*/
this.arrival = destination['arrival'];
/**
* Santa's departure time. Millis since unix epoch (suitable for use with Date constructor).
* @export {number}
*/
this.departure = destination['departure'];
/** @export {number} */
this.presentsDelivered = destination['presentsDelivered'];
/** @export {number} */
this.population = destination['population'];
/** @export {string} */
this.city = destination['city'];
/** @export {string} */
this.region = destination['region'];
/** @export {!LatLng} */
this.location = destination['location'];
/** @export {!SantaDetails} */
this.details = destination['details'];
// Properties below are private and not from destination.
/** @private {!Array<!SantaLocation>} */
this._array = array;
/** @private {number} */
this._index = index;
/** @private {?number} */
this._distanceTravelled = null;
}
/**
* Total distance travelled in meters.
*
* @return {number}
* @export
*/
get distanceTravelled() {
if (this._distanceTravelled !== null) {
return this._distanceTravelled;
}
// Recursive, yes, but even IE6 has a call stack limit of around 1000.
const prev = this.prev;
if (prev === null) {
return this._distanceTravelled = 0;
}
const dist = prev.distanceTravelled + prev.distanceTo(this.location);
return this._distanceTravelled = dist;
}
/**
* Returns the distance between this location and a given LatLng.
*
* @param {LatLng} to
* @return {number} in meters
* @export
*/
distanceTo(to) {
return spherical.computeDistanceBetween(this.location, to);
}
/**
* Whether this SantaLocation represents the North Pole. Just return for the first location, as
* the last shouldn't be drawn twice.
*
* @return {boolean} whether this is the North Pole
* @export
*/
get isNorthPole() {
return this.prev === null;
}
/**
* @return {?SantaLocation} Santa's previous destination.
* @export
*/
get prev() {
return this._array[this._index - 1] || null;
}
/**
* @return {?SantaLocation} Santa's next destination.
* @export
*/
get next() {
return this._array[this._index + 1] || null;
}
}
export class Route {
/**
* @param {string} url
* @param {!Object<string, *>} data
*/
constructor(url, data) {
this.url = url;
/** @type {!Array<!StreamUpdate>} */
this._stream = buildStream(/** @type {!Array<!Object>} */ (data['stream']));
const destinations = /** @type {!Array<!Object>} */ (data['destinations']) || [];
const arr = [];
const update = destinations.map((raw, i) => {
// nb. There should not be any bad data, but act defensively.
if (raw['departure'] < raw['arrival'] && i !== destinations.length - 1) {
raw['departure'] = raw['arrival']; // departure is >= arrival
}
return new SantaLocation(raw, arr, i)
});
arr.push(...update);
/**
* @export {!Array<!SantaLocation>}
*/
this.locations = arr;
}
/**
* @param {?LatLng} p position to find nearest location to
* @param {number=} limit for search in meters, default 1000km
* @return {SantaLocation} nearest location
* @export
*/
nearestDestinationTo(user, limit=1000 * 1000) {
if (!user) {
return null;
}
// We don't optimize this overly much, because it should only be called once on each load.
let best = Infinity;
let cand = null;
const all = this.locations;
const l = all.length;
for (let i = 0; i < l; ++i) {
const test = all[i];
if (Math.abs(user.lat - test.location.lat) > 15) {
continue; // too far out, assume >15deg is too much
}
const dist = spherical.computeDistanceBetween(user, test.location);
if (dist > limit || dist >= best) {
continue; // outside threshold
}
best = dist;
cand = test;
}
return cand;
}
/**
* Return the SantaState object for the current time.
*
* @param {number} timestamp
* @param {?LatLng=} userLocation
* @param {?SantaLocation=} userDestination
* @return {!SantaState}
* @export
*/
getState(timestamp, userLocation=null, userDestination=null) {
const destIndex = this._findDestinationIndex(timestamp);
const dest = this.locations[destIndex];
const dests = this.locations.slice(0, destIndex); // do not include dest in slice
const arrivalTime = userDestination ? userDestination.arrival : 0;
const stream = this._findNearestStream(timestamp);
if (timestamp >= dest.arrival || dest.prev === null) {
dests.push(dest);
// Santa is at `dest`.
const position = dest.location;
const distanceToUser =
userLocation ? spherical.computeDistanceBetween(position, userLocation) : -1;
return /** @type {!SantaState} */ ({
position,
heading: 0,
presentsDelivered: calculatePresentsDelivered(timestamp, dest.prev, dest, dest.next),
distanceTravelled: dest.distanceTravelled,
distanceToUser,
userDestination,
arrivalTime,
prev: dest.prev,
stopover: dest,
next: dest.next,
dests,
stream,
});
}
// Santa is in transit.
const {prev} = dest;
const travelTime = dest.arrival - prev.departure;
const elapsed = Math.max(timestamp - prev.departure, 0);
const ratio = elapsed / travelTime;
let currentLocation;
if (userDestination && userLocation && dest.id === userDestination.id) {
// If this is the segment where the user is, interpolate to them.
const firstDistance = spherical.computeDistanceBetween(prev.location, userLocation);
const secondDistance = spherical.computeDistanceBetween(userLocation, dest.location);
const along = (firstDistance + secondDistance) * ratio;
if (along < firstDistance) {
// Interpolate between the previous location and the user's location.
const firstRatio = firstDistance ? (along / firstDistance) : 0;
currentLocation = spherical.interpolate(prev.location, userLocation, firstRatio);
} else {
// Interpolate between the user's location and the destination.
const secondRatio = secondDistance ? ((along - firstDistance) / secondDistance) : 0;
currentLocation = spherical.interpolate(userLocation, dest.location, secondRatio);
}
} else {
// Otherwise, interpolate between stops normally.
currentLocation = spherical.interpolate(prev.location, dest.location, ratio);
}
const distanceToUser =
userLocation ? spherical.computeDistanceBetween(currentLocation, userLocation) : -1;
return /** @type {!SantaState} */ ({
position: currentLocation,
heading: spherical.computeHeading(currentLocation, dest.location),
presentsDelivered: calculatePresentsDelivered(timestamp, prev, null, dest),
distanceTravelled: calculateDistanceTravelled(timestamp, prev, dest),
distanceToUser,
userDestination,
arrivalTime,
prev: dest.prev,
stopover: null,
next: dest,
dests,
stream,
});
}
/**
* @param {number} timestamp
* @return {?StreamUpdate} closest stream entry, or null
*/
_findNearestStream(timestamp) {
const index = sort.bisectLeft(this._stream, timestamp, (entry) => entry.timestamp);
return this._stream[index] || null;
}
/**
* @param {number} timestamp
* @return {number} index of location
*/
_findDestinationIndex(timestamp) {
const locations = this.locations;
const first = locations[0];
if (!first || first.departure > timestamp) {
return 0; // not flying yet, assume at workshop
}
const index = sort.bisectLeft(locations, timestamp, (entry) => entry.departure);
if (index >= locations.length) {
return locations.length - 1;
}
return index;
}
}
/**
* @param {!Array<!Object>} raw
* @return {!Array<!StreamUpdate>}
*/
function buildStream(raw) {
const out = [];
const valid = ['didyouknow', 'status', 'update'];
function findValid(cand) {
for (let i = 0; i < valid.length; ++i) {
const t = valid[i];
if (t in cand && cand[t]) {
return t;
}
}
return null;
}
raw.forEach((cand) => {
if (!cand.timestamp) {
return;
}
const type = findValid(cand);
if (!type) {
return; // old-style stream that we don't care about
}
const update = /** @type {!StreamUpdate} */ ({
timestamp: /** @type {number} */ (cand.timestamp),
type: type,
message: /** @type {string} */ (cand[type]),
});
out.push(update);
});
return out;
}
/**
* @param {(?LatLng|string|null|undefined)} s
* @return {?LatLng}
*/
export function parseLatLng(s) {
if (!s) {
return null;
}
/** @type {?LatLng} */
let out = null;
if (typeof s === 'object') {
// support passing an existing LatLng object
out = {
lat: +s.lat,
lng: +s.lng,
};
} else if (typeof s === 'string') {
const parts = s.split(',');
if (parts.length !== 2) {
return null;
}
out = {lat: +parts[0], lng: +parts[1]};
} else {
return null;
}
if (!isFinite(out.lat) || !isFinite(out.lng)) {
return null; // NaN or Infinity
}
return out;
}
/**
* @param {number} now
* @param {!SantaLocation} prev
* @param {?SantaLocation} stopover
* @param {!SantaLocation} next
* @return {number}
*/
export function calculatePresentsDelivered(now, prev, stopover, next) {
if (next === null) {
if (stopover !== null) {
// nb. only happens at landing, which has no gifts
return stopover.presentsDelivered;
} else if (prev !== null) {
return prev.presentsDelivered;
}
}
if (prev === null) {
return 0;
}
if (!stopover) {
// Santa is flying between prev and next.
const elapsed = now - prev.departure;
const duration = next.arrival - prev.departure;
let delivering = (next.presentsDelivered - prev.presentsDelivered);
if (next.next === null) {
// The next stop is the final stop, so deliver all the presents before Santa arrives: don't
// adjust by PRESENTS_OVER_WATER, which reduces the amount so that we can deliver at city.
} else {
delivering *= PRESENTS_OVER_WATER;
}
// While flying, deliver some of the quota.
return Math.floor(prev.presentsDelivered + delivering * elapsed / duration);
}
const elapsed = now - stopover.arrival;
const duration = (stopover.departure - stopover.arrival) || 1e-10;
const delivering = stopover.presentsDelivered - prev.presentsDelivered;
// While stopped, deliver remaining quota.
return Math.floor(prev.presentsDelivered +
(delivering * PRESENTS_OVER_WATER) +
delivering * PRESENTS_IN_CITY * elapsed / duration);
}
/**
* @param {number} now
* @param {!SantaLocation} prev
* @param {!SantaLocation} next
* @return {number}
*/
export function calculateDistanceTravelled(now, prev, next) {
const elapsed = now - prev.departure;
const travelTime = next.arrival - prev.departure;
if (!travelTime) {
return next.distanceTravelled;
}
const legLength = spherical.computeDistanceBetween(next.location, prev.location);
return prev.distanceTravelled + (legLength * (elapsed / travelTime));
}