ISSUE-080 - Adjustments to state around Event.

Added new EventStatus state to track when the event is waiting, running, suspended, or stopped.
This commit is contained in:
Kyle Geib committed 2022-03-18 15:54:37 -04:00
1 parent 734a05d73e
commit 1a51966288
3 files changed
+44 -16

No files matched your search

@@ -20,7 +20,7 @@ export function StatusIndicator({
const { timingBoard: { statusIndicator: { mode } } } = const { timingBoard: { statusIndicator: { mode } } } =
useSelector(broadcastGraphicsSelector); useSelector(broadcastGraphicsSelector);
const { progress: { scheduledLaps, lapCount } } = useSelector(eventSelector); const { eventProgress: { scheduledLaps, lapCount } } = useSelector(eventSelector);
const toWideTransition: TransitionArgs[] = [{ const toWideTransition: TransitionArgs[] = [{
property: "all", property: "all",
+33 -12
View File
@@ -13,6 +13,7 @@ import {
SafetyCarStatus, SafetyCarStatus,
VirtualSafetyCarStatus, VirtualSafetyCarStatus,
GridSpot, GridSpot,
EventStatus,
} from "../../types/state"; } from "../../types/state";
import { Meters } from "../../types/util"; import { Meters } from "../../types/util";
import { orMatch } from "../../utils/common"; import { orMatch } from "../../utils/common";
@@ -31,20 +32,21 @@ const POLE_TO_START: Meters = -10;
const initialState: RootState["event"] = { const initialState: RootState["event"] = {
trackName: "Circuit de Spa-Franchorchamps", trackName: "Circuit de Spa-Franchorchamps",
trackLength: 7004, trackLength: 7004,
trackStartFinishDelta: -124,
courseStatus: { courseStatus: {
courseFlag: CourseFlag.Green, courseFlag: CourseFlag.Green,
sectorFlags: [SectorFlag.Green, SectorFlag.Green, SectorFlag.Green], sectorFlags: [SectorFlag.Green, SectorFlag.Green, SectorFlag.Green],
safetyCar: SafetyCarStatus.Clear, safetyCar: SafetyCarStatus.Clear,
virtualSafetyCar: VirtualSafetyCarStatus.Clear, virtualSafetyCar: VirtualSafetyCarStatus.Clear,
}, },
mode: EventMode.Race, eventStatus: EventStatus.Waiting,
progress: { eventMode: EventMode.Race,
eventProgress: {
startTime: Date.now(), startTime: Date.now(),
timeLimit: 60 * 60 * 2 * 1000, timeLimit: 60 * 60 * 2 * 1000,
lapCount: 1, lapCount: 1,
scheduledLaps: 44, scheduledLaps: 44,
}, },
leaderGridSpot: 0,
grid: [ grid: [
{ {
position: 1, position: 1,
@@ -235,32 +237,51 @@ export const eventSlice = createSlice({
state.lastUpdate = Date.now(); state.lastUpdate = Date.now();
}, },
refreshRunningOrder: (state) => { refreshRunningOrder: (state) => {
const sortingGrid = state.grid.map((car, index) => ({ const sortingGrid = state.grid.map((car, index): {
gridSpot: GridSpot,
distance: Meters,
status: CarStatus,
} => ({
gridSpot: index, gridSpot: index,
distance: car.distance, distance: car.distance,
status: car.status, 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) { if (status === CarStatus.Retired) {
return 1000000; return -1000000;
} else if (status === CarStatus.DidNotStart) { } else if (status === CarStatus.DidNotStart) {
return 2000000; return -2000000;
} }
return 0; return 0;
}; };
sortingGrid.sort((a, b) => sortingGrid.sort((a, b) =>
// Retired cars go to the end but still maintain their own order. // 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) => { 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(); state.lastUpdate = Date.now();
}, },
+10 -3
View File
@@ -71,10 +71,11 @@ export interface OverlayItem {
export interface Event { export interface Event {
trackName: string; trackName: string;
trackLength: Meters; trackLength: Meters;
trackStartFinishDelta: Meters; // Negative if finish behind start line.
courseStatus: CourseStatus; courseStatus: CourseStatus;
mode: EventMode; eventStatus: EventStatus;
progress: EventProgress; eventMode: EventMode;
leaderGridSpot: GridSpot; // Might be removed. eventProgress: EventProgress;
grid: Grid; grid: Grid;
lastUpdate: number; lastUpdate: number;
} }
@@ -84,6 +85,12 @@ export interface EventProgress {
lapCount: number; lapCount: number;
scheduledLaps: 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 { export enum EventMode {
Race, Race,
Sprint, Sprint,