MCPcopy
hub / github.com/slowlydev/f1-dash / getDriverPosition

Function getDriverPosition

dashboard/src/components/dashboard/Map.tsx:29–99  ·  view source on GitHub ↗
(
	timingDriver: TimingDataDriver | undefined,
	originalTrackPoints: { x: number; y: number }[] | null,
)

Source from the content-addressed store, hash-verified

27
28// Function to calculate driver position based on their segment progress
29function getDriverPosition(
30 timingDriver: TimingDataDriver | undefined,
31 originalTrackPoints: { x: number; y: number }[] | null,
32): PositionCar | null {
33 if (!timingDriver || !originalTrackPoints || originalTrackPoints.length === 0) {
34 return null;
35 }
36
37 // Get all segments from all sectors
38 const allSegments = timingDriver.Sectors.flatMap((sector) => sector.Segments);
39
40 if (allSegments.length === 0) {
41 // No segments available, position at start/finish line
42 return {
43 Status: "OnTrack",
44 X: originalTrackPoints[0].x,
45 Y: originalTrackPoints[0].y,
46 Z: 0,
47 };
48 }
49
50 // Find the furthest segment with a meaningful status
51 // Status values: 0 = not started, 1 = in progress, 2+ = completed
52 let furthestSegmentIndex = -1;
53 for (let i = allSegments.length - 1; i >= 0; i--) {
54 const status = allSegments[i].Status;
55 if (status !== undefined && status > 0) {
56 furthestSegmentIndex = i;
57 break;
58 }
59 }
60
61 // If no completed segments found, check for any segment with status 0 (current segment)
62 if (furthestSegmentIndex === -1) {
63 for (let i = 0; i < allSegments.length; i++) {
64 if (allSegments[i].Status !== undefined) {
65 furthestSegmentIndex = i;
66 break;
67 }
68 }
69 }
70
71 // Still no segments found, default to start
72 if (furthestSegmentIndex === -1) {
73 furthestSegmentIndex = 0;
74 }
75
76 // Calculate position index based on segment progress
77 // Add small offset for in-progress segments to show forward movement
78 const baseRatio = furthestSegmentIndex / Math.max(allSegments.length - 1, 1);
79 const currentSegmentStatus = allSegments[furthestSegmentIndex]?.Status || 0;
80
81 // Add fractional progress within current segment if it's in progress (status 1)
82 const segmentProgress = currentSegmentStatus === 1 ? 0.5 : 0;
83 const segmentSize = 1 / Math.max(allSegments.length, 1);
84 const adjustedRatio = baseRatio + segmentProgress * segmentSize;
85
86 const positionIndex = Math.floor(adjustedRatio * (originalTrackPoints.length - 1));

Callers 1

MapFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected