Add FastAPI interface between the game and map.

A possible explanation for the infrequent CTDs we've been seeing since
adding fast forward is that QWebChannel doesn't keep a reference to the
python objects that it passes to js, so if the object is GC'd before the
front end is done with it, it crashes.

We don't really like QWebChannel anyway, so this begins replacing that
with FastAPI.
This commit is contained in:
Dan Albert
2022-02-13 14:22:05 -08:00
parent 1df31b2496
commit 350f08be2f
33 changed files with 419 additions and 330 deletions

View File

@@ -0,0 +1 @@
from .routes import router

View File

@@ -0,0 +1,126 @@
from __future__ import annotations
from pydantic import BaseModel, Field
from game import Game
from game.ato import Flight
from game.flightplan import HoldZoneGeometry, IpZoneGeometry, JoinZoneGeometry
from ..leaflet import LeafletPoly, ShapelyUtil
class HoldZonesJs(BaseModel):
home_bubble: LeafletPoly = Field(alias="homeBubble")
target_bubble: LeafletPoly = Field(alias="targetBubble")
join_bubble: LeafletPoly = Field(alias="joinBubble")
excluded_zones: list[LeafletPoly] = Field(alias="excludedZones")
permissible_zones: list[LeafletPoly] = Field(alias="permissibleZones")
preferred_lines: list[LeafletPoly] = Field(alias="preferredLines")
@classmethod
def empty(cls) -> HoldZonesJs:
return HoldZonesJs(
homeBubble=[],
targetBubble=[],
joinBubble=[],
excludedZones=[],
permissibleZones=[],
preferredLines=[],
)
@classmethod
def for_flight(cls, flight: Flight, game: Game) -> HoldZonesJs:
target = flight.package.target
home = flight.departure
if flight.package.waypoints is None:
return HoldZonesJs.empty()
ip = flight.package.waypoints.ingress
join = flight.package.waypoints.join
geometry = HoldZoneGeometry(
target.position, home.position, ip, join, game.blue, game.theater
)
return HoldZonesJs(
homeBubble=ShapelyUtil.poly_to_leaflet(geometry.home_bubble, game.theater),
targetBubble=ShapelyUtil.poly_to_leaflet(
geometry.target_bubble, game.theater
),
joinBubble=ShapelyUtil.poly_to_leaflet(geometry.join_bubble, game.theater),
excludedZones=ShapelyUtil.polys_to_leaflet(
geometry.excluded_zones, game.theater
),
permissibleZones=ShapelyUtil.polys_to_leaflet(
geometry.permissible_zones, game.theater
),
preferredLines=ShapelyUtil.lines_to_leaflet(
geometry.preferred_lines, game.theater
),
)
class IpZonesJs(BaseModel):
home_bubble: LeafletPoly = Field(alias="homeBubble")
ipBubble: LeafletPoly = Field(alias="ipBubble")
permissibleZone: LeafletPoly = Field(alias="permissibleZone")
safeZones: list[LeafletPoly] = Field(alias="safeZones")
@classmethod
def empty(cls) -> IpZonesJs:
return IpZonesJs(homeBubble=[], ipBubble=[], permissibleZone=[], safeZones=[])
@classmethod
def for_flight(cls, flight: Flight, game: Game) -> IpZonesJs:
target = flight.package.target
home = flight.departure
geometry = IpZoneGeometry(target.position, home.position, game.blue)
return IpZonesJs(
homeBubble=ShapelyUtil.poly_to_leaflet(geometry.home_bubble, game.theater),
ipBubble=ShapelyUtil.poly_to_leaflet(geometry.ip_bubble, game.theater),
permissibleZone=ShapelyUtil.poly_to_leaflet(
geometry.permissible_zone, game.theater
),
safeZones=ShapelyUtil.polys_to_leaflet(geometry.safe_zones, game.theater),
)
class JoinZonesJs(BaseModel):
home_bubble: LeafletPoly = Field(alias="homeBubble")
target_bubble: LeafletPoly = Field(alias="targetBubble")
ip_bubble: LeafletPoly = Field(alias="ipBubble")
excluded_zones: list[LeafletPoly] = Field(alias="excludedZones")
permissible_zones: list[LeafletPoly] = Field(alias="permissibleZones")
preferred_lines: list[LeafletPoly] = Field(alias="preferredLines")
@classmethod
def empty(cls) -> JoinZonesJs:
return JoinZonesJs(
homeBubble=[],
targetBubble=[],
ipBubble=[],
excludedZones=[],
permissibleZones=[],
preferredLines=[],
)
@classmethod
def for_flight(cls, flight: Flight, game: Game) -> JoinZonesJs:
target = flight.package.target
home = flight.departure
if flight.package.waypoints is None:
return JoinZonesJs.empty()
ip = flight.package.waypoints.ingress
geometry = JoinZoneGeometry(target.position, home.position, ip, game.blue)
return JoinZonesJs(
homeBubble=ShapelyUtil.poly_to_leaflet(geometry.home_bubble, game.theater),
targetBubble=ShapelyUtil.poly_to_leaflet(
geometry.target_bubble, game.theater
),
ipBubble=ShapelyUtil.poly_to_leaflet(geometry.ip_bubble, game.theater),
excludedZones=ShapelyUtil.polys_to_leaflet(
geometry.excluded_zones, game.theater
),
permissibleZones=ShapelyUtil.polys_to_leaflet(
geometry.permissible_zones, game.theater
),
preferredLines=ShapelyUtil.lines_to_leaflet(
geometry.preferred_lines, game.theater
),
)

View File

@@ -0,0 +1,38 @@
from uuid import UUID
from fastapi import APIRouter, Depends
from game import Game
from game.ato import Flight
from game.server import GameContext
from .models import HoldZonesJs, IpZonesJs, JoinZonesJs
router = APIRouter(prefix="/debug/waypoint-geometries")
# TODO: Maintain map of UUID -> Flight in Game.
def find_flight(game: Game, flight_id: UUID) -> Flight:
for coalition in game.coalitions:
for package in coalition.ato.packages:
for flight in package.flights:
if flight.id == flight_id:
return flight
raise KeyError(f"No flight found with ID {flight_id}")
@router.get("/hold/{flight_id}")
def hold_zones(flight_id: UUID, game: Game = Depends(GameContext.get)) -> HoldZonesJs:
flight = find_flight(game, flight_id)
return HoldZonesJs.for_flight(flight, game)
@router.get("/ip/{flight_id}")
def ip_zones(flight_id: UUID, game: Game = Depends(GameContext.get)) -> IpZonesJs:
flight = find_flight(game, flight_id)
return IpZonesJs.for_flight(flight, game)
@router.get("/join/{flight_id}")
def join_zones(flight_id: UUID, game: Game = Depends(GameContext.get)) -> JoinZonesJs:
flight = find_flight(game, flight_id)
return JoinZonesJs.for_flight(flight, game)