mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Add websocket event stream for sim updates.
As a proof of concept this only covers the flight positions.
This commit is contained in:
@@ -22,7 +22,7 @@ class Navigating(InFlight):
|
||||
self, events: GameUpdateEvents, time: datetime, duration: timedelta
|
||||
) -> None:
|
||||
super().on_game_tick(events, time, duration)
|
||||
events.update_flight(self.flight)
|
||||
events.update_flight(self.flight, self.estimate_position())
|
||||
|
||||
def progress(self) -> float:
|
||||
return (
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
from .dependencies import GameContext
|
||||
from .eventstream import EventStream
|
||||
from .server import Server
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from . import debuggeometries
|
||||
from . import debuggeometries, eventstream
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(debuggeometries.router)
|
||||
app.include_router(eventstream.router)
|
||||
|
||||
2
game/server/eventstream/__init__.py
Normal file
2
game/server/eventstream/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .eventstream import EventStream
|
||||
from .routes import router
|
||||
29
game/server/eventstream/eventstream.py
Normal file
29
game/server/eventstream/eventstream.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from asyncio import Queue
|
||||
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class EventStream:
|
||||
_queue: Queue[GameUpdateEvents] = Queue()
|
||||
|
||||
@classmethod
|
||||
def drain(cls) -> None:
|
||||
while not cls._queue.empty():
|
||||
cls._queue.get_nowait()
|
||||
cls._queue.task_done()
|
||||
|
||||
@classmethod
|
||||
async def put(cls, events: GameUpdateEvents) -> None:
|
||||
await cls._queue.put(events)
|
||||
|
||||
@classmethod
|
||||
def put_nowait(cls, events: GameUpdateEvents) -> None:
|
||||
# The queue has infinite size so this should never need to block anyway. If for
|
||||
# some reason the queue is full this will throw QueueFull.
|
||||
cls._queue.put_nowait(events)
|
||||
|
||||
@classmethod
|
||||
async def get(cls) -> GameUpdateEvents:
|
||||
events = await cls._queue.get()
|
||||
cls._queue.task_done()
|
||||
return events
|
||||
25
game/server/eventstream/models.py
Normal file
25
game/server/eventstream/models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from game.server.leaflet import LeafletLatLon
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game import Game
|
||||
from game.sim.gameupdateevents import GameUpdateEvents
|
||||
|
||||
|
||||
class GameUpdateEventsJs(BaseModel):
|
||||
updated_flights: dict[UUID, LeafletLatLon]
|
||||
|
||||
@classmethod
|
||||
def from_events(cls, events: GameUpdateEvents, game: Game) -> GameUpdateEventsJs:
|
||||
return GameUpdateEventsJs(
|
||||
updated_flights={
|
||||
f[0].id: game.theater.point_to_ll(f[1]).as_list()
|
||||
for f in events.updated_flights
|
||||
}
|
||||
)
|
||||
21
game/server/eventstream/routes.py
Normal file
21
game/server/eventstream/routes.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from .eventstream import EventStream
|
||||
from .models import GameUpdateEventsJs
|
||||
from .. import GameContext
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.websocket("/eventstream")
|
||||
async def event_stream(websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
while True:
|
||||
if not (events := await EventStream.get()).empty:
|
||||
await websocket.send_json(
|
||||
jsonable_encoder(
|
||||
GameUpdateEventsJs.from_events(events, GameContext.get())
|
||||
)
|
||||
)
|
||||
@@ -72,6 +72,11 @@ class GameLoop:
|
||||
self.completed = True
|
||||
|
||||
def send_update(self, rate_limit: bool) -> None:
|
||||
# We don't skip empty events because we still want the tick in the Qt part of
|
||||
# the UI, which will update things like the current simulation time. The time
|
||||
# probably be an "event" of its own. For now the websocket endpoint will filter
|
||||
# out empty events to avoid the map handling unnecessary events, but we still
|
||||
# pass the events through to Qt.
|
||||
now = datetime.now()
|
||||
time_since_update = now - self.last_update_time
|
||||
if not rate_limit or time_since_update >= timedelta(seconds=1 / 60):
|
||||
|
||||
@@ -2,6 +2,8 @@ from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from dcs import Point
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from game.ato import Flight
|
||||
from game.sim.combat import FrozenCombat
|
||||
@@ -12,7 +14,18 @@ class GameUpdateEvents:
|
||||
self.simulation_complete = False
|
||||
self.new_combats: list[FrozenCombat] = []
|
||||
self.updated_combats: list[FrozenCombat] = []
|
||||
self.updated_flights: list[Flight] = []
|
||||
self.updated_flights: list[tuple[Flight, Point]] = []
|
||||
|
||||
@property
|
||||
def empty(self) -> bool:
|
||||
return not any(
|
||||
[
|
||||
self.simulation_complete,
|
||||
self.new_combats,
|
||||
self.updated_combats,
|
||||
self.updated_flights,
|
||||
]
|
||||
)
|
||||
|
||||
def complete_simulation(self) -> None:
|
||||
self.simulation_complete = True
|
||||
@@ -23,5 +36,5 @@ class GameUpdateEvents:
|
||||
def update_combat(self, combat: FrozenCombat) -> None:
|
||||
self.updated_combats.append(combat)
|
||||
|
||||
def update_flight(self, flight: Flight) -> None:
|
||||
self.updated_flights.append(flight)
|
||||
def update_flight(self, flight: Flight, new_position: Point) -> None:
|
||||
self.updated_flights.append((flight, new_position))
|
||||
|
||||
Reference in New Issue
Block a user