From 1a519662885919cf18b26606c9757713988abc5b Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 18 Mar 2022 15:54:37 -0400 Subject: [PATCH] ISSUE-080 - Adjustments to state around Event. Added new EventStatus state to track when the event is waiting, running, suspended, or stopped. --- .../StatusIndicator/StatusIndicator.tsx | 2 +- src/features/event/eventSlice.ts | 45 ++++++++++++++----- src/types/state.ts | 13 ++++-- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/components/BroadcastGraphics/TimingBoard/StatusIndicator/StatusIndicator.tsx b/src/components/BroadcastGraphics/TimingBoard/StatusIndicator/StatusIndicator.tsx index a9e2364..a76c544 100644 --- a/src/components/BroadcastGraphics/TimingBoard/StatusIndicator/StatusIndicator.tsx +++ b/src/components/BroadcastGraphics/TimingBoard/StatusIndicator/StatusIndicator.tsx @@ -20,7 +20,7 @@ export function StatusIndicator({ const { timingBoard: { statusIndicator: { mode } } } = useSelector(broadcastGraphicsSelector); - const { progress: { scheduledLaps, lapCount } } = useSelector(eventSelector); + const { eventProgress: { scheduledLaps, lapCount } } = useSelector(eventSelector); const toWideTransition: TransitionArgs[] = [{ property: "all", diff --git a/src/features/event/eventSlice.ts b/src/features/event/eventSlice.ts index 79ab73c..e787470 100644 --- a/src/features/event/eventSlice.ts +++ b/src/features/event/eventSlice.ts @@ -13,6 +13,7 @@ import { SafetyCarStatus, VirtualSafetyCarStatus, GridSpot, + EventStatus, } from "../../types/state"; import { Meters } from "../../types/util"; import { orMatch } from "../../utils/common"; @@ -31,20 +32,21 @@ const POLE_TO_START: Meters = -10; const initialState: RootState["event"] = { trackName: "Circuit de Spa-Franchorchamps", trackLength: 7004, + trackStartFinishDelta: -124, courseStatus: { courseFlag: CourseFlag.Green, sectorFlags: [SectorFlag.Green, SectorFlag.Green, SectorFlag.Green], safetyCar: SafetyCarStatus.Clear, virtualSafetyCar: VirtualSafetyCarStatus.Clear, }, - mode: EventMode.Race, - progress: { + eventStatus: EventStatus.Waiting, + eventMode: EventMode.Race, + eventProgress: { startTime: Date.now(), timeLimit: 60 * 60 * 2 * 1000, lapCount: 1, scheduledLaps: 44, }, - leaderGridSpot: 0, grid: [ { position: 1, @@ -235,32 +237,51 @@ export const eventSlice = createSlice({ state.lastUpdate = Date.now(); }, refreshRunningOrder: (state) => { - const sortingGrid = state.grid.map((car, index) => ({ + const sortingGrid = state.grid.map((car, index): { + gridSpot: GridSpot, + distance: Meters, + status: CarStatus, + } => ({ gridSpot: index, distance: car.distance, status: car.status, })); - const add = (status: CarStatus) => { + // Adds distance to the cars that are out of the race. + const out = (status: CarStatus) => { if (status === CarStatus.Retired) { - return 1000000; + return -1000000; } else if (status === CarStatus.DidNotStart) { - return 2000000; + return -2000000; } return 0; }; sortingGrid.sort((a, b) => // Retired cars go to the end but still maintain their own order. - (b.distance - add(b.status)) - (a.distance - add(a.status)), + (b.distance + out(b.status)) - (a.distance + out(a.status)), ); + const raceEndDistance = state.trackLength * state.eventProgress.scheduledLaps + + state.trackStartFinishDelta; + sortingGrid.forEach((entry, sortedPosition) => { - state.grid[entry.gridSpot].position = sortedPosition + 1; + // Don't update positions of cars who have crossed the finish line. + if (entry.distance < raceEndDistance) { + state.grid[entry.gridSpot].position = sortedPosition + 1; + } }); - state.leaderGridSpot = sortingGrid[0].gridSpot; - state.progress.lapCount = - Math.max(1, Math.floor(sortingGrid[0].distance / state.trackLength) + 1); + + /** + * Calculate which lap we're on by taking the leader's distance. + * Also must consider the fact that the race distance 0 is at the start + * line, but the race ends at the finish line, which is often (always?) + * behind the start line by some distance. + */ + state.eventProgress.lapCount = + Math.max(1, Math.floor( + (sortingGrid[0].distance - state.trackStartFinishDelta) / state.trackLength, + ) + 1); state.lastUpdate = Date.now(); }, diff --git a/src/types/state.ts b/src/types/state.ts index fbbf7a2..0ebcfd4 100644 --- a/src/types/state.ts +++ b/src/types/state.ts @@ -71,10 +71,11 @@ export interface OverlayItem { export interface Event { trackName: string; trackLength: Meters; + trackStartFinishDelta: Meters; // Negative if finish behind start line. courseStatus: CourseStatus; - mode: EventMode; - progress: EventProgress; - leaderGridSpot: GridSpot; // Might be removed. + eventStatus: EventStatus; + eventMode: EventMode; + eventProgress: EventProgress; grid: Grid; lastUpdate: number; } @@ -84,6 +85,12 @@ export interface EventProgress { lapCount: number; scheduledLaps: number; } +export enum EventStatus { + Waiting, // Event is waiting to start. + Running, // Event is running. + Stopped, // Event has concluded. + Suspended, // Event is suspended (ex: rain delay). +} export enum EventMode { Race, Sprint,