-
Notifications
You must be signed in to change notification settings - Fork 16
update logic for relative deltas, adds base work for speeds and avg laps #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6b809f0
add car speeds store
tariknz bda58c3
more test fixes
tariknz ec8be53
add speeds column for testing
tariknz 367b81c
fixes and rounding
tariknz a55a1e5
better round
tariknz 5bfb949
debounce logic
tariknz d122675
update delta to use speed
tariknz 450a9a7
update
tariknz 579a21f
go back to using driverCarEstLapTime logic
tariknz 86a0f10
revert delta calc
tariknz 55873ab
remove useless validation
tariknz 299ee93
add class ratio adjustment
tariknz 36d8eeb
slight mod
tariknz d176ab1
tidy up
tariknz 6dfb26b
wip - another attempt
tariknz 53c3c0a
black
tariknz 66f5467
simplify
tariknz 19ecb9d
fix tests for now
tariknz ccb964b
small tweak for handling multiclass
tariknz 7415a11
updates
tariknz 9cd3059
add lap times store and tidy up carspeed store
tariknz 422bf1d
fix bug with returning nested array
tariknz 24ddd52
comment
tariknz 4d33570
fix wetness
tariknz db80acb
tidy up
tariknz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ export const Relative = () => { | |
| ))} | ||
| </tbody> | ||
| </table> | ||
|
|
||
| <SessionFooter /> | ||
| </div> | ||
| ); | ||
|
|
||
197 changes: 197 additions & 0 deletions
197
src/frontend/components/Standings/hooks/useDriverRelatives.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { useDriverRelatives } from './useDriverRelatives'; | ||
| import { useDriverCarIdx, useSessionStore, useTelemetryValues } from '@irdashies/context'; | ||
| import { useDriverStandings } from './useDriverPositions'; | ||
| import type { Standings } from '../createStandings'; | ||
|
|
||
| // Mock the context hooks | ||
| vi.mock('@irdashies/context', () => ({ | ||
| useDriverCarIdx: vi.fn(), | ||
| useTelemetryValues: vi.fn(), | ||
| useSessionStore: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('./useDriverPositions', () => ({ | ||
| useDriverStandings: vi.fn(), | ||
| })); | ||
|
|
||
| describe('useDriverRelatives', () => { | ||
| const mockDrivers: Standings[] = [ | ||
| { | ||
| carIdx: 0, | ||
| classPosition: 1, | ||
| isPlayer: true, | ||
| driver: { | ||
| name: 'Driver 1', | ||
| carNum: '1', | ||
| license: 'A', | ||
| rating: 2000, | ||
| }, | ||
| fastestTime: 100, | ||
| hasFastestTime: true, | ||
| lastTime: 105, | ||
| onPitRoad: false, | ||
| onTrack: true, | ||
| carClass: { | ||
| id: 1, | ||
| color: 0, | ||
| name: 'Class 1', | ||
| relativeSpeed: 1.0, | ||
| estLapTime: 100, | ||
| }, | ||
| }, | ||
| { | ||
| carIdx: 1, | ||
| classPosition: 2, | ||
| isPlayer: false, | ||
| driver: { | ||
| name: 'Driver 2', | ||
| carNum: '2', | ||
| license: 'B', | ||
| rating: 1800, | ||
| }, | ||
| fastestTime: 102, | ||
| hasFastestTime: false, | ||
| lastTime: 107, | ||
| onPitRoad: false, | ||
| onTrack: true, | ||
| carClass: { | ||
| id: 1, | ||
| color: 0, | ||
| name: 'Class 1', | ||
| relativeSpeed: 1.0, | ||
| estLapTime: 100, | ||
| }, | ||
| }, | ||
| { | ||
| carIdx: 2, | ||
| classPosition: 3, | ||
| isPlayer: false, | ||
| driver: { | ||
| name: 'Driver 3', | ||
| carNum: '3', | ||
| license: 'C', | ||
| rating: 1600, | ||
| }, | ||
| fastestTime: 104, | ||
| hasFastestTime: false, | ||
| lastTime: 109, | ||
| onPitRoad: false, | ||
| onTrack: true, | ||
| carClass: { | ||
| id: 1, | ||
| color: 0, | ||
| name: 'Class 1', | ||
| relativeSpeed: 1.0, | ||
| estLapTime: 100, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const mockCarIdxLapDistPct = [0.5, 0.6, 0.4]; // Player, Ahead, Behind | ||
| const mockCarIdxEstTime = [99, 100, 90]; // Player, Same class, Faster class | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(useDriverCarIdx).mockReturnValue(0); | ||
| vi.mocked(useTelemetryValues).mockImplementation((key: string) => { | ||
| if (key === 'CarIdxLapDistPct') return mockCarIdxLapDistPct; | ||
| if (key === 'CarIdxEstTime') return mockCarIdxEstTime; | ||
| return []; | ||
| }); | ||
| vi.mocked(useDriverStandings).mockReturnValue(mockDrivers); | ||
| vi.mocked(useSessionStore).mockReturnValue({ | ||
| session: { | ||
| DriverInfo: { | ||
| PaceCarIdx: 0, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return empty array when no player is found', () => { | ||
| vi.mocked(useDriverCarIdx).mockReturnValue(undefined); | ||
|
|
||
| const { result } = renderHook(() => useDriverRelatives({ buffer: 2 })); | ||
| expect(result.current).toEqual([]); | ||
| }); | ||
|
|
||
| it('should calculate correct deltas for cars ahead and behind', () => { | ||
| const { result } = renderHook(() => useDriverRelatives({ buffer: 2 })); | ||
|
|
||
| expect(result.current).toHaveLength(3); // Player + 1 ahead + 1 behind | ||
| expect(result.current[0].carIdx).toBe(1); // Car ahead | ||
| expect(result.current[1].carIdx).toBe(0); // Player | ||
| expect(result.current[2].carIdx).toBe(2); // Car behind | ||
|
|
||
| // Car ahead should have positive delta | ||
| expect(result.current[0].delta).toBeGreaterThan(0); | ||
| // Player should have zero delta | ||
| expect(result.current[1].delta).toBe(0); | ||
| // Car behind should have negative delta | ||
| expect(result.current[2].delta).toBeLessThan(0); | ||
| }); | ||
|
|
||
| it('should respect buffer limit', () => { | ||
| const { result } = renderHook(() => useDriverRelatives({ buffer: 1 })); | ||
|
|
||
| // Should only include player and one car ahead/behind | ||
| expect(result.current).toHaveLength(3); | ||
| }); | ||
|
|
||
| it.each([ | ||
| [0.1, 0.2, 0.8], // Player near start, Car ahead near start, Car behind near finish | ||
| [0.2, 0.3, 0.9], | ||
| [0, 0.1, 0.7], | ||
| [0.9, 0, 0.6], | ||
| ])( | ||
| 'should handle cars crossing the start/finish line', | ||
| (playerDistPct, aheadDistPct, behindDistPct) => { | ||
| const mockCarIdxLapDistPctWithCrossing = [ | ||
| playerDistPct, | ||
| aheadDistPct, | ||
| behindDistPct, | ||
| ]; | ||
|
|
||
| vi.mocked(useTelemetryValues).mockImplementation((key: string) => { | ||
| if (key === 'CarIdxLapDistPct') return mockCarIdxLapDistPctWithCrossing; | ||
| if (key === 'CarIdxEstTime') return mockCarIdxEstTime; | ||
| return []; | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => useDriverRelatives({ buffer: 1 })); | ||
|
|
||
| // Car ahead should still be ahead by 10% | ||
| expect(result.current[0].carIdx).toBe(1); | ||
| expect(result.current[0].relativePct).toBeCloseTo(0.1); | ||
| expect(result.current[0].delta).toBeCloseTo(10); | ||
|
|
||
| // Player should be in the middle | ||
| expect(result.current[1].carIdx).toBe(0); | ||
| expect(result.current[1].relativePct).toBe(0); | ||
| expect(result.current[1].delta).toBe(0); | ||
|
|
||
| // Car behind should be behind by 20% | ||
| expect(result.current[2].carIdx).toBe(2); | ||
| expect(result.current[2].relativePct).toBeCloseTo(-0.3); | ||
| expect(result.current[2].delta).toBeCloseTo(-30); | ||
| } | ||
| ); | ||
|
|
||
| it('should filter out off-track cars', () => { | ||
| const mockDriversWithOffTrack = [ | ||
| { ...mockDrivers[0] }, | ||
| { ...mockDrivers[1], onTrack: false }, | ||
| { ...mockDrivers[2] }, | ||
| ]; | ||
|
|
||
| vi.mocked(useDriverStandings).mockReturnValue(mockDriversWithOffTrack); | ||
|
|
||
| const { result } = renderHook(() => useDriverRelatives({ buffer: 2 })); | ||
|
|
||
| // Should not include the off-track car | ||
| expect(result.current).toHaveLength(2); | ||
| expect(result.current.some((driver) => driver.carIdx === 1)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.