"""Class and methods for a drone""" from dataclasses import dataclass, field from typing import List from graph import Graph from models import Zone, Connection from pathfinder import PathResult @dataclass class Drone: """ Represent one drone travelling along a fixed path Initialize path as an empty list using the default factory of library dataclass. """ id_: int path: List[str] = field(default_factory=list) path_index: int = 0 delivered: bool = False in_transit: bool = False # read-only, no parentheses needed to call @property def current_zone(self) -> str: """Return the drone's current zone.""" return self.path[self.path_index] @property def next_zone(self) -> str | None: """Return the next zone, or None if the drone is delivered.""" next_index = self.path_index + 1 if next_index >= len(self.path): return None return self.path[next_index] def move(self) -> str | None: """Move the drone to its next zone.""" dest = self.next_zone if dest is None: self.delivered = True return None self.path_index += 1 if self.path_index == len(self.path) - 1: self.delivered = True return dest def leave_transit(self, src: Zone, dest: Zone, graph: Graph) -> None: """Leave transit to restricted zone""" self.move() connection = graph.get_connection(src, dest) connection.congested = False dest.congested = False connection.link_occupancy -=1 dest.occupancy += 1 dest.reservation -= 1 self.in_transit = False def update_path(self, path_result: PathResult) -> None: """ Update drone's path""" self.path = path_result.path self.path_index = 0 def update_dest(self, graph: Graph) -> Zone: """"Return drone's destination""" if self.next_zone is None: raise KeyError("That is it! No next zone") dest_name: str = self.next_zone dest: Zone = graph.get_zone(dest_name) return dest