Dan Albert 350f08be2f 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.
2022-02-15 18:14:34 -08:00

45 lines
1.4 KiB
Python

from __future__ import annotations
from PySide2.QtCore import Property, QObject, Signal
from game import Game
from game.server.leaflet import LeafletPoly, ShapelyUtil
class MapZonesJs(QObject):
inclusionZonesChanged = Signal()
exclusionZonesChanged = Signal()
seaZonesChanged = Signal()
def __init__(
self,
inclusion_zones: list[LeafletPoly],
exclusion_zones: list[LeafletPoly],
sea_zones: list[LeafletPoly],
) -> None:
super().__init__()
self._inclusion_zones = inclusion_zones
self._exclusion_zones = exclusion_zones
self._sea_zones = sea_zones
@Property(list, notify=inclusionZonesChanged)
def inclusionZones(self) -> list[LeafletPoly]:
return self._inclusion_zones
@Property(list, notify=exclusionZonesChanged)
def exclusionZones(self) -> list[LeafletPoly]:
return self._exclusion_zones
@Property(list, notify=seaZonesChanged)
def seaZones(self) -> list[LeafletPoly]:
return self._sea_zones
@classmethod
def from_game(cls, game: Game) -> MapZonesJs:
zones = game.theater.landmap
return MapZonesJs(
ShapelyUtil.polys_to_leaflet(zones.inclusion_zones, game.theater),
ShapelyUtil.polys_to_leaflet(zones.exclusion_zones, game.theater),
ShapelyUtil.polys_to_leaflet(zones.sea_zones, game.theater),
)