Skip to content

Commit af41a56

Browse files
DerMegaTMBraunDEtariknz
authored
Feature/faster cars from behind (#15)
Co-authored-by: Tobias Mueller <[email protected]> Co-authored-by: Tarik Alani <[email protected]> Co-authored-by: Tarik Alani <[email protected]>
1 parent 6c29a41 commit af41a56

File tree

9 files changed

+103
-5
lines changed

9 files changed

+103
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/bridge/iracingSdk/mock-data/session.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,7 @@
34263426
"CarClassPowerAdjust": "0.000 %",
34273427
"CarClassDryTireSetLimit": "0 %",
34283428
"CarClassColor": 16767577,
3429-
"CarClassEstLapTime": 113.6302,
3429+
"CarClassEstLapTime": 126.6302,
34303430
"IRating": 1367,
34313431
"LicLevel": 15,
34323432
"LicSubLevel": 375,

src/app/irsdk/node/utils/mock-data/session.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,7 @@
34263426
"CarClassPowerAdjust": "0.000 %",
34273427
"CarClassDryTireSetLimit": "0 %",
34283428
"CarClassColor": 16767577,
3429-
"CarClassEstLapTime": 113.6302,
3429+
"CarClassEstLapTime": 126.6302,
34303430
"IRating": 1367,
34313431
"LicLevel": 15,
34323432
"LicSubLevel": 375,

src/app/storage/defaultDashboard.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,15 @@ export const defaultDashboard: DashboardLayout = {
6969
height: 330,
7070
},
7171
},
72+
{
73+
id: 'fastercarsfrombehind',
74+
enabled: false,
75+
layout: {
76+
x: 700,
77+
y: 200,
78+
width: 400,
79+
height: 40,
80+
},
81+
}
7282
],
7383
};

src/frontend/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Settings } from './components/Settings/Settings';
1414
import { Relative } from './components/Standings/Relative';
1515
import { Weather } from './components/Weather';
1616
import { TrackMap } from './components/TrackMap/TrackMap';
17+
import { FasterCarsFromBehind } from './components/FasterCarsFromBehind/FasterCarsFromBehind';
1718
import { EditMode } from './components/EditMode/EditMode';
1819

1920
// TODO: type this better, right now the config comes from settings
@@ -25,6 +26,7 @@ const WIDGET_MAP: Record<string, (config: any) => React.JSX.Element> = {
2526
settings: Settings,
2627
map: TrackMap,
2728
weather: Weather,
29+
fastercarsfrombehind: FasterCarsFromBehind,
2830
};
2931

3032
const AppRoutes = () => {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Meta, StoryObj } from '@storybook/react';
2+
import { FasterCarsFromBehind } from './FasterCarsFromBehind';
3+
import { TelemetryDecorator } from '../../../../.storybook/telemetryDecorator';
4+
import { DynamicTelemetrySelector } from '../Standings/DynamicTelemetrySelector';
5+
import { useState } from 'react';
6+
7+
export default {
8+
component: FasterCarsFromBehind,
9+
parameters: {
10+
controls: {
11+
exclude: ['telemetryPath'],
12+
}
13+
}
14+
} as Meta<typeof FasterCarsFromBehind>;
15+
16+
type Story = StoryObj<typeof FasterCarsFromBehind>;
17+
18+
export const Primary: Story = {
19+
decorators: [TelemetryDecorator()],
20+
};
21+
22+
export const DynamicTelemetry: Story = {
23+
decorators: [(Story, context) => {
24+
const [selectedPath, setSelectedPath] = useState('/test-data/1745291694179');
25+
26+
return (
27+
<>
28+
<DynamicTelemetrySelector
29+
onPathChange={setSelectedPath}
30+
initialPath={selectedPath}
31+
/>
32+
{TelemetryDecorator(selectedPath)(Story, context)}
33+
</>
34+
);
35+
}],
36+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useMemo } from 'react';
2+
import { useAutoAnimate } from '@formkit/auto-animate/react';
3+
import { useCarBehind } from './hooks/useCarBehind';
4+
5+
export const FasterCarsFromBehind = () => {
6+
const carBehind = useCarBehind();
7+
const [parent] = useAutoAnimate();
8+
9+
const layout = useMemo(() => {
10+
const hidden = carBehind.name === null || carBehind.name == undefined ? 'hidden' : '';
11+
const animate = carBehind.distance > -0.3 ? 'animate-pulse' : '';
12+
const red = carBehind.percent;
13+
const green = 100 - carBehind.percent;
14+
15+
return { hidden, animate, red, green };
16+
}, [
17+
carBehind.name,
18+
carBehind.distance,
19+
carBehind.percent
20+
]);
21+
22+
return (
23+
<div className={`w-full flex justify-between rounded-sm p-1 pb-2 font-bold relative ${layout.hidden} ${layout.animate} ${carBehind.background}`}
24+
ref={parent}>
25+
<div className="rounded-sm bg-gray-700 p-1">{carBehind.name}</div>
26+
<div className="rounded-sm bg-gray-700 p-1">{carBehind.distance}</div>
27+
<div className={`absolute bottom-0 left-0 rounded-b-sm bg-white h-1 flex-none`} style={{width: carBehind.percent+'%', backgroundColor: `rgb(${layout.red}%, ${layout.green}%, 0%)`}}></div>
28+
</div>
29+
);
30+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useMemo } from 'react';
2+
import { useDriverRelatives } from '../../Standings/hooks/useDriverRelatives';
3+
import { getTailwindStyle } from '../../../utils/colors';
4+
5+
export const useCarBehind = () => {
6+
const drivers = useDriverRelatives({ buffer: 1 });
7+
const carBehind = drivers[2];
8+
const myCar = drivers[1];
9+
10+
const background = getTailwindStyle(carBehind?.carClass?.color).classHeader;
11+
12+
const FasterCarFromBehind = useMemo(() => {
13+
const percent = parseInt((100 - (Math.abs(carBehind?.delta) / 3 * 100)).toFixed(0));
14+
15+
return { name: carBehind?.driver?.name, distance: parseFloat(carBehind?.delta.toFixed(1)), background: background, percent : percent };
16+
}, [carBehind?.delta, carBehind?.driver?.name, background]);
17+
18+
if(carBehind?.carClass?.relativeSpeed <= myCar?.carClass?.relativeSpeed || carBehind?.delta < -3) return {name: null, distance: 0, background: null, percent: 0};
19+
return FasterCarFromBehind;
20+
};

test-data/1733030013074/session.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,7 @@
34263426
"CarClassPowerAdjust": "0.000 %",
34273427
"CarClassDryTireSetLimit": "0 %",
34283428
"CarClassColor": 16767577,
3429-
"CarClassEstLapTime": 113.6302,
3429+
"CarClassEstLapTime": 126.6302,
34303430
"IRating": 1367,
34313431
"LicLevel": 15,
34323432
"LicSubLevel": 375,

0 commit comments

Comments
 (0)