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

View File

@ -11,5 +11,5 @@ export default interface Game {
supply_routes: SupplyRoute[]; supply_routes: SupplyRoute[];
front_lines: FrontLine[]; front_lines: FrontLine[];
flights: Flight[]; 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 backend from "./backend";
import Game from "./game"; import Game from "./game";
export default function reloadGameState(dispatch: AppDispatch) { export default function reloadGameState(
dispatch: AppDispatch,
ignoreRecenter: boolean = false
) {
backend backend
.get("/game") .get("/game")
.catch((error) => console.log(`Error fetching game state: ${error}`)) .catch((error) => console.log(`Error fetching game state: ${error}`))
@ -12,6 +15,10 @@ export default function reloadGameState(dispatch: AppDispatch) {
dispatch(gameUnloaded()); dispatch(gameUnloaded());
return; 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: {}, reducers: {},
extraReducers: (builder) => { extraReducers: (builder) => {
builder.addCase(gameLoaded, (state, action) => { 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) => { builder.addCase(gameUnloaded, (state) => {
state.center = { lat: 0, lng: 0 }; state.center = { lat: 0, lng: 0 };

View File

@ -307,6 +307,7 @@ class Game:
with logged_duration("Turn initialization"): with logged_duration("Turn initialization"):
self.initialize_turn(events) self.initialize_turn(events)
events.begin_new_turn()
EventStream.put_nowait(events) EventStream.put_nowait(events)
# Autosave progress # Autosave progress

View File

@ -35,6 +35,7 @@ class GameUpdateEventsJs(BaseModel):
updated_control_points: set[int] updated_control_points: set[int]
reset_on_map_center: LeafletLatLon | None reset_on_map_center: LeafletLatLon | None
game_unloaded: bool game_unloaded: bool
new_turn: bool
@classmethod @classmethod
def from_events( def from_events(
@ -85,4 +86,5 @@ class GameUpdateEventsJs(BaseModel):
updated_control_points=events.updated_control_points, updated_control_points=events.updated_control_points,
reset_on_map_center=recenter_map, reset_on_map_center=recenter_map,
game_unloaded=events.game_unloaded, game_unloaded=events.game_unloaded,
new_turn=events.new_turn,
) )

View File

@ -36,6 +36,7 @@ class GameUpdateEvents:
updated_control_points: set[int] = field(default_factory=set) updated_control_points: set[int] = field(default_factory=set)
reset_on_map_center: LatLng | None = None reset_on_map_center: LatLng | None = None
game_unloaded: bool = False game_unloaded: bool = False
new_turn: bool = False
shutting_down: bool = False shutting_down: bool = False
@property @property
@ -136,6 +137,10 @@ class GameUpdateEvents:
self.game_unloaded = False self.game_unloaded = False
return self return self
def begin_new_turn(self) -> GameUpdateEvents:
self.new_turn = True
return self
def shut_down(self) -> GameUpdateEvents: def shut_down(self) -> GameUpdateEvents:
self.shutting_down = True self.shutting_down = True
return self return self