Dan Albert b7439cbd17 Add metadata to FastAPI endpoints for OpenAPI.
operation_ids give us better function names when generating the
typescript API from the openapi.json. BaseModel.Config.title does the
same for type names. Response models (or 204 status codes) need to be
explicit or the API will be declared as returning any.
2022-03-06 17:12:00 -08:00

63 lines
1.8 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
from uuid import UUID
from pydantic import BaseModel
from game.server.leaflet import LeafletPoint
if TYPE_CHECKING:
from game import Game
from game.theater import TheaterGroundObject
class TgoJs(BaseModel):
id: UUID
name: str
control_point_name: str
category: str
blue: bool
position: LeafletPoint
units: list[str] # TODO: Event stream
threat_ranges: list[float] # TODO: Event stream
detection_ranges: list[float] # TODO: Event stream
dead: bool # TODO: Event stream
sidc: str # TODO: Event stream
class Config:
title = "Tgo"
@staticmethod
def for_tgo(tgo: TheaterGroundObject) -> TgoJs:
if not tgo.might_have_aa:
threat_ranges = []
detection_ranges = []
else:
threat_ranges = [tgo.threat_range(group).meters for group in tgo.groups]
detection_ranges = [
tgo.detection_range(group).meters for group in tgo.groups
]
return TgoJs(
id=tgo.id,
name=tgo.name,
control_point_name=tgo.control_point.name,
category=tgo.category,
blue=tgo.control_point.captured,
position=tgo.position.latlng(),
units=[unit.display_name for unit in tgo.units],
threat_ranges=threat_ranges,
detection_ranges=detection_ranges,
dead=tgo.is_dead,
sidc=str(tgo.sidc()),
)
@staticmethod
def all_in_game(game: Game) -> list[TgoJs]:
tgos = []
for control_point in game.theater.controlpoints:
for tgo in control_point.connected_objectives:
if not tgo.is_control_point:
tgos.append(TgoJs.for_tgo(tgo))
return tgos