def _remove_front_line_with(
     self, connection: ControlPoint, events: GameUpdateEvents
 ) -> None:
     front = self.front_lines[connection]
     del self.front_lines[connection]
     del connection.front_lines[self]
     self.front_line_db.remove(front.id)
     events.delete_front_line(front)
 def _create_front_line_with(
     self, connection: ControlPoint, events: GameUpdateEvents
 ) -> None:
     blue, red = FrontLine.sort_control_points(self, connection)
     front = FrontLine(blue, red)
     self.front_lines[connection] = front
     connection.front_lines[self] = front
     self.front_line_db.add(front.id, front)
     events.new_front_line(front)
Example #3
0
    def change_map_package_selection(self, index: QModelIndex) -> None:
        if not index.isValid():
            EventStream.put_nowait(GameUpdateEvents().deselect_flight())
            return

        package = self.ato_model.get_package_model(index)
        if package.rowCount() == 0:
            EventStream.put_nowait(GameUpdateEvents().deselect_flight())
        else:
            EventStream.put_nowait(GameUpdateEvents().select_flight(
                package.flight_at_index(package.index(0))))
Example #4
0
 def cheat_capture(self) -> None:
     events = GameUpdateEvents()
     self.cp.capture(self.game_model.game, events, for_player=not self.cp.captured)
     # Reinitialized ground planners and the like. The ATO needs to be reset because
     # missions planned against the flipped base are no longer valid.
     self.game_model.game.initialize_turn(events)
     EventStream.put_nowait(events)
     GameUpdateSignal.get_instance().updateGame(self.game_model.game)
    def capture(self, game: Game, events: GameUpdateEvents, for_player: bool) -> None:
        new_coalition = game.coalition_for(for_player)
        self.ground_unit_orders.refund_all(self.coalition)
        self.retreat_ground_units(game)
        self.retreat_air_units(game)
        self.depopulate_uncapturable_tgos()

        # All the attached TGOs have either been depopulated or captured. Tell the UI to
        # update their state.
        for tgo in self.connected_objectives:
            events.update_tgo(tgo)

        self._coalition = new_coalition
        self.base.set_strength_to_minimum()
        self._clear_front_lines(events)
        self._create_missing_front_lines(events)
        events.update_control_point(self)
Example #6
0
 def change_map_flight_selection(self, index: QModelIndex) -> None:
     events = GameUpdateEvents()
     if not index.isValid():
         events.deselect_flight()
     else:
         events.select_flight(self.package_model.flight_at_index(index))
     EventStream.put_nowait(events)
Example #7
0
 def run_in_thread(self) -> Iterator[None]:
     # This relies on undocumented behavior, but it is what the developer recommends:
     # https://github.com/encode/uvicorn/issues/742
     thread = Thread(target=self.run)
     thread.start()
     try:
         while not self.started:
             time.sleep(1e-3)
         yield
     finally:
         self.should_exit = True
         EventStream.put_nowait(GameUpdateEvents().shut_down())
         thread.join()
Example #8
0
 def add_flight(self, flight: Flight) -> None:
     """Adds the new flight to the package."""
     self.package_model.add_flight(flight)
     planner = FlightPlanBuilder(self.package_model.package, self.game.blue,
                                 self.game.theater)
     try:
         planner.populate_flight_plan(flight)
         self.package_model.update_tot()
         EventStream.put_nowait(GameUpdateEvents().new_flight(flight))
     except PlanningError as ex:
         self.package_model.delete_flight(flight)
         logging.exception("Could not create flight")
         QMessageBox.critical(self, "Could not create flight", str(ex),
                              QMessageBox.Ok)
     # noinspection PyUnresolvedReferences
     self.package_changed.emit()
Example #9
0
def set_position(
        flight_id: UUID,
        waypoint_idx: int,
        position: LeafletPoint,
        game: Game = Depends(GameContext.require),
) -> None:
    from game.server import EventStream

    flight = game.db.flights.get(flight_id)
    if waypoint_idx == 0:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

    waypoint = flight.flight_plan.waypoints[waypoint_idx - 1]
    waypoint.position = Point.from_latlng(LatLng(position.lat, position.lng),
                                          game.theater.terrain)
    package_model = (GameContext.get_model().ato_model_for(
        flight.blue).find_matching_package_model(flight.package))
    if package_model is None:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=f"Could not find PackageModel owning {flight}",
        )
    package_model.update_tot()
    EventStream.put_nowait(GameUpdateEvents().update_flight(flight))
Example #10
0
 def event_context() -> Iterator[GameUpdateEvents]:
     events = GameUpdateEvents()
     yield events
     EventStream.put_nowait(events)
Example #11
0
 def kill(self, events: GameUpdateEvents) -> None:
     self.alive = False
     self.ground_object.invalidate_threat_poly()
     events.update_tgo(self.ground_object)
Example #12
0
 def on_close(self, _result) -> None:
     EventStream.put_nowait(GameUpdateEvents().update_flights_in_package(
         self.package_model.package))
Example #13
0
    def initialize_turn_0(self) -> None:
        # We don't need to send events for turn 0. The UI isn't up yet, and it'll fetch
        # the entire game state when it comes up.
        from game.sim import GameUpdateEvents

        self._create_missing_front_lines(GameUpdateEvents())
Example #14
0
def on_game_load(game: Game | None) -> None:
    EventStream.drain()
    EventStream.put_nowait(GameUpdateEvents().game_loaded(game))