ISSUE-075 - Switched to using GridSpots for the SelectedCars state.
Chyrons still show out-of-date information, though. Might need to do some refactoring to get this straightened.
This commit is contained in:
1 parent
bf89bc96dc
commit
d9b30f64f2
6 files changed
+44
-50
No files matched your search
@@ -1,9 +1,7 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { findIndex } from "lodash";
|
||||
|
||||
import { RootState, Car } from "../../../types/state";
|
||||
import { RootState, GridSpot } from "../../../types/state";
|
||||
import { Optional } from "../../../types/util";
|
||||
import { carMatch } from "../../../utils/comparators";
|
||||
|
||||
const initialState: RootState["broadcastDirector"] = {
|
||||
selectedCars: [],
|
||||
@@ -13,22 +11,16 @@ export const broadcastDirectorSlice = createSlice({
|
||||
name: "broadcastDirector",
|
||||
initialState,
|
||||
reducers: {
|
||||
pushSelectedCars: (state, action: PayloadAction<Car>) => {
|
||||
const index = findIndex(state.selectedCars, carMatch(action.payload));
|
||||
if (index === -1) {
|
||||
state.selectedCars = state.selectedCars.concat(action.payload);
|
||||
pushSelectedCars: (state, action: PayloadAction<GridSpot>) => {
|
||||
if (!state.selectedCars.includes(action.payload)) {
|
||||
state.selectedCars.push(action.payload);
|
||||
}
|
||||
},
|
||||
setSelectedCars: (state, action: PayloadAction<Car>) => { // Might be removed.
|
||||
setSelectedCars: (state, action: PayloadAction<GridSpot>) => { // Might be removed.
|
||||
state.selectedCars = [action.payload];
|
||||
},
|
||||
removeSelectedCars: (state, action: PayloadAction<Car | number>) => {
|
||||
const index = typeof action.payload === "number"
|
||||
? action.payload
|
||||
: findIndex(state.selectedCars, carMatch(action.payload));
|
||||
state.selectedCars = state.selectedCars.slice(0, index).concat(
|
||||
state.selectedCars.slice(index + 1),
|
||||
);
|
||||
removeSelectedCars: (state, action: PayloadAction<GridSpot>) => {
|
||||
state.selectedCars = state.selectedCars.filter(grid => grid !== action.payload);
|
||||
},
|
||||
/**
|
||||
* Shortcut to clear out selected cars. There is also a trick - if you
|
||||
@@ -36,7 +28,7 @@ export const broadcastDirectorSlice = createSlice({
|
||||
* car.
|
||||
* @param action Car (optional)
|
||||
*/
|
||||
clearSelectedCars: (state, action: PayloadAction<Optional<Car>>) => {
|
||||
clearSelectedCars: (state, action: PayloadAction<Optional<GridSpot>>) => {
|
||||
if (action.payload == null) {
|
||||
state.selectedCars = [];
|
||||
} else {
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
SectorFlag,
|
||||
SafetyCarStatus,
|
||||
VirtualSafetyCarStatus,
|
||||
Car,
|
||||
GridSpot,
|
||||
} from "../../types/state";
|
||||
import { Meters } from "../../types/util";
|
||||
import { orMatch } from "../../utils/common";
|
||||
@@ -211,14 +211,14 @@ const initialState: RootState["event"] = {
|
||||
};
|
||||
|
||||
interface IncreaseDistanceAction {
|
||||
cars: Car[];
|
||||
gridSpots: GridSpot[];
|
||||
distance: Meters;
|
||||
randomness: number;
|
||||
usePerformance: boolean;
|
||||
}
|
||||
|
||||
interface SetCarStatusAction {
|
||||
cars: Car[];
|
||||
gridSpots: GridSpot[];
|
||||
status: CarStatus;
|
||||
}
|
||||
|
||||
@@ -267,9 +267,11 @@ export const eventSlice = createSlice({
|
||||
increaseDistance: (state, action: PayloadAction<IncreaseDistanceAction>) => {
|
||||
const gridMap = driverToGridMap(state.grid);
|
||||
|
||||
const { cars, distance, randomness, usePerformance } = action.payload;
|
||||
const { gridSpots, distance, randomness, usePerformance } = action.payload;
|
||||
|
||||
gridSpots.forEach(gridSpot => {
|
||||
const car = state.grid[gridSpot];
|
||||
|
||||
cars.forEach(car => {
|
||||
// Do not update cars that are DNS, retired, or finished.
|
||||
// This does remind me that Finished cars must be treated and reported
|
||||
// differently...
|
||||
@@ -289,13 +291,13 @@ export const eventSlice = createSlice({
|
||||
});
|
||||
state.lastUpdate = Date.now();
|
||||
},
|
||||
awardFastestLap: (state, action: PayloadAction<Car>) => {
|
||||
awardFastestLap: (state, action: PayloadAction<GridSpot>) => {
|
||||
// Awards fastest lap to provided car. Removes fastest lap from anyone else.
|
||||
state.grid.forEach((car, index) => {
|
||||
const fastestLapNoticeIndex = car.notices.findIndex(
|
||||
notice => notice === CarNotice.FastestLap,
|
||||
);
|
||||
if (car.driver.id === action.payload.driver.id) {
|
||||
if (car.driver.id === state.grid[action.payload].driver.id) {
|
||||
if (fastestLapNoticeIndex === -1) {
|
||||
state.grid[index].notices.push(CarNotice.FastestLap);
|
||||
}
|
||||
@@ -307,8 +309,9 @@ export const eventSlice = createSlice({
|
||||
},
|
||||
setCarStatus: (state, action: PayloadAction<SetCarStatusAction>) => {
|
||||
const gridMap = driverToGridMap(state.grid);
|
||||
const { cars, status } = action.payload;
|
||||
cars.forEach(car => {
|
||||
const { gridSpots, status } = action.payload;
|
||||
gridSpots.forEach(gridSpot => {
|
||||
const car = state.grid[gridSpot];
|
||||
state.grid[gridMap[car.driver.id]].status = status;
|
||||
});
|
||||
state.lastUpdate = Date.now();
|
||||
|
||||
Reference in new issue
Block a user