Clean up IADS exception handling.

This commit is contained in:
Raffson 2022-06-29 05:30:17 +02:00 committed by GitHub
parent d45bba19c8
commit 8c2c353071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,19 +118,19 @@ class IadsNetwork:
if game.iads_considerate_culling(node.group.ground_object): if game.iads_considerate_culling(node.group.ground_object):
# Skip culled ground objects # Skip culled ground objects
continue continue
try:
skynet_node = SkynetNode.from_group(node.group) # SkynetNode.from_group(node.group) may raise an exception
for connection in node.connections.values(): # (originating from SkynetNode.dcs_name_for_group)
if connection.ground_object.is_friendly( # but if it does, we want to know because it's supposed to be impossible afaict
skynet_node.player skynet_node = SkynetNode.from_group(node.group)
) and not game.iads_considerate_culling(connection.ground_object): for connection in node.connections.values():
skynet_node.connections[connection.iads_role.value].append( if connection.ground_object.is_friendly(
SkynetNode.dcs_name_for_group(connection) skynet_node.player
) ) and not game.iads_considerate_culling(connection.ground_object):
skynet_nodes.append(skynet_node) skynet_node.connections[connection.iads_role.value].append(
except IadsNetworkException: SkynetNode.dcs_name_for_group(connection)
# Node not skynet compatible )
continue skynet_nodes.append(skynet_node)
return skynet_nodes return skynet_nodes
def update_tgo(self, tgo: TheaterGroundObject) -> None: def update_tgo(self, tgo: TheaterGroundObject) -> None:
@ -139,13 +139,13 @@ class IadsNetwork:
for cn in self.nodes: for cn in self.nodes:
if cn.group.ground_object == tgo: if cn.group.ground_object == tgo:
self.nodes.remove(cn) self.nodes.remove(cn)
try:
# Create a new node for the tgo # Create a new node for the tgo
self.node_for_tgo(tgo) node = self.node_for_tgo(tgo)
# TODO Add the connections or calculate them.. if node is None:
except IadsNetworkException:
# Not participating # Not participating
pass return
# TODO Add the connections or calculate them..
def node_for_group(self, group: IadsGroundGroup) -> IadsNetworkNode: def node_for_group(self, group: IadsGroundGroup) -> IadsNetworkNode:
"""Get existing node from the iads network or create a new node""" """Get existing node from the iads network or create a new node"""
@ -157,7 +157,7 @@ class IadsNetwork:
self.nodes.append(node) self.nodes.append(node)
return node return node
def node_for_tgo(self, tgo: TheaterGroundObject) -> IadsNetworkNode: def node_for_tgo(self, tgo: TheaterGroundObject) -> Optional[IadsNetworkNode]:
"""Get existing node from the iads network or create a new node""" """Get existing node from the iads network or create a new node"""
for cn in self.nodes: for cn in self.nodes:
if cn.group.ground_object == tgo: if cn.group.ground_object == tgo:
@ -177,8 +177,7 @@ class IadsNetwork:
node.add_connection_for_group(group) node.add_connection_for_group(group)
if node is None: if node is None:
# Raise exception as TGO does not participate to the IADS logging.debug(f"TGO {tgo.name} not participating to IADS")
raise IadsNetworkException(f"TGO {tgo.name} not participating to IADS")
return node return node
def initialize_network(self, ground_objects: Iterator[TheaterGroundObject]) -> None: def initialize_network(self, ground_objects: Iterator[TheaterGroundObject]) -> None:
@ -202,31 +201,35 @@ class IadsNetwork:
"""Initialize the IADS Network in basic mode (SAM & EWR only)""" """Initialize the IADS Network in basic mode (SAM & EWR only)"""
for go in self.ground_objects.values(): for go in self.ground_objects.values():
if isinstance(go, IadsGroundObject): if isinstance(go, IadsGroundObject):
try: self.node_for_tgo(go)
self.node_for_tgo(go)
except IadsNetworkException:
# TGO does not participate to the IADS -> Skip
pass
def initialize_network_from_config(self) -> None: def initialize_network_from_config(self) -> None:
"""Initialize the IADS Network from a configuration""" """Initialize the IADS Network from a configuration"""
for element_name, connections in self.iads_config.items(): for element_name, connections in self.iads_config.items():
warning_msg = (
f"IADS: No ground object found for {element_name}."
f" This can be normal behaviour."
)
try: try:
node = self.node_for_tgo(self.ground_objects[element_name]) node = self.node_for_tgo(self.ground_objects[element_name])
except (KeyError, IadsNetworkException): except KeyError:
node = None
warning_msg = (
f"IADS: No ground object found for connection {element_name}"
)
if node is None:
# Log a warning as this can be normal. Possible case is for example # Log a warning as this can be normal. Possible case is for example
# when the campaign request a Long Range SAM but the faction has none # when the campaign request a Long Range SAM but the faction has none
# available. Therefore the TGO will not get populated at all # available. Therefore the TGO will not get populated at all
logging.warning( logging.warning(warning_msg)
f"IADS: No ground object found for {element_name}. This can be normal behaviour."
)
continue continue
# Find all connected ground_objects # Find all connected ground_objects
for node_name in connections: for node_name in connections:
try: try:
node.add_connection_for_tgo(self.ground_objects[node_name]) node.add_connection_for_tgo(self.ground_objects[node_name])
except (KeyError): except KeyError:
logging.error( logging.error(
f"IADS: No ground object found for connection {node_name}" f"IADS: No ground object found for connection {node_name}"
) )
@ -243,10 +246,9 @@ class IadsNetwork:
and IadsRole.for_category(go.category) == IadsRole.COMMAND_CENTER and IadsRole.for_category(go.category) == IadsRole.COMMAND_CENTER
) )
): ):
try: # Set as primary node
# Set as primary node node = self.node_for_tgo(go)
node = self.node_for_tgo(go) if node is None:
except IadsNetworkException:
# TGO does not participate to iads network # TGO does not participate to iads network
continue continue
# Find nearby Power or Connection # Find nearby Power or Connection