mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
https://github.com/dcs-liberation/dcs_liberation/issues/2039 Partial fix for https://github.com/dcs-liberation/dcs_liberation/issues/2045 (now works in the new map, old one not fixed yet).
82 lines
2.1 KiB
TypeScript
82 lines
2.1 KiB
TypeScript
import { RootState } from "../app/store";
|
|
import { gameLoaded, gameUnloaded } from "./actions";
|
|
import { Flight } from "./flight";
|
|
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
|
|
import { LatLng } from "leaflet";
|
|
|
|
interface FlightsState {
|
|
flights: { [id: string]: Flight };
|
|
selected: Flight | null;
|
|
}
|
|
|
|
const initialState: FlightsState = {
|
|
flights: {},
|
|
selected: null,
|
|
};
|
|
|
|
export const flightsSlice = createSlice({
|
|
name: "flights",
|
|
initialState,
|
|
reducers: {
|
|
registerFlight: (state, action: PayloadAction<Flight>) => {
|
|
const flight = action.payload;
|
|
if (flight.id in state.flights) {
|
|
console.log(`Overriding flight with ID: ${flight.id}`);
|
|
}
|
|
state.flights[flight.id] = flight;
|
|
},
|
|
unregisterFlight: (state, action: PayloadAction<string>) => {
|
|
const id = action.payload;
|
|
delete state.flights[id];
|
|
},
|
|
updateFlight: (state, action: PayloadAction<Flight>) => {
|
|
const flight = action.payload;
|
|
state.flights[flight.id] = flight;
|
|
},
|
|
deselectFlight: (state) => {
|
|
state.selected = null;
|
|
},
|
|
selectFlight: (state, action: PayloadAction<string>) => {
|
|
const id = action.payload;
|
|
state.selected = state.flights[id];
|
|
},
|
|
updateFlightPositions: (
|
|
state,
|
|
action: PayloadAction<[string, LatLng][]>
|
|
) => {
|
|
for (const [id, position] of action.payload) {
|
|
state.flights[id].position = position;
|
|
}
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(gameLoaded, (state, action) => {
|
|
state.selected = null;
|
|
state.flights = action.payload.flights.reduce(
|
|
(acc: { [key: string]: Flight }, curr) => {
|
|
acc[curr.id] = curr;
|
|
return acc;
|
|
},
|
|
{}
|
|
);
|
|
});
|
|
builder.addCase(gameUnloaded, (state) => {
|
|
state.selected = null;
|
|
state.flights = {};
|
|
});
|
|
},
|
|
});
|
|
|
|
export const {
|
|
registerFlight,
|
|
unregisterFlight,
|
|
updateFlight,
|
|
deselectFlight,
|
|
selectFlight,
|
|
updateFlightPositions,
|
|
} = flightsSlice.actions;
|
|
|
|
export const selectFlights = (state: RootState) => state.flights;
|
|
|
|
export default flightsSlice.reducer;
|