mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
mypy struggles to prove this cast correct when there are two or'd isinstance checks where both types coincidentally have properties of the same name (but no defined protocol making that explicit). I'm not really sure why mypy is happy with this in its current state, but it isn't after a change I'm making. All our isinstance use is a bit of an anti-pattern anyway, so extract a method that exposes the data we care about. The start/end times for tankers aren't actually used, so this could be simplified even more, but that data _should_ be used.
77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from dataclasses import dataclass
|
|
from datetime import timedelta
|
|
from typing import TYPE_CHECKING, Type
|
|
|
|
from .flightplan import FlightPlan, Layout
|
|
from .ibuilder import IBuilder
|
|
from .waypointbuilder import WaypointBuilder
|
|
from .. import Flight
|
|
from ..flightwaypointtype import FlightWaypointType
|
|
|
|
if TYPE_CHECKING:
|
|
from ..flightwaypoint import FlightWaypoint
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CustomLayout(Layout):
|
|
custom_waypoints: list[FlightWaypoint]
|
|
|
|
def iter_waypoints(self) -> Iterator[FlightWaypoint]:
|
|
yield self.departure
|
|
yield from self.custom_waypoints
|
|
|
|
|
|
class CustomFlightPlan(FlightPlan[CustomLayout]):
|
|
@staticmethod
|
|
def builder_type() -> Type[Builder]:
|
|
return Builder
|
|
|
|
@property
|
|
def tot_waypoint(self) -> FlightWaypoint:
|
|
target_types = (
|
|
FlightWaypointType.PATROL_TRACK,
|
|
FlightWaypointType.TARGET_GROUP_LOC,
|
|
FlightWaypointType.TARGET_POINT,
|
|
FlightWaypointType.TARGET_SHIP,
|
|
)
|
|
for waypoint in self.waypoints:
|
|
if waypoint in target_types:
|
|
return waypoint
|
|
return self.layout.departure
|
|
|
|
def tot_for_waypoint(self, waypoint: FlightWaypoint) -> timedelta | None:
|
|
if waypoint == self.tot_waypoint:
|
|
return self.package.time_over_target
|
|
return None
|
|
|
|
def depart_time_for_waypoint(self, waypoint: FlightWaypoint) -> timedelta | None:
|
|
return None
|
|
|
|
@property
|
|
def mission_begin_on_station_time(self) -> timedelta | None:
|
|
return None
|
|
|
|
@property
|
|
def mission_departure_time(self) -> timedelta:
|
|
return self.package.time_over_target
|
|
|
|
|
|
class Builder(IBuilder[CustomFlightPlan, CustomLayout]):
|
|
def __init__(
|
|
self, flight: Flight, waypoints: list[FlightWaypoint] | None = None
|
|
) -> None:
|
|
super().__init__(flight)
|
|
if waypoints is None:
|
|
waypoints = []
|
|
self.waypoints = waypoints
|
|
|
|
def layout(self) -> CustomLayout:
|
|
builder = WaypointBuilder(self.flight, self.coalition)
|
|
return CustomLayout(builder.takeoff(self.flight.departure), self.waypoints)
|
|
|
|
def build(self) -> CustomFlightPlan:
|
|
return CustomFlightPlan(self.flight, self.layout())
|