Beispiel #1
0
 def add_node(self, area: Area, node: Node):
     if area.node_with_name(node.name) is not None:
         raise ValueError(f"A node named {node.name} already exists.")
     area.nodes.append(node)
     area.connections[node] = {}
     area.clear_dock_cache()
     self.game.world_list.invalidate_node_cache()
Beispiel #2
0
    def remove_node(self, area: Area, node: Node):
        area.nodes.remove(node)
        area.connections.pop(node, None)
        for connection in area.connections.values():
            connection.pop(node, None)
        area.clear_dock_cache()

        self.game.world_list.invalidate_node_cache()
Beispiel #3
0
    def replace_node(self, area: Area, old_node: Node, new_node: Node):
        def sub(n: Node):
            return new_node if n == old_node else n

        if old_node not in area.nodes:
            raise ValueError("Given {} does does not belong to {}{}".format(
                old_node.name, area.name,
                ", but the area contains a node with that name."
                if area.node_with_name(old_node.name) is not None else "."
            ))

        if old_node.name != new_node.name and area.node_with_name(new_node.name) is not None:
            raise ValueError(f"A node named {new_node.name} already exists.")

        if isinstance(old_node, DockNode):
            self.remove_node(area, old_node.lock_node)

        old_identifier = old_node.identifier
        self.replace_references_to_node_identifier(
            old_identifier,
            old_identifier.renamed(new_node.name),
        )

        area.nodes[area.nodes.index(old_node)] = new_node

        new_connections = {
            sub(source_node): {
                sub(target_node): requirements
                for target_node, requirements in connection.items()
            }
            for source_node, connection in area.connections.items()
        }
        area.connections.clear()
        area.connections.update(new_connections)
        if area.default_node == old_node.name:
            object.__setattr__(area, "default_node", new_node.name)
        area.clear_dock_cache()

        if isinstance(new_node, DockNode):
            self.add_node(area, DockLockNode.create_from_dock(new_node, self.new_node_index()))

        self.game.world_list.invalidate_node_cache()