diff --git a/package.json b/package.json
index 0a03793..cbbe916 100644
--- a/package.json
+++ b/package.json
@@ -43,6 +43,7 @@
"react-scripts": "5.0.0",
"react-use-measure": "^2.1.1",
"resize-observer-polyfill": "^1.5.1",
+ "rooks": "^5.10.3",
"styled-components": "^5.3.3",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
diff --git a/src/App.tsx b/src/App.tsx
index 5715b1d..d35ba53 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -8,6 +8,7 @@ import {
import { MainMenu } from "./components/Menu";
import { useDispatch } from "react-redux";
import { useEffect } from "react";
+import { EventLoop } from "./components/Broadcast/RaceDirectorUI/EventLoop/EventLoop";
function App() {
const dispatch = useDispatch();
@@ -40,6 +41,7 @@ function App() {
+
);
}
diff --git a/src/components/Broadcast/Layout/Layout.tsx b/src/components/Broadcast/Layout/Layout.tsx
index fb7fe3f..0e333c8 100644
--- a/src/components/Broadcast/Layout/Layout.tsx
+++ b/src/components/Broadcast/Layout/Layout.tsx
@@ -5,6 +5,7 @@ import { MockupBlock } from "../../Common/MockupBlock.styled";
import { LayoutGrid, LayoutGridItem } from "../../Common/LayoutGrid.styled";
import { TimingTowerPrototypeControls } from "../../Workspaces/PrototypingControls/TimingTowerPrototype";
import { ChyronsDirectorControls } from "../BroadcastDirectorUI/Chyrons";
+import { RaceDirectorControls } from "../RaceDirectorUI/EventLoop/RaceDirectorControls";
type LayoutProps = {
headerHeight?: number,
@@ -31,9 +32,7 @@ export function Layout({ headerHeight }: LayoutProps) {
- Race Director UI
+
diff --git a/src/components/Broadcast/RaceDirectorUI/EventLoop/EventLoop.tsx b/src/components/Broadcast/RaceDirectorUI/EventLoop/EventLoop.tsx
new file mode 100644
index 0000000..ca44862
--- /dev/null
+++ b/src/components/Broadcast/RaceDirectorUI/EventLoop/EventLoop.tsx
@@ -0,0 +1,45 @@
+import { useState } from "react";
+import { useIntervalWhen } from "rooks";
+import { useDispatch, useSelector } from "react-redux";
+
+import { eventSelector } from "../../../../features/event/eventSelector";
+import { EventStatus, GridSpot } from "../../../../types/state";
+import { Milliseconds } from "../../../../types/util";
+import { increaseDistance, refreshRunningOrder } from "../../../../features/event/eventSlice";
+
+const UPDATE_TICK_MS: Milliseconds = 1500;
+const SUB_DIVISIONS = 1;
+
+export function EventLoop() {
+ const dispatch = useDispatch();
+ const { eventStatus, grid, sortedGridSpots } = useSelector(eventSelector);
+
+ const gridSpots: GridSpot[] = Array.from(Array(grid.length).keys());
+
+ const [tick, setTick] = useState(0);
+ const amount = Math.ceil(gridSpots.length / SUB_DIVISIONS);
+
+ useIntervalWhen(
+ () => {
+ const selectedGridSpots = sortedGridSpots.slice(tick, tick + amount);
+ dispatch(increaseDistance({
+ gridSpots: selectedGridSpots,
+ distance: UPDATE_TICK_MS / 1000 * 61, // 61 m/s for Spa.
+ randomness: 0.008,
+ usePerformance: true,
+ }));
+ dispatch(refreshRunningOrder());
+
+ if ((tick + amount) > gridSpots.length) {
+ setTick(0);
+ } else {
+ setTick(tick + amount);
+ }
+ },
+ UPDATE_TICK_MS / SUB_DIVISIONS,
+ eventStatus === EventStatus.Running,
+ true,
+ );
+
+ return null;
+}
diff --git a/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/RaceDirectorControls.tsx b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/RaceDirectorControls.tsx
new file mode 100644
index 0000000..10686f4
--- /dev/null
+++ b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/RaceDirectorControls.tsx
@@ -0,0 +1,26 @@
+import { useDispatch, useSelector } from "react-redux";
+import { PadBox } from "@bedrock-layout/padbox";
+
+import { eventSelector } from "../../../../../features/event/eventSelector";
+import { ClickSpan } from "../../../../Common/Inputs/ClickSpan.styled";
+import { EventStatus } from "../../../../../types/state";
+import { setEventStatus } from "../../../../../features/event/eventSlice";
+
+export function RaceDirectorControls() {
+ const dispatch = useDispatch();
+ const { eventStatus } = useSelector(eventSelector);
+
+ return (
+
+ {
+ if (eventStatus !== EventStatus.Running) {
+ dispatch(setEventStatus(EventStatus.Running));
+ } else {
+ dispatch(setEventStatus(EventStatus.Stopped));
+ }
+ }}>
+ EventStatus = {EventStatus[eventStatus]}
+
+
+ );
+}
diff --git a/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/index.ts b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/index.ts
new file mode 100644
index 0000000..f2bb12e
--- /dev/null
+++ b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/index.ts
@@ -0,0 +1 @@
+export * from "./RaceDirectorControls";
diff --git a/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/styles.ts b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/styles.ts
new file mode 100644
index 0000000..556ca95
--- /dev/null
+++ b/src/components/Broadcast/RaceDirectorUI/EventLoop/RaceDirectorControls/styles.ts
@@ -0,0 +1,3 @@
+import styled from "styled-components/macro";
+
+export const Derp = styled.div``;
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/components/Workspaces/PrototypingControls/TimingTowerPrototype/styles.ts b/src/components/Common/Inputs/ClickSpan.styled.ts
similarity index 100%
rename from src/components/Workspaces/PrototypingControls/TimingTowerPrototype/styles.ts
rename to src/components/Common/Inputs/ClickSpan.styled.ts
diff --git a/src/components/Workspaces/PrototypingControls/TimingTowerPrototype/TimingTowerPrototypeControls.tsx b/src/components/Workspaces/PrototypingControls/TimingTowerPrototype/TimingTowerPrototypeControls.tsx
index 0f07fce..0324e0f 100644
--- a/src/components/Workspaces/PrototypingControls/TimingTowerPrototype/TimingTowerPrototypeControls.tsx
+++ b/src/components/Workspaces/PrototypingControls/TimingTowerPrototype/TimingTowerPrototypeControls.tsx
@@ -3,16 +3,16 @@ import { PadBox } from "@bedrock-layout/padbox";
import { Split } from "@bedrock-layout/split";
import { Stack } from "@bedrock-layout/stack";
import { useDispatch, useSelector } from "react-redux";
-import { broadcastDirectorSelector } from "../../../../features/broadcast/director/broadcastDirectorSelector";
+import { broadcastDirectorSelector } from "../../../../features/broadcast/director/broadcastDirectorSelector";
import { broadcastGraphicsSelector } from "../../../../features/broadcast/graphics/broadcastGraphicsSelector";
-import { setTimingTowerDisplayMode, setTimingTowerSplitsMode } from "../../../../features/broadcast/graphics/broadcastGraphicsSlice";
import { eventSelector } from "../../../../features/event/eventSelector";
+import { setTimingTowerDisplayMode, setTimingTowerSplitsMode } from "../../../../features/broadcast/graphics/broadcastGraphicsSlice";
import { awardFastestLap, increaseDistance, refreshRunningOrder, setCarStatus } from "../../../../features/event/eventSlice";
import { BGTimingTowerDisplayMode, BGTimingTowerSplitsMode, CarStatus } from "../../../../types/state";
-import { GridSelection } from "../GridSelection";
-import { ClickSpan } from "./styles";
+import { GridSelection } from "../GridSelection";
+import { ClickSpan } from "../../../Common/Inputs/ClickSpan.styled";
export function TimingTowerPrototypeControls() {
const dispatch = useDispatch();
@@ -113,18 +113,18 @@ export function TimingTowerPrototypeControls() {
>
Splits Mode = {BGTimingTowerSplitsMode[splitsMode]}
- dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.LeftAndRight))}>
- {(displayMode === BGTimingTowerDisplayMode.LeftAndRight ? "*" : "") + "LeftAndRight"}
+ dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.Hidden))}>
+ {(displayMode === BGTimingTowerDisplayMode.Hidden ? "*" : "") + "Hidden"}
dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.LeftOnly))}>
{(displayMode === BGTimingTowerDisplayMode.LeftOnly ? "*" : "") + "LeftOnly"}
+ dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.LeftAndRight))}>
+ {(displayMode === BGTimingTowerDisplayMode.LeftAndRight ? "*" : "") + "LeftAndRight"}
+
dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.FullLeft))}>
{(displayMode === BGTimingTowerDisplayMode.FullLeft ? "*" : "") + "FullLeft"}
- dispatch(setTimingTowerDisplayMode(BGTimingTowerDisplayMode.Hidden))}>
- {(displayMode === BGTimingTowerDisplayMode.Hidden ? "*" : "") + "Hidden"}
-
diff --git a/src/features/event/eventSlice.ts b/src/features/event/eventSlice.ts
index 79ab73c..ba70a86 100644
--- a/src/features/event/eventSlice.ts
+++ b/src/features/event/eventSlice.ts
@@ -13,11 +13,13 @@ import {
SafetyCarStatus,
VirtualSafetyCarStatus,
GridSpot,
+ EventStatus,
+ Car,
} from "../../types/state";
import { Meters } from "../../types/util";
import { orMatch } from "../../utils/common";
import { cloneDriver, getTeam } from "../../utils/dataLookup";
-import { driverToGridMap } from "../../utils/event";
+import { driverToGridMap, sortCarPosition } from "../../utils/event";
const DEFAULT_TYRE: Tyre = {
compound: TyreCompound.Medium,
@@ -31,20 +33,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,
@@ -207,6 +210,7 @@ const initialState: RootState["event"] = {
driver: cloneDriver("mazepin"),
},
],
+ sortedGridSpots: Array.from(Array(20).keys()),
lastUpdate: Date.now(),
};
@@ -232,35 +236,45 @@ export const eventSlice = createSlice({
reducers: {
setCourseFlag: (state, action: PayloadAction) => {
state.courseStatus.courseFlag = action.payload;
+
+ state.lastUpdate = Date.now();
+ },
+ setEventStatus: (state, action: PayloadAction) => {
+ state.eventStatus = action.payload;
+
state.lastUpdate = Date.now();
},
refreshRunningOrder: (state) => {
- const sortingGrid = state.grid.map((car, index) => ({
+ const sortingGrid = state.grid.map((car, index): Car & {
+ gridSpot: GridSpot,
+ } => ({
gridSpot: index,
- distance: car.distance,
- status: car.status,
+ ...car,
}));
- const add = (status: CarStatus) => {
- if (status === CarStatus.Retired) {
- return 1000000;
- } else if (status === CarStatus.DidNotStart) {
- return 2000000;
- }
- return 0;
- };
+ sortingGrid.sort(sortCarPosition);
+ state.sortedGridSpots = sortingGrid.map(car => car.gridSpot);
- 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)),
- );
+ 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();
},
@@ -284,11 +298,13 @@ export const eventSlice = createSlice({
addedDistance *= random(1 - randomness, 1 + randomness);
}
if (usePerformance) {
+ addedDistance *= 1 - (car.position * 0.005);
addedDistance *=
getTeam(car.driver.team.id).performance + car.driver.performance;
}
state.grid[gridMap[car.driver.id]].distance += addedDistance;
});
+
state.lastUpdate = Date.now();
},
awardFastestLap: (state, action: PayloadAction) => {
@@ -305,6 +321,7 @@ export const eventSlice = createSlice({
state.grid[index].notices.splice(fastestLapNoticeIndex, 1);
}
});
+
state.lastUpdate = Date.now();
},
setCarStatus: (state, action: PayloadAction) => {
@@ -314,6 +331,7 @@ export const eventSlice = createSlice({
const car = state.grid[gridSpot];
state.grid[gridMap[car.driver.id]].status = status;
});
+
state.lastUpdate = Date.now();
},
},
@@ -321,6 +339,7 @@ export const eventSlice = createSlice({
export const {
setCourseFlag,
+ setEventStatus,
refreshRunningOrder,
increaseDistance,
awardFastestLap,
diff --git a/src/types/state.ts b/src/types/state.ts
index fbbf7a2..68118a0 100644
--- a/src/types/state.ts
+++ b/src/types/state.ts
@@ -71,11 +71,13 @@ 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;
+ sortedGridSpots: GridSpot[];
lastUpdate: number;
}
export interface EventProgress {
@@ -84,6 +86,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,
diff --git a/src/utils/event.ts b/src/utils/event.ts
index 18cb8d6..439ce95 100644
--- a/src/utils/event.ts
+++ b/src/utils/event.ts
@@ -1,4 +1,4 @@
-import { Car, Grid } from "../types/state";
+import { Car, CarStatus, Grid } from "../types/state";
import { Meters } from "../types/util";
@@ -41,3 +41,19 @@ export function driverToGridMap(grid: Grid) {
});
return gridMap;
}
+
+
+// Adds distance to the cars that are out of the race.
+function outPosition(status: CarStatus) {
+ if (status === CarStatus.Retired) {
+ return -1000000;
+ } else if (status === CarStatus.DidNotStart) {
+ return -2000000;
+ }
+ return 0;
+}
+
+export function sortCarPosition(a: Car, b: Car): number {
+ return (b.distance + outPosition(b.status))
+ - (a.distance + outPosition(a.status));
+}
diff --git a/yarn.lock b/yarn.lock
index b202fd9..cbfc9cd 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -8105,6 +8105,14 @@ rollup@^2.43.1:
optionalDependencies:
fsevents "~2.3.2"
+rooks@^5.10.3:
+ version "5.10.3"
+ resolved "https://registry.yarnpkg.com/rooks/-/rooks-5.10.3.tgz#d302404226de16ed7d451cee3de0f73801745084"
+ integrity sha512-Xnu6WM6bND67jIAaukEXu+US+mQ8JZMCF7db2v987qzprGZSjOUUDrOuMrWpi/0JXQtQl2fKTseBUtAS+pNxZQ==
+ dependencies:
+ lodash.debounce "^4.0.8"
+ raf "^3.4.1"
+
run-parallel@^1.1.9:
version "1.2.0"
resolved "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz"