Add an endpoint for listing all control points.

This commit is contained in:
Dan Albert
2022-02-27 22:37:47 -08:00
parent e3adcada52
commit 0056747aee
4 changed files with 57 additions and 1 deletions

View File

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

View File

@@ -0,0 +1,31 @@
from __future__ import annotations
from pydantic import BaseModel
from game.server.leaflet import LeafletPoint
from game.theater import ControlPoint
class ControlPointJs(BaseModel):
id: int
name: str
blue: bool
position: LeafletPoint
mobile: bool
destination: LeafletPoint | None
sidc: str
@staticmethod
def for_control_point(control_point: ControlPoint) -> ControlPointJs:
destination = None
if control_point.target_position is not None:
destination = control_point.target_position.latlng()
return ControlPointJs(
id=control_point.id,
name=control_point.name,
blue=control_point.captured,
position=control_point.position.latlng(),
mobile=control_point.moveable and control_point.captured,
destination=destination,
sidc=str(control_point.sidc()),
)

View File

@@ -0,0 +1,15 @@
from fastapi import APIRouter, Depends
from game import Game
from .models import ControlPointJs
from ..dependencies import GameContext
router: APIRouter = APIRouter(prefix="/control-points")
@router.get("/")
def list_control_points(game: Game = Depends(GameContext.get)) -> list[ControlPointJs]:
control_points = []
for control_point in game.theater.controlpoints:
control_points.append(ControlPointJs.for_control_point(control_point))
return control_points