summaryrefslogtreecommitdiff
path: root/src/drone.py
blob: d47a08a4361cfa71db56d50141f80b92f5c622b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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