feat(hunt): track walking distance via GPS, show live locations on map
Docker Image CI / test (push) Failing after 41s
Docker Image CI / deploy (push) Skipped
Tests / unit (push) Failing after 36s
Tests / integration (push) Successful in 1m34s

Add foreground-only GPS distance tracking for hunts
This commit is contained in:
2026-09-27 20:55:58 +02:00
parent a0fc836c05
commit 61cc516f47
43 changed files with 2313 additions and 24 deletions
+94
View File
@@ -0,0 +1,94 @@
/**
* Shared, pure geo math used by both the client accumulator and the
* server-side distance recompute. Distances are in meters; the aggregate
* is stored as an Int (rounded) to avoid float drift in the database.
*/
/** Mean earth radius in meters (WGS-84 volumetric mean). */
const EARTH_RADIUS_M = 6_371_000;
/** Fixes reporting an accuracy worse than this are dropped (anchor unchanged). */
export const MAX_ACCURACY_M = 50;
/** A candidate implying a speed above this is a teleport (anchor unchanged). */
export const MAX_SPEED_MPS = 7;
/** Segments shorter than this are treated as stationary jitter (not added). */
export const MIN_SEGMENT_M = 5;
export type GeoPoint = {
lat: number;
lon: number;
accuracy?: number | null;
recordedAt: Date | string;
};
export type GeoCoord = Pick<GeoPoint, 'lat' | 'lon'>;
/** Great-circle distance between two coordinates in meters. */
export function haversine(a: GeoCoord, b: GeoCoord): number {
const toRad = (deg: number) => (deg * Math.PI) / 180;
const dLat = toRad(b.lat - a.lat);
const dLon = toRad(b.lon - a.lon);
const lat1 = toRad(a.lat);
const lat2 = toRad(b.lat);
const h =
Math.sin(dLat / 2) ** 2 +
Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLon / 2) ** 2;
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(h)));
}
function toTime(recordedAt: Date | string): number {
return new Date(recordedAt).getTime();
}
/**
* Derive the walked distance (Int meters) from a set of raw fixes.
*
* Points are deduped (by `recordedAt` + coords) and sorted by `recordedAt`,
* then walked with an "anchor". For each candidate:
* - accuracy worse than {@link MAX_ACCURACY_M} → dropped, anchor unchanged;
* - implied speed above {@link MAX_SPEED_MPS} → teleport, anchor unchanged;
* - segment shorter than {@link MIN_SEGMENT_M} → jitter, anchor unchanged;
* - otherwise the segment is added and the anchor advances.
*/
export function computeTrackDistance(points: GeoPoint[]): number {
const seen = new Set<string>();
const normalized: (GeoPoint & { time: number })[] = [];
for (const p of points) {
const time = toTime(p.recordedAt);
if (Number.isNaN(time)) continue;
const key = `${time}|${p.lat}|${p.lon}`;
if (seen.has(key)) continue;
seen.add(key);
normalized.push({ ...p, time });
}
normalized.sort((a, b) => a.time - b.time);
let total = 0;
let anchor: { lat: number; lon: number; time: number } | null = null;
for (const p of normalized) {
if (p.accuracy != null && p.accuracy > MAX_ACCURACY_M) continue;
if (anchor === null) {
anchor = { lat: p.lat, lon: p.lon, time: p.time };
continue;
}
const dtSeconds = (p.time - anchor.time) / 1000;
// Non-positive time delta is ambiguous (duplicate timestamps) → skip.
if (dtSeconds <= 0) continue;
const distance = haversine(anchor, p);
if (distance / dtSeconds > MAX_SPEED_MPS) continue; // teleport
if (distance < MIN_SEGMENT_M) continue; // stationary jitter
total += distance;
anchor = { lat: p.lat, lon: p.lon, time: p.time };
}
return Math.round(total);
}