mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
https://github.com/dcs-liberation/dcs_liberation/issues/2253 https://github.com/dcs-liberation/dcs_liberation/issues/2263
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { RootState } from "../app/store";
|
|
import { gameLoaded, gameUnloaded } from "./actions";
|
|
import { ThreatZoneContainer, ThreatZones } from "./liberationApi";
|
|
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
|
|
|
|
interface ThreatZonesState {
|
|
zones: ThreatZoneContainer;
|
|
}
|
|
|
|
const initialState: ThreatZonesState = {
|
|
zones: {
|
|
blue: {
|
|
full: [],
|
|
aircraft: [],
|
|
air_defenses: [],
|
|
radar_sams: [],
|
|
},
|
|
red: {
|
|
full: [],
|
|
aircraft: [],
|
|
air_defenses: [],
|
|
radar_sams: [],
|
|
},
|
|
},
|
|
};
|
|
|
|
export interface IThreatZoneUpdate {
|
|
blue: boolean;
|
|
zones: ThreatZones;
|
|
}
|
|
|
|
export const threatZonesSlice = createSlice({
|
|
name: "threatZonesState",
|
|
initialState,
|
|
reducers: {
|
|
updated: (state, action: PayloadAction<IThreatZoneUpdate[]>) => {
|
|
for (const [blue, zones] of Object.entries(action.payload)) {
|
|
const data = {blue: (blue === "true"), zones: zones} as unknown as IThreatZoneUpdate
|
|
if (data.blue) {
|
|
state.zones.blue = data.zones;
|
|
} else {
|
|
state.zones.red = data.zones;
|
|
}
|
|
}
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(gameLoaded, (state, action) => {
|
|
state.zones = action.payload.threat_zones;
|
|
});
|
|
builder.addCase(gameUnloaded, (state) => {
|
|
state.zones = initialState.zones;
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { updated: threatZonesUpdated } = threatZonesSlice.actions;
|
|
|
|
export const selectThreatZones = (state: RootState) => state.threatZones;
|
|
|
|
export default threatZonesSlice.reducer;
|