From d6e82d44fcbe4c21b32f0dc605cd547f9e92c174 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 25 Feb 2022 16:34:11 -0800 Subject: [PATCH] Fix FastAPI doc pages. We can't directly use frozen dataclasses from pydcs in our interface because pydantic can't process them. Pydantic is able to automatically convert to our modelview type from the pydcs type though. --- game/server/combat/models.py | 7 +++---- game/server/flights/models.py | 4 ++-- game/server/leaflet.py | 9 +++++++++ game/server/mapzones/models.py | 5 ++--- game/server/waypoints/models.py | 9 ++++----- game/server/waypoints/routes.py | 7 +++++-- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/game/server/combat/models.py b/game/server/combat/models.py index 215b08b1..092a4e21 100644 --- a/game/server/combat/models.py +++ b/game/server/combat/models.py @@ -2,10 +2,9 @@ from __future__ import annotations from uuid import UUID -from dcs.mapping import LatLng from pydantic import BaseModel -from game.server.leaflet import LeafletPoly, ShapelyUtil +from game.server.leaflet import LeafletPoint, LeafletPoly, ShapelyUtil from game.sim.combat import FrozenCombat from game.sim.combat.aircombat import AirCombat from game.sim.combat.atip import AtIp @@ -15,8 +14,8 @@ from game.theater import ConflictTheater class FrozenCombatJs(BaseModel): id: UUID - flight_position: LatLng | None - target_positions: list[LatLng] | None + flight_position: LeafletPoint | None + target_positions: list[LeafletPoint] | None footprint: list[LeafletPoly] | None @staticmethod diff --git a/game/server/flights/models.py b/game/server/flights/models.py index 91e3248d..fa7a2df7 100644 --- a/game/server/flights/models.py +++ b/game/server/flights/models.py @@ -2,17 +2,17 @@ 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 +from game.server.leaflet import LeafletPoint class FlightJs(BaseModel): id: UUID blue: bool - position: LatLng | None + position: LeafletPoint | None @staticmethod def for_flight(flight: Flight) -> FlightJs: diff --git a/game/server/leaflet.py b/game/server/leaflet.py index ed6005cf..9ec58543 100644 --- a/game/server/leaflet.py +++ b/game/server/leaflet.py @@ -3,6 +3,7 @@ from __future__ import annotations from typing import Union from dcs import Point +from pydantic import BaseModel from shapely.geometry import LineString, MultiLineString, MultiPolygon, Polygon from game.theater import ConflictTheater @@ -11,6 +12,14 @@ LeafletLatLon = list[float] LeafletPoly = list[LeafletLatLon] +class LeafletPoint(BaseModel): + lat: float + lng: float + + class Config: + orm_mode = True + + class ShapelyUtil: @staticmethod def poly_to_leaflet(poly: Polygon, theater: ConflictTheater) -> LeafletPoly: diff --git a/game/server/mapzones/models.py b/game/server/mapzones/models.py index 8aef605c..25789305 100644 --- a/game/server/mapzones/models.py +++ b/game/server/mapzones/models.py @@ -1,9 +1,8 @@ from __future__ import annotations -from dcs.mapping import LatLng from pydantic import BaseModel -from game.server.leaflet import LeafletPoly, ShapelyUtil +from game.server.leaflet import LeafletPoint, LeafletPoly, ShapelyUtil from game.theater import ConflictTheater from game.threatzones import ThreatZones @@ -15,7 +14,7 @@ class MapZonesJs(BaseModel): class UnculledZoneJs(BaseModel): - position: LatLng + position: LeafletPoint radius: float diff --git a/game/server/waypoints/models.py b/game/server/waypoints/models.py index ac1a1194..d732c248 100644 --- a/game/server/waypoints/models.py +++ b/game/server/waypoints/models.py @@ -1,16 +1,15 @@ from __future__ import annotations -from dcs.mapping import LatLng -from pydantic.dataclasses import dataclass +from pydantic import BaseModel from game.ato import FlightWaypoint from game.ato.flightwaypointtype import FlightWaypointType +from game.server.leaflet import LeafletPoint -@dataclass -class FlightWaypointJs: +class FlightWaypointJs(BaseModel): name: str - position: LatLng + position: LeafletPoint altitude_ft: float altitude_reference: str is_movable: bool diff --git a/game/server/waypoints/routes.py b/game/server/waypoints/routes.py index 51128eb7..91a758e4 100644 --- a/game/server/waypoints/routes.py +++ b/game/server/waypoints/routes.py @@ -8,6 +8,7 @@ from game import Game from game.ato.flightwaypoint import FlightWaypoint from game.ato.flightwaypointtype import FlightWaypointType from game.server import GameContext +from game.server.leaflet import LeafletPoint from game.server.waypoints.models import FlightWaypointJs from game.utils import meters @@ -37,7 +38,7 @@ def all_waypoints_for_flight( def set_position( flight_id: UUID, waypoint_idx: int, - position: LatLng, + position: LeafletPoint, game: Game = Depends(GameContext.get), ) -> None: flight = game.db.flights.get(flight_id) @@ -45,7 +46,9 @@ def set_position( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) waypoint = flight.flight_plan.waypoints[waypoint_idx - 1] - waypoint.position = Point.from_latlng(position, game.theater.terrain) + waypoint.position = Point.from_latlng( + LatLng(position.lat, position.lng), game.theater.terrain + ) package_model = ( GameContext.get_model() .ato_model_for(flight.blue)