mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
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:
2
game/server/__init__.py
Normal file
2
game/server/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .dependencies import GameContext
|
||||
from .server import Server
|
||||
6
game/server/app.py
Normal file
6
game/server/app.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from . import debuggeometries
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(debuggeometries.router)
|
||||
1
game/server/debuggeometries/__init__.py
Normal file
1
game/server/debuggeometries/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .routes import router
|
||||
126
game/server/debuggeometries/models.py
Normal file
126
game/server/debuggeometries/models.py
Normal 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
|
||||
),
|
||||
)
|
||||
38
game/server/debuggeometries/routes.py
Normal file
38
game/server/debuggeometries/routes.py
Normal 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)
|
||||
15
game/server/dependencies.py
Normal file
15
game/server/dependencies.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from game import Game
|
||||
|
||||
|
||||
class GameContext:
|
||||
_game: Game | None
|
||||
|
||||
@classmethod
|
||||
def set(cls, game: Game | None) -> None:
|
||||
cls._game = game
|
||||
|
||||
@classmethod
|
||||
def get(cls) -> Game:
|
||||
if cls._game is None:
|
||||
raise RuntimeError("GameContext has no Game set")
|
||||
return cls._game
|
||||
47
game/server/leaflet.py
Normal file
47
game/server/leaflet.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Union
|
||||
|
||||
from dcs import Point
|
||||
from shapely.geometry import LineString, MultiLineString, MultiPolygon, Polygon
|
||||
|
||||
from game.theater import ConflictTheater
|
||||
|
||||
LeafletLatLon = list[float]
|
||||
LeafletPoly = list[LeafletLatLon]
|
||||
|
||||
|
||||
class ShapelyUtil:
|
||||
@staticmethod
|
||||
def poly_to_leaflet(poly: Polygon, theater: ConflictTheater) -> LeafletPoly:
|
||||
if poly.is_empty:
|
||||
return []
|
||||
return [
|
||||
theater.point_to_ll(Point(x, y)).as_list() for x, y in poly.exterior.coords
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def polys_to_leaflet(
|
||||
cls, poly: Union[Polygon, MultiPolygon], theater: ConflictTheater
|
||||
) -> list[LeafletPoly]:
|
||||
if isinstance(poly, MultiPolygon):
|
||||
polys = poly.geoms
|
||||
else:
|
||||
polys = [poly]
|
||||
return [cls.poly_to_leaflet(poly, theater) for poly in polys]
|
||||
|
||||
@staticmethod
|
||||
def line_to_leaflet(
|
||||
line: LineString, theater: ConflictTheater
|
||||
) -> list[LeafletLatLon]:
|
||||
return [theater.point_to_ll(Point(x, y)).as_list() for x, y in line.coords]
|
||||
|
||||
@classmethod
|
||||
def lines_to_leaflet(
|
||||
cls, line_string: MultiLineString | LineString, theater: ConflictTheater
|
||||
) -> list[list[LeafletLatLon]]:
|
||||
if isinstance(line_string, MultiLineString):
|
||||
lines = line_string.geoms
|
||||
else:
|
||||
lines = [line_string]
|
||||
return [cls.line_to_leaflet(line, theater) for line in lines]
|
||||
35
game/server/server.py
Normal file
35
game/server/server.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import time
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from threading import Thread
|
||||
|
||||
import uvicorn
|
||||
from uvicorn import Config
|
||||
|
||||
from game.server.settings import ServerSettings
|
||||
|
||||
|
||||
class Server(uvicorn.Server):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(
|
||||
Config(
|
||||
"game.server.app:app",
|
||||
host=ServerSettings.get().server_bind_address,
|
||||
port=ServerSettings.get().server_port,
|
||||
log_level="info",
|
||||
)
|
||||
)
|
||||
|
||||
@contextmanager
|
||||
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
|
||||
thread.join()
|
||||
28
game/server/settings.py
Normal file
28
game/server/settings.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
|
||||
from pydantic import BaseSettings
|
||||
|
||||
|
||||
class ServerSettings(BaseSettings):
|
||||
"""Settings controlling server behavior.
|
||||
|
||||
The values listed here will be automatically modified based on the environment. e.g.
|
||||
running with SERVER_BIND_ADDRESS=0.0.0.0 will cause the server to bind to all
|
||||
interfaces.
|
||||
|
||||
https://fastapi.tiangolo.com/advanced/settings
|
||||
"""
|
||||
|
||||
# WARNING: Be extremely cautious exposing the server to other machines. As there is
|
||||
# no client/server workflow yet, security has not been a focus.
|
||||
server_bind_address: str = "::1"
|
||||
|
||||
# If you for some reason change the port, you'll need to also update map.js.
|
||||
server_port: int = 5000
|
||||
|
||||
@classmethod
|
||||
@lru_cache
|
||||
def get(cls) -> ServerSettings:
|
||||
return cls()
|
||||
Reference in New Issue
Block a user