Move FlightJs out of MapModel.

This commit is contained in:
Dan Albert
2022-02-22 20:40:58 -08:00
parent ad0d3412fb
commit 45e76e12b6
18 changed files with 333 additions and 326 deletions

View File

@@ -6,6 +6,7 @@ from uuid import UUID
from pydantic import BaseModel
from game.server.combat.models import FrozenCombatJs
from game.server.flights.models import FlightJs
from game.server.leaflet import LeafletLatLon
if TYPE_CHECKING:
@@ -14,18 +15,24 @@ if TYPE_CHECKING:
class GameUpdateEventsJs(BaseModel):
updated_flights: dict[UUID, LeafletLatLon]
new_combats: list[FrozenCombatJs] = []
updated_combats: list[FrozenCombatJs] = []
navmesh_updates: set[bool] = set()
unculled_zones_updated: bool = False
threat_zones_updated: bool = False
updated_flight_positions: dict[UUID, LeafletLatLon]
new_combats: list[FrozenCombatJs]
updated_combats: list[FrozenCombatJs]
navmesh_updates: set[bool]
unculled_zones_updated: bool
threat_zones_updated: bool
new_flights: list[FlightJs]
updated_flights: set[UUID]
deleted_flights: set[UUID]
selected_flight: UUID | None
deselected_flight: bool
@classmethod
def from_events(cls, events: GameUpdateEvents, game: Game) -> GameUpdateEventsJs:
return GameUpdateEventsJs(
updated_flights={
f[0].id: f[1].latlng().as_list() for f in events.updated_flights
updated_flight_positions={
f[0].id: f[1].latlng().as_list()
for f in events.updated_flight_positions
},
new_combats=[
FrozenCombatJs.for_combat(c, game.theater) for c in events.new_combats
@@ -37,4 +44,9 @@ class GameUpdateEventsJs(BaseModel):
navmesh_updates=events.navmesh_updates,
unculled_zones_updated=events.unculled_zones_updated,
threat_zones_updated=events.threat_zones_updated,
new_flights=[FlightJs.for_flight(f) for f in events.new_flights],
updated_flights=events.updated_flights,
deleted_flights=events.deleted_flights,
selected_flight=events.selected_flight,
deselected_flight=events.deselected_flight,
)

View File

@@ -0,0 +1,26 @@
from __future__ import annotations
from uuid import UUID
from dcs.mapping import LatLng
from pydantic import BaseModel
from game.ato import Flight
from game.ato.flightstate import InFlight
class FlightJs(BaseModel):
id: UUID
blue: bool
position: LatLng | None
@staticmethod
def for_flight(flight: Flight) -> FlightJs:
# Don't provide a location for aircraft that aren't in the air. Later we can
# expand the model to include the state data for the UI so that it can make its
# own decisions about whether or not to draw the aircraft, but for now we'll
# filter here.
position = None
if isinstance(flight.state, InFlight):
position = flight.position().latlng()
return FlightJs(id=flight.id, blue=flight.blue, position=position)

View File

@@ -4,13 +4,30 @@ from fastapi import APIRouter, Depends
from shapely.geometry import LineString, Point as ShapelyPoint
from game import Game
from game.server import GameContext
from game.server.leaflet import LeafletPoly, ShapelyUtil
from game.ato.flightplan import CasFlightPlan, PatrollingFlightPlan
from game.server import GameContext
from game.server.flights.models import FlightJs
from game.server.leaflet import LeafletPoly, ShapelyUtil
router: APIRouter = APIRouter(prefix="/flights")
@router.get("/")
def list_flights(game: Game = Depends(GameContext.get)) -> list[FlightJs]:
flights = []
for coalition in game.coalitions:
for package in coalition.ato.packages:
for flight in package.flights:
flights.append(FlightJs.for_flight(flight))
return flights
@router.get("/{flight_id}")
def get_flight(flight_id: UUID, game: Game = Depends(GameContext.get)) -> FlightJs:
flight = game.db.flights.get(flight_id)
return FlightJs.for_flight(flight)
@router.get("/{flight_id}/commit-boundary")
def commit_boundary(
flight_id: UUID, game: Game = Depends(GameContext.get)