summaryrefslogtreecommitdiff
path: root/src/drone.py
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-11-25 17:56:21 +0100
committeryctct <yctct@yctct.com>2026-11-25 17:56:21 +0100
commit489b223db74f468032c1eeb6ca18ee6a4da3bc33 (patch)
treea360664813443859ebfc0dcce79dec7fddf93c9f /src/drone.py
Add files, first commit
Diffstat (limited to 'src/drone.py')
-rw-r--r--src/drone.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/drone.py b/src/drone.py
new file mode 100644
index 0000000..d47a08a
--- /dev/null
+++ b/src/drone.py
@@ -0,0 +1,77 @@
+"""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