Reset game state on new turn.

This may not be the way to do this long term, but it is how the old map
works so it's at least not a regression. It might be better to generate
events for the between-turn changes in state instead.

https://github.com/dcs-liberation/dcs_liberation/issues/2039
This commit is contained in:
Dan Albert
2022-03-06 00:02:25 -08:00
parent 73fcfcec7b
commit 738cf1f381
7 changed files with 26 additions and 4 deletions

View File

@@ -45,6 +45,7 @@ interface GameUpdateEvents {
updated_control_points: number[];
reset_on_map_center: LatLng | null;
game_unloaded: boolean;
new_turn: boolean;
}
export const handleStreamedEvents = (
@@ -126,4 +127,8 @@ export const handleStreamedEvents = (
if (events.game_unloaded) {
dispatch(gameUnloaded());
}
if (events.new_turn) {
reloadGameState(dispatch, true);
}
};

View File

@@ -11,5 +11,5 @@ export default interface Game {
supply_routes: SupplyRoute[];
front_lines: FrontLine[];
flights: Flight[];
map_center: LatLngLiteral;
map_center: LatLngLiteral | null;
}

View File

@@ -3,7 +3,10 @@ import { gameLoaded, gameUnloaded } from "./actions";
import backend from "./backend";
import Game from "./game";
export default function reloadGameState(dispatch: AppDispatch) {
export default function reloadGameState(
dispatch: AppDispatch,
ignoreRecenter: boolean = false
) {
backend
.get("/game")
.catch((error) => console.log(`Error fetching game state: ${error}`))
@@ -12,6 +15,10 @@ export default function reloadGameState(dispatch: AppDispatch) {
dispatch(gameUnloaded());
return;
}
dispatch(gameLoaded(response.data as Game));
const game = response.data as Game;
if (ignoreRecenter) {
game.map_center = null;
}
dispatch(gameLoaded(game));
});
}

View File

@@ -17,7 +17,9 @@ const mapSlice = createSlice({
reducers: {},
extraReducers: (builder) => {
builder.addCase(gameLoaded, (state, action) => {
state.center = action.payload.map_center;
if (action.payload.map_center != null) {
state.center = action.payload.map_center;
}
});
builder.addCase(gameUnloaded, (state) => {
state.center = { lat: 0, lng: 0 };