120 lines
3.9 kB
1"""The zone graph assembled from a map file."""
2
3from .connection import Connection
4from .zone import Zone, ZoneRole
5
6
7class Network:
8 """A map's zones and the connections between them.
9
10 Structural rules are enforced here and reported as ValueError.
11 The parser catches those and re-raises them as ParserError with
12 the line number attached, so this class stays unaware of the files.
13 """
14
15 def __init__(self) -> None:
16 """Create an empty network."""
17 self.zones: dict[str, Zone] = {}
18 self.connections: dict[tuple[str, str], Connection] = {}
19 self._adjacency: dict[str, list[Connection]] = {}
20 self._start: Zone | None = None
21 self._end: Zone | None = None
22
23 def add_zone(self, zone: Zone) -> None:
24 """Register a zone.
25
26 Args:
27 zone: The zone to add.
28
29 Raises:
30 ValueError: If the name is taken, or if the map declares a
31 second start or end zone.
32 """
33 if zone.name in self.zones:
34 raise ValueError(f"duplicate zone name {zone.name!r}")
35 if zone.role is ZoneRole.START and self._start is not None:
36 raise ValueError("more than one start_hub")
37 if zone.role is ZoneRole.END and self._end is not None:
38 raise ValueError("more than one end_hub")
39 self.zones[zone.name] = zone
40 self._adjacency[zone.name] = []
41 if zone.role is ZoneRole.START:
42 self._start = zone
43 elif zone.role is ZoneRole.END:
44 self._end = zone
45
46 def add_connection(self, connection: Connection) -> None:
47 """Register a connection between two known zones.
48
49 Args:
50 connection: The connection to add.
51
52 Raises:
53 ValueError: If an endpoint is unknown, if the two endpoints
54 are the same zone, or if the link already exists in
55 either direction.
56 """
57 for name in (connection.a, connection.b):
58 if name not in self.zones:
59 raise ValueError(f"unknown zone {name!r}")
60 if connection.a == connection.b:
61 raise ValueError(f"zone {connection.a!r} linked to itself")
62 if connection.key in self.connections:
63 raise ValueError(f"duplicate connection {connection}")
64 self.connections[connection.key] = connection
65 self._adjacency[connection.a].append(connection)
66 self._adjacency[connection.b].append(connection)
67
68 def neighbors(self, name: str) -> list[tuple[Zone, Connection]]:
69 """List the zones reachable in one hop from a zone.
70
71 Blocked zones are included: callers decide whether to skip
72 them, so the renderer can still draw the whole map.
73
74 Args:
75 name: Name of the zone to look around.
76
77 Returns:
78 Pairs of neighbouring zone and the link leading to it.
79
80 Raises:
81 KeyError: If no zone carries that name.
82 """
83 links = self._adjacency[name]
84 return [(self.zones[link.other(name)], link) for link in links]
85
86 @property
87 def start(self) -> Zone:
88 """The zone every drone departs from.
89
90 Returns:
91 The start zone.
92
93 Raises:
94 ValueError: If the map declared none.
95 """
96 if self._start is None:
97 raise ValueError("map declares no start_hub")
98 return self._start
99
100 @property
101 def end(self) -> Zone:
102 """The zone drones are delivered to.
103
104 Returns:
105 The end zone.
106
107 Raises:
108 ValueError: If the map declared none.
109 """
110 if self._end is None:
111 raise ValueError("map declares no end_hub")
112 return self._end
113
114 def __len__(self) -> int:
115 """Count the zones in the network.
116
117 Returns:
118 The number of registered zones.
119 """
120 return len(self.zones)