Move threat zones out of MapModel.

This commit is contained in:
Dan Albert
2022-02-22 19:59:29 -08:00
parent 1c543666b5
commit b1356551c6
10 changed files with 62 additions and 142 deletions

View File

@@ -119,8 +119,9 @@ class Coalition:
def adjust_budget(self, amount: float) -> None:
self.budget += amount
def compute_threat_zones(self) -> None:
def compute_threat_zones(self, events: GameUpdateEvents) -> None:
self._threat_zone = ThreatZones.for_faction(self.game, self.player)
events.update_threat_zones()
def compute_nav_meshes(self, events: GameUpdateEvents) -> None:
self._navmesh = NavMesh.from_threat_zones(

View File

@@ -421,8 +421,8 @@ class Game:
return TransitNetworkBuilder(self.theater, player).build()
def compute_threat_zones(self, events: GameUpdateEvents) -> None:
self.blue.compute_threat_zones()
self.red.compute_threat_zones()
self.blue.compute_threat_zones(events)
self.red.compute_threat_zones(events)
self.blue.compute_nav_meshes(events)
self.red.compute_nav_meshes(events)

View File

@@ -19,6 +19,7 @@ class GameUpdateEventsJs(BaseModel):
updated_combats: list[FrozenCombatJs] = []
navmesh_updates: set[bool] = set()
unculled_zones_updated: bool = False
threat_zones_updated: bool = False
@classmethod
def from_events(cls, events: GameUpdateEvents, game: Game) -> GameUpdateEventsJs:
@@ -35,4 +36,5 @@ class GameUpdateEventsJs(BaseModel):
],
navmesh_updates=events.navmesh_updates,
unculled_zones_updated=events.unculled_zones_updated,
threat_zones_updated=events.threat_zones_updated,
)

View File

@@ -3,7 +3,9 @@ from __future__ import annotations
from dcs.mapping import LatLng
from pydantic import BaseModel
from game.server.leaflet import LeafletPoly
from game.server.leaflet import LeafletPoly, ShapelyUtil
from game.theater import ConflictTheater
from game.threatzones import ThreatZones
class MapZonesJs(BaseModel):
@@ -15,3 +17,24 @@ class MapZonesJs(BaseModel):
class UnculledZoneJs(BaseModel):
position: LatLng
radius: float
class ThreatZonesJs(BaseModel):
full: list[LeafletPoly]
aircraft: list[LeafletPoly]
air_defenses: list[LeafletPoly]
radar_sams: list[LeafletPoly]
@classmethod
def from_zones(cls, zones: ThreatZones, theater: ConflictTheater) -> ThreatZonesJs:
return ThreatZonesJs(
full=ShapelyUtil.polys_to_leaflet(zones.all, theater),
aircraft=ShapelyUtil.polys_to_leaflet(zones.airbases, theater),
air_defenses=ShapelyUtil.polys_to_leaflet(zones.air_defenses, theater),
radar_sams=ShapelyUtil.polys_to_leaflet(zones.radar_sam_threats, theater),
)
class ThreatZoneContainerJs(BaseModel):
blue: ThreatZonesJs
red: ThreatZonesJs

View File

@@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
from game import Game
from game.server import GameContext
from .models import MapZonesJs, UnculledZoneJs
from .models import MapZonesJs, ThreatZoneContainerJs, ThreatZonesJs, UnculledZoneJs
from ..leaflet import ShapelyUtil
router: APIRouter = APIRouter(prefix="/map-zones")
@@ -29,3 +29,11 @@ def get_unculled_zones(game: Game = Depends(GameContext.get)) -> list[UnculledZo
)
for zone in game.get_culling_zones()
]
@router.get("/threats")
def get_threat_zones(game: Game = Depends(GameContext.get)) -> ThreatZoneContainerJs:
return ThreatZoneContainerJs(
blue=ThreatZonesJs.from_zones(game.threat_zone_for(player=True), game.theater),
red=ThreatZonesJs.from_zones(game.threat_zone_for(player=False), game.theater),
)

View File

@@ -17,6 +17,7 @@ class GameUpdateEvents:
self.updated_flights: list[tuple[Flight, Point]] = []
self.navmesh_updates: set[bool] = set()
self.unculled_zones_updated: bool = False
self.threat_zones_updated: bool = False
@property
def empty(self) -> bool:
@@ -28,6 +29,7 @@ class GameUpdateEvents:
self.updated_flights,
self.navmesh_updates,
self.unculled_zones_updated,
self.threat_zones_updated,
]
)
@@ -48,3 +50,6 @@ class GameUpdateEvents:
def update_unculled_zones(self) -> None:
self.unculled_zones_updated = True
def update_threat_zones(self) -> None:
self.threat_zones_updated = True