Example #1
0
    def test_connect_nodes(self, mock_get_node_by_serial, mock_connect_nodes,
                           mock_utils):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 1,
                                'y': 1
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 2,
                                'y': 2
                            },
                            size=1,
                            real=True)

        mock_get_node_by_serial.side_effect = [node_1, node_2]
        mock_connect_nodes.return_value = None
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        # Act
        data_handler.connect_nodes(node_1, node_1)

        # Assert
        mock_connect_nodes.called_once(node_1, node_1, allow_overflow=True)
Example #2
0
def check_collisions(x_location, y_location, graph, node_size, extra_space):
    """

    :param x_location:
    :param y_location:
    :param graph:
    :param node_size:
    :param extra_space:
    :return:
    """
    temp_node = NodeObject('0', {'x': x_location, 'y': y_location}, node_size)
    collision = False
    for node in graph.node_list:
        if temp_node.distance(node) < node_size + node.size + extra_space * 2:
            collision = True
    return collision
Example #3
0
    def add_node(self, x_loc, y_loc, node_colour=Colours['red'], node_shape=Shapes['circle'], node_size=50, serial=None,
                 real=True):
        """
        :param serial: A specific serial for the node.
        :param x_loc: The x location of the node
        :param y_loc: The y location of the node
        :param node_colour: Colour of the node
        :param node_shape: Shape of the node
        :param node_size: Size of the node (int value)
        :return: the new node of type NodeObject
        """
        if self.size["max_x"] < x_loc + node_size and 0 > x_loc - node_size:
            raise Exception("Error! Coordinate of node is out of bound: {}".format(x_loc))
        if self.size["max_y"] < y_loc + node_size and 0 > y_loc - node_size:
            raise Exception("Error! Coordinate of node is out of bound: {}".format(y_loc))

        location = {'x': x_loc, 'y': y_loc}
        if serial is not None:
            if self.get_node_by_serial(serial) is not None:
                print("Error - Trying to add a node with an existing serial")
                return None
        else:
            serial = get_serial()
        new_node = NodeObject(serial=serial, location=location, size=node_size, colour=node_colour, shape=node_shape,
                              real=real, dummy_num=self.inner_node_count)
        self.inner_node_count+=1
        self.node_list.append(new_node)
        self.center_node = self.node_list[0].serial_num
        return new_node
Example #4
0
    def test_get_furthest_nodes(self):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 1,
                                'y': 1
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 2,
                                'y': 2
                            },
                            size=1,
                            real=True)
        node_3 = NodeObject(serial=3,
                            location={
                                'x': 3,
                                'y': 3
                            },
                            size=1,
                            real=True)
        node_4 = NodeObject(serial=4,
                            location={
                                'x': 4,
                                'y': 4
                            },
                            size=1,
                            real=True)

        # Act
        res = GameDataHandler.get_furthest_nodes(node_1, node_2, node_3,
                                                 node_4)

        # Assert
        self.assertIn(
            node_1.serial_num, res,
            "node with serial number {0} was not returned by function."
            " Got {1}".format(node_1.serial_num, res))
        self.assertIn(
            node_4.serial_num, res,
            "node with serial number {0} was not returned by function."
            " Got {1}".format(node_4.serial_num, res))
Example #5
0
 def add_empty_edge(self, graph, nodes_list, edges_list):
     for node in graph.node_list:
         if node not in nodes_list:
             for friend in node.neighbors:
                 if friend in [item.serial_num for item in nodes_list]:
                     tmp_node = graph.get_node_by_serial(friend)
                     fake_node_1 = NodeObject(serial=None,
                                              location={
                                                  'x': node.x,
                                                  'y': node.y
                                              },
                                              size=node.size,
                                              real=False)
                     fake_node_2 = NodeObject(serial=None,
                                              location={
                                                  'x': tmp_node.x,
                                                  'y': tmp_node.y
                                              },
                                              size=tmp_node.size,
                                              real=False)
                     edges_list.append((fake_node_1, fake_node_2))
                     return
Example #6
0
    def test_distance_from_line(self):
        # Arrange
        node1 = NodeObject(serial=1, location={'x': 1, 'y': 5}, size=1)
        node2 = NodeObject(serial=2, location={'x': 8, 'y': 7}, size=1)
        node_far = NodeObject(serial=3, location={'x': 4, 'y': 3}, size=1)
        node_close = NodeObject(serial=4, location={'x': 5, 'y': 7}, size=1)
        max_error = 0.001
        far_result = 2.7472112789737806
        close_result = 0.8241633836921342

        # Act
        far_distance = node_far.distance_from_line(node1, node2)
        close_distance = node_close.distance_from_line(node1, node2)

        # Assert
        assert far_distance < far_result + max_error or far_distance > far_result - max_error
        assert close_distance < close_result + max_error or close_distance > close_result - max_error
Example #7
0
 def test_distance_between_nodes(self):
     node1 = NodeObject(serial=1, location={'x': 1, 'y': 3}, size=1)
     node2 = NodeObject(serial=2, location={'x': 5, 'y': 6}, size=1)
     actual_distance = 5
     assert node1.distance(node2) == actual_distance
Example #8
0
    def tester_graph_2():
        graph = GraphObject(None, 5000, 5000, 20, 3, 10)
        node1 = NodeObject("a26", {'x': 614, 'y': 600}, 50, Colours['yellow'])
        node2 = NodeObject("ce5", {
            'x': -2465,
            'y': 2732
        }, 50, Colours['yellow'])
        node3 = NodeObject("4ec", {'x': -3229, 'y': 3455}, 50, Colours['red'])
        node4 = NodeObject("5e7", {'x': -1184, 'y': 3385}, 50, Colours['blue'])
        node5 = NodeObject("5ea", {'x': 303, 'y': 1050}, 50, Colours['red'])
        node6 = NodeObject("a97", {'x': -1128, 'y': 1884}, 50, Colours['blue'])
        node7 = NodeObject("5f3", {'x': -2910, 'y': 271}, 50, Colours['red'])
        node8 = NodeObject("68f", {'x': -1105, 'y': 1409}, 50, Colours['blue'])
        node9 = NodeObject("a0f", {
            'x': -1598,
            'y': 4054
        }, 50, Colours['yellow'])
        node10 = NodeObject("b3b", {'x': 1047, 'y': 61}, 50, Colours['red'])
        node11 = NodeObject("379", {'x': -1034, 'y': 2046}, 50, Colours['red'])
        node12 = NodeObject("c5c", {'x': 1543, 'y': 4466}, 50, Colours['red'])
        node13 = NodeObject("97b", {
            'x': -2551,
            'y': 1694
        }, 50, Colours['yellow'])
        node14 = NodeObject("ac2", {'x': 821, 'y': 4534}, 50, Colours['blue'])
        node15 = NodeObject("788", {
            'x': -2790,
            'y': 2330
        }, 50, Colours['yellow'])
        node16 = NodeObject("01d", {'x': 450, 'y': 300}, 50, Colours['blue'])
        node17 = NodeObject("e29", {
            'x': -272,
            'y': 910
        }, 50, Colours['yellow'])
        node18 = NodeObject("d21", {
            'x': -2329,
            'y': 2245
        }, 50, Colours['blue'])
        node19 = NodeObject("7ac", {'x': 693, 'y': 3124}, 50, Colours['blue'])
        node20 = NodeObject("189", {
            'x': -2748,
            'y': 3403
        }, 50, Colours['blue'])

        graph.node_list.append(node1)
        graph.node_list.append(node2)
        graph.node_list.append(node3)
        graph.node_list.append(node4)
        graph.node_list.append(node5)
        graph.node_list.append(node6)
        graph.node_list.append(node7)
        graph.node_list.append(node8)
        graph.node_list.append(node9)
        graph.node_list.append(node10)
        graph.node_list.append(node11)
        graph.node_list.append(node12)
        graph.node_list.append(node13)
        graph.node_list.append(node14)
        graph.node_list.append(node15)
        graph.node_list.append(node16)
        graph.node_list.append(node17)
        graph.node_list.append(node18)
        graph.node_list.append(node19)
        graph.node_list.append(node20)

        graph.connections = [('01d', 'e29'), ('01d', 'a97'), ('ac2', 'ce5'),
                             ('379', 'ce5'), ('4ec', 'c5c'), ('5e7', '68f'),
                             ('5e7', '5ea'), ('5ea', '788'), ('a0f', 'a97'),
                             ('5f3', 'b3b'), ('68f', '7ac'), ('a0f', 'd21'),
                             ('a26', 'b3b'), ('189', '379'), ('97b', 'c5c'),
                             ('97b', 'e29'), ('a26', 'ac2'), ('788', '7ac'),
                             ('189', 'd21')]
        graph.center_node = "01d"

        return graph
Example #9
0
    def get_partly_visible_edge(self, edge, top, bottom, left, right, node,
                                edge_equation):
        """

        :param edge: an edge that can be seen onscreen but where at least one node is not visible
        :param top: equation representing the top border of the screen
        :param bottom: equation representing the bottom border of the screen
        :param left: equation representing the left border of the screen
        :param right: equation representing the right border of the screen
        :param node: the visible node connected to the edge, or None if no node is visible
        :param edge_equation: the equation of the checked edge
        :return: A tuple containing two NodeObjects, each representing a one of the edge's nodes, the edge's slope and
        the edge's equation. If one of the edge's nodes is not onscreen, the x,y coordinates represent the intersection
        between the edge and the screen, a new serial is created, size is set to 0 and real is set to False.
        """
        first_node = None
        second_node = None

        if node:
            first_node = self.original_graph.get_node_by_serial(node.serial)

        # check if edge collides with top border
        if LineEquation.check_collision_point(edge_equation, top):
            col_point = LineEquation.get_equation_collision_point(
                edge_equation, top)
            location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)}
            if first_node is not None:
                second_node = NodeObject(serial=get_serial(),
                                         location=location,
                                         size=0)
                second_node.real = False
            else:
                first_node = NodeObject(serial=get_serial(),
                                        location=location,
                                        size=0)
                first_node.real = False

        # check if edge collides with bottom border
        if LineEquation.check_collision_point(edge_equation, bottom):
            col_point = LineEquation.get_equation_collision_point(
                edge_equation, bottom)
            location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)}
            if first_node is not None:
                second_node = NodeObject(serial=get_serial(),
                                         location=location,
                                         size=0)
                second_node.real = False
            else:
                first_node = NodeObject(serial=get_serial(),
                                        location=location,
                                        size=0)
                first_node.real = False

        # check if edge collides with left border
        if LineEquation.check_collision_point(edge_equation, left):
            col_point = LineEquation.get_equation_collision_point(
                edge_equation, left)
            location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)}
            if first_node is not None:
                second_node = NodeObject(serial=get_serial(),
                                         location=location,
                                         size=0)
                second_node.real = False

            else:
                first_node = NodeObject(serial=get_serial(),
                                        location=location,
                                        size=0)
                first_node.real = False

        # check if edge collides with right border
        if LineEquation.check_collision_point(edge_equation, right):
            col_point = LineEquation.get_equation_collision_point(
                edge_equation, right)
            location = {'x': round(col_point.x, 2), 'y': round(col_point.y, 2)}
            if first_node is not None:
                second_node = NodeObject(serial=get_serial(),
                                         location=location,
                                         size=0)
                second_node.real = False
            else:
                first_node = NodeObject(serial=get_serial(),
                                        location=location,
                                        size=0)
                first_node.real = False

        if second_node is None:
            if first_node is None:
                return None
            else:
                raise Exception(
                    "Only One viable node for onscreen edge: {}".format(
                        edge.print_by_serial()))

        min_dist = edge.node1.get_radius() / 2
        if first_node.distance(second_node) < min_dist:
            return None

        if first_node.x < second_node.x:
            curr_edge = (first_node, second_node, edge.slope, edge_equation)
        else:
            curr_edge = (second_node, first_node, edge.slope, edge_equation)

        return curr_edge
Example #10
0
 def tester_graph_1():
     graph = GraphObject(None, 800, 400, 6, 4, 5)
     node1 = NodeObject(188, {
         'x': 369,
         'y': 168
     }, 50, {
         'R': 1,
         'G': 0,
         'B': 0,
         'name': "red"
     })
     node2 = NodeObject(-55, {
         'x': 480,
         'y': 275
     }, 50, {
         'R': 0,
         'G': 1,
         'B': 0,
         'name': "green"
     })
     node3 = NodeObject(640, {
         'x': 628,
         'y': 169
     }, 50, {
         'R': 0,
         'G': 0,
         'B': 1,
         'name': "blue"
     })
     node4 = NodeObject(206, {
         'x': 636,
         'y': 306
     }, 50, {
         'R': 1,
         'G': 0,
         'B': 1,
         'name': "magenta"
     })
     node5 = NodeObject(-67, {
         'x': 250,
         'y': 289
     }, 50, {
         'R': 1,
         'G': 1,
         'B': 0,
         'name': "yellow"
     })
     node6 = NodeObject(186, {
         'x': 184,
         'y': 71
     }, 50, {
         'R': 0,
         'G': 1,
         'B': 1,
         'name': "cyan"
     })
     graph.node_list.append(node1)
     graph.node_list.append(node2)
     graph.node_list.append(node3)
     graph.node_list.append(node4)
     graph.node_list.append(node5)
     graph.node_list.append(node6)
     graph.connections = [(640, 188), (-55, 206), (-55, 186), (-67, 640),
                          (-67, 206), (640, 206), (-67, 186), (640, 186)]
     graph.center_node = 188
     graph.question_object_list = GraphTester.create_questions()
     return graph
Example #11
0
    def test_data_collection(self):
        # WIP
        return
        # Assemble
        new_graph = create_rand_graph("../graph_config.txt")
        gamer = GameDataHandler("../graph_config.txt")

        nodes_list = []
        edges = []

        def add_empty_edge(self, graph, nodes_list, edges_list):
            for node in graph.node_list:
                if node not in nodes_list:
                    for friend in node.neighbors:
                        if friend in [item.serial_num for item in nodes_list]:
                            tmp_node = graph.get_node_by_serial(friend)
                            fake_node_1 = NodeObject(serial=None,
                                                     location={
                                                         'x': node.x,
                                                         'y': node.y
                                                     },
                                                     size=node.size,
                                                     real=False)
                            fake_node_2 = NodeObject(serial=None,
                                                     location={
                                                         'x': tmp_node.x,
                                                         'y': tmp_node.y
                                                     },
                                                     size=tmp_node.size,
                                                     real=False)
                            edges_list.append((fake_node_1, fake_node_2))
                            return

        for i in range(3):
            nodes_list.append(new_graph.node_list[i])
        # Add an extra node that is connected to one of the existing ones

        for node in nodes_list[0].neighbors:
            if node not in nodes_list:
                nodes_list.append(new_graph.get_node_by_serial(node))
                break
        for node in nodes_list:
            for friend in node.neighbors:
                if friend in [item.serial_num for item in nodes_list]:
                    edges.append((node, new_graph.get_node_by_serial(friend)))
                else:
                    origin_node = new_graph.get_node_by_serial(friend)
                    fake_node = NodeObject(serial=None,
                                           location={
                                               'x': origin_node.x,
                                               'y': origin_node.y
                                           },
                                           size=origin_node.size,
                                           real=False)
                    edges.append((node, fake_node))

        # clean nodes
        for node in nodes_list:
            node.neighbors = set()
            node.possible_neighbors = set()

        # Add a few empty edges
        add_empty_edge(new_graph, nodes_list, edges)
        add_empty_edge(new_graph, nodes_list, edges)
        add_empty_edge(new_graph, nodes_list, edges)

        mock_reader.return_value = {'nodes': nodes_list, 'edges': edges}

        # act
        gamer.do_move()
        time.sleep(1)
Example #12
0
    def setUp(self):
        self.node_1_real = NodeObject(serial=10,
                                      location={
                                          'x': 100,
                                          'y': 100
                                      },
                                      size=1,
                                      real=True)
        self.node_1_unreal = NodeObject(serial=11,
                                        location={
                                            'x': 100,
                                            'y': 100
                                        },
                                        size=1,
                                        real=False)
        self.node_2_real = NodeObject(serial=20,
                                      location={
                                          'x': 200,
                                          'y': 200
                                      },
                                      size=1,
                                      real=True)
        self.node_2_unreal = NodeObject(serial=21,
                                        location={
                                            'x': 200,
                                            'y': 200
                                        },
                                        size=1,
                                        real=False)
        self.node_2_unreal_moved = NodeObject(serial=22,
                                              location={
                                                  'x': 205,
                                                  'y': 205
                                              },
                                              size=1,
                                              real=False)
        self.node_3_real = NodeObject(serial=30,
                                      location={
                                          'x': 300,
                                          'y': 300
                                      },
                                      size=1,
                                      real=True)
        self.node_3_unreal = NodeObject(serial=31,
                                        location={
                                            'x': 300,
                                            'y': 300
                                        },
                                        size=1,
                                        real=False)
        self.node_3_unreal_moved = NodeObject(serial=32,
                                              location={
                                                  'x': 305,
                                                  'y': 305
                                              },
                                              size=1,
                                              real=False)
        self.node_4_real = NodeObject(serial=40,
                                      location={
                                          'x': 400,
                                          'y': 400
                                      },
                                      size=1,
                                      real=True)
        self.node_4_unreal = NodeObject(serial=41,
                                        location={
                                            'x': 400,
                                            'y': 400
                                        },
                                        size=1,
                                        real=False)

        self.node_list = [
            self.node_1_real, self.node_1_unreal, self.node_2_real,
            self.node_2_unreal, self.node_3_real, self.node_3_unreal,
            self.node_4_real, self.node_4_unreal
        ]
Example #13
0
class TestGameDataHandler(unittest.TestCase):
    def setUp(self):
        self.node_1_real = NodeObject(serial=10,
                                      location={
                                          'x': 100,
                                          'y': 100
                                      },
                                      size=1,
                                      real=True)
        self.node_1_unreal = NodeObject(serial=11,
                                        location={
                                            'x': 100,
                                            'y': 100
                                        },
                                        size=1,
                                        real=False)
        self.node_2_real = NodeObject(serial=20,
                                      location={
                                          'x': 200,
                                          'y': 200
                                      },
                                      size=1,
                                      real=True)
        self.node_2_unreal = NodeObject(serial=21,
                                        location={
                                            'x': 200,
                                            'y': 200
                                        },
                                        size=1,
                                        real=False)
        self.node_2_unreal_moved = NodeObject(serial=22,
                                              location={
                                                  'x': 205,
                                                  'y': 205
                                              },
                                              size=1,
                                              real=False)
        self.node_3_real = NodeObject(serial=30,
                                      location={
                                          'x': 300,
                                          'y': 300
                                      },
                                      size=1,
                                      real=True)
        self.node_3_unreal = NodeObject(serial=31,
                                        location={
                                            'x': 300,
                                            'y': 300
                                        },
                                        size=1,
                                        real=False)
        self.node_3_unreal_moved = NodeObject(serial=32,
                                              location={
                                                  'x': 305,
                                                  'y': 305
                                              },
                                              size=1,
                                              real=False)
        self.node_4_real = NodeObject(serial=40,
                                      location={
                                          'x': 400,
                                          'y': 400
                                      },
                                      size=1,
                                      real=True)
        self.node_4_unreal = NodeObject(serial=41,
                                        location={
                                            'x': 400,
                                            'y': 400
                                        },
                                        size=1,
                                        real=False)

        self.node_list = [
            self.node_1_real, self.node_1_unreal, self.node_2_real,
            self.node_2_unreal, self.node_3_real, self.node_3_unreal,
            self.node_4_real, self.node_4_unreal
        ]

    def test_get_furthest_nodes(self):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 1,
                                'y': 1
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 2,
                                'y': 2
                            },
                            size=1,
                            real=True)
        node_3 = NodeObject(serial=3,
                            location={
                                'x': 3,
                                'y': 3
                            },
                            size=1,
                            real=True)
        node_4 = NodeObject(serial=4,
                            location={
                                'x': 4,
                                'y': 4
                            },
                            size=1,
                            real=True)

        # Act
        res = GameDataHandler.get_furthest_nodes(node_1, node_2, node_3,
                                                 node_4)

        # Assert
        self.assertIn(
            node_1.serial_num, res,
            "node with serial number {0} was not returned by function."
            " Got {1}".format(node_1.serial_num, res))
        self.assertIn(
            node_4.serial_num, res,
            "node with serial number {0} was not returned by function."
            " Got {1}".format(node_4.serial_num, res))

    @patch('SupplementaryFiles.GameDataHandler.Utils')
    @patch('SupplementaryFiles.GameDataHandler.GraphObject.connect_nodes')
    @patch('SupplementaryFiles.GameDataHandler.GraphObject.get_node_by_serial')
    def test_connect_nodes(self, mock_get_node_by_serial, mock_connect_nodes,
                           mock_utils):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 1,
                                'y': 1
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 2,
                                'y': 2
                            },
                            size=1,
                            real=True)

        mock_get_node_by_serial.side_effect = [node_1, node_2]
        mock_connect_nodes.return_value = None
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        # Act
        data_handler.connect_nodes(node_1, node_1)

        # Assert
        mock_connect_nodes.called_once(node_1, node_1, allow_overflow=True)

    @patch('SupplementaryFiles.GameDataHandler.Utils')
    def test_two_edges_are_one(self, mock_utils):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 100,
                                'y': 100
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 200,
                                'y': 200
                            },
                            size=1,
                            real=True)
        node_3 = NodeObject(serial=3,
                            location={
                                'x': 300,
                                'y': 300
                            },
                            size=1,
                            real=True)
        node_4 = NodeObject(serial=4,
                            location={
                                'x': 400,
                                'y': 400
                            },
                            size=1,
                            real=True)
        node_5 = NodeObject(serial=4,
                            location={
                                'x': 100,
                                'y': 400
                            },
                            size=1,
                            real=True)

        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        edge_35 = (node_3, node_5, node_3.slope(node_5),
                   LineEquation(slope=node_3.slope(node_5),
                                const=1,
                                edge1=node_3,
                                edge2=node_5))
        edge_24 = (node_2, node_4, node_2.slope(node_4),
                   LineEquation(slope=node_2.slope(node_4),
                                const=1,
                                edge1=node_2,
                                edge2=node_4))
        edge_12 = (node_1, node_2, node_1.slope(node_2),
                   LineEquation(slope=node_1.slope(node_2),
                                const=1,
                                edge1=node_1,
                                edge2=node_2))
        edge_13 = (node_1, node_3, node_1.slope(node_3),
                   LineEquation(slope=node_1.slope(node_3),
                                const=1,
                                edge1=node_1,
                                edge2=node_3))
        edge_34 = (node_3, node_4, node_3.slope(node_4),
                   LineEquation(slope=node_3.slope(node_4),
                                const=1,
                                edge1=node_3,
                                edge2=node_4))

        # Act

        res_miss = data_handler.two_edges_are_one(edge_35, edge_24)
        res_not_sure = data_handler.two_edges_are_one(edge_12, edge_34)
        res_hit = data_handler.two_edges_are_one(edge_13, edge_24)
        res_hit_same_point = data_handler.two_edges_are_one(edge_12, edge_13)
        res_same_edge = data_handler.two_edges_are_one(edge_12, edge_12)

        # Assert
        self.assertFalse(res_not_sure)
        self.assertFalse(res_miss)
        self.assertTrue(res_hit)
        self.assertTrue(res_hit_same_point)
        self.assertTrue(res_same_edge)

    @patch('SupplementaryFiles.GameDataHandler.Utils')
    @patch('SupplementaryFiles.GameDataHandler.GameDataHandler.connect_nodes')
    @patch(
        'SupplementaryFiles.GameDataHandler.GameDataHandler.clean_connection')
    @patch('SupplementaryFiles.GameDataHandler.GraphObject.get_node_by_serial')
    def test_connect_edges_advanced(self, mock_get_node_by_serial, mock_clean,
                                    mock_connect, mock_utils):
        def mock_get_node(serial):
            for node in self.node_list:
                if node.serial_num == serial:
                    return node
            return None

        # Assemble
        mock_get_node_by_serial.side_effect = mock_get_node
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        edge_two_real_14 = (self.node_1_real, self.node_4_real,
                            self.node_1_real.slope(self.node_4_real),
                            LineEquation(slope=self.node_1_real.slope(
                                self.node_4_real),
                                         const=1,
                                         edge1=self.node_1_real,
                                         edge2=self.node_4_real))
        edge_left_real_13 = (self.node_1_real, self.node_3_unreal,
                             self.node_1_real.slope(self.node_3_unreal),
                             LineEquation(slope=self.node_1_real.slope(
                                 self.node_3_unreal),
                                          const=1,
                                          edge1=self.node_1_real,
                                          edge2=self.node_3_unreal))
        edge_left_real_12 = (self.node_1_real, self.node_2_unreal,
                             self.node_1_real.slope(self.node_2_unreal),
                             LineEquation(slope=self.node_1_real.slope(
                                 self.node_2_unreal),
                                          const=1,
                                          edge1=self.node_1_real,
                                          edge2=self.node_2_unreal))
        edge_right_real_24 = (self.node_2_unreal, self.node_4_real,
                              self.node_2_unreal.slope(self.node_4_real),
                              LineEquation(slope=self.node_2_unreal.slope(
                                  self.node_4_real),
                                           const=1,
                                           edge1=self.node_2_unreal,
                                           edge2=self.node_4_real))
        edge_two_unreal_23 = (self.node_2_unreal, self.node_3_unreal,
                              self.node_2_unreal.slope(self.node_3_unreal),
                              LineEquation(slope=self.node_2_unreal.slope(
                                  self.node_3_unreal),
                                           const=1,
                                           edge1=self.node_2_unreal,
                                           edge2=self.node_3_unreal))
        edge_two_unreal_13 = (self.node_1_unreal, self.node_3_unreal,
                              self.node_1_unreal.slope(self.node_3_unreal),
                              LineEquation(slope=self.node_1_unreal.slope(
                                  self.node_3_unreal),
                                           const=1,
                                           edge1=self.node_1_unreal,
                                           edge2=self.node_3_unreal))

        # Act + Assert

        # Case 1 - One edge is connected to two real nodes. Other edge only connects to one real node and the other
        #  to a fake node
        edge = data_handler.connect_edges(edge_two_real_14, edge_left_real_13)
        mock_connect.assert_called_with(self.node_1_real, self.node_4_real)
        self.assertEquals(edge_two_real_14[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_real_14[3].edge2, edge[3].edge2)

        # Case 2 - Both edges each connect to one real node and one fake node. Both real nodes are different
        edge = data_handler.connect_edges(edge_left_real_13,
                                          edge_right_real_24)
        self.assertEquals(edge_two_real_14[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_real_14[3].edge2, edge[3].edge2)

        # Case 3 - Both edges connect to the same real node. Both edge's other node is  different fake one
        edge = data_handler.connect_edges(edge_left_real_13, edge_left_real_12)
        mock_connect.assert_called_with(self.node_1_real, self.node_3_unreal)
        self.assertEquals(edge_left_real_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_left_real_13[3].edge2, edge[3].edge2)

        # Case 4 - One edge is connected to a real node and to a fake node. Other edge connects to two fake nodes.
        edge = data_handler.connect_edges(edge_left_real_12,
                                          edge_two_unreal_23)
        mock_connect.assert_called_with(self.node_1_real, self.node_3_unreal)
        self.assertEquals(edge_left_real_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_left_real_13[3].edge2, edge[3].edge2)

        # Case 5 - Both edge have two fake nodes.
        edge = data_handler.connect_edges(edge_two_unreal_13,
                                          edge_two_unreal_23)
        mock_connect.assert_called_with(self.node_1_unreal, self.node_3_unreal)
        self.assertEquals(edge_two_unreal_13[3].edge1, edge[3].edge1)
        self.assertEquals(edge_two_unreal_13[3].edge2, edge[3].edge2)

    def test_data_collection(self):
        # WIP
        return
        # Assemble
        new_graph = create_rand_graph("../graph_config.txt")
        gamer = GameDataHandler("../graph_config.txt")

        nodes_list = []
        edges = []

        def add_empty_edge(self, graph, nodes_list, edges_list):
            for node in graph.node_list:
                if node not in nodes_list:
                    for friend in node.neighbors:
                        if friend in [item.serial_num for item in nodes_list]:
                            tmp_node = graph.get_node_by_serial(friend)
                            fake_node_1 = NodeObject(serial=None,
                                                     location={
                                                         'x': node.x,
                                                         'y': node.y
                                                     },
                                                     size=node.size,
                                                     real=False)
                            fake_node_2 = NodeObject(serial=None,
                                                     location={
                                                         'x': tmp_node.x,
                                                         'y': tmp_node.y
                                                     },
                                                     size=tmp_node.size,
                                                     real=False)
                            edges_list.append((fake_node_1, fake_node_2))
                            return

        for i in range(3):
            nodes_list.append(new_graph.node_list[i])
        # Add an extra node that is connected to one of the existing ones

        for node in nodes_list[0].neighbors:
            if node not in nodes_list:
                nodes_list.append(new_graph.get_node_by_serial(node))
                break
        for node in nodes_list:
            for friend in node.neighbors:
                if friend in [item.serial_num for item in nodes_list]:
                    edges.append((node, new_graph.get_node_by_serial(friend)))
                else:
                    origin_node = new_graph.get_node_by_serial(friend)
                    fake_node = NodeObject(serial=None,
                                           location={
                                               'x': origin_node.x,
                                               'y': origin_node.y
                                           },
                                           size=origin_node.size,
                                           real=False)
                    edges.append((node, fake_node))

        # clean nodes
        for node in nodes_list:
            node.neighbors = set()
            node.possible_neighbors = set()

        # Add a few empty edges
        add_empty_edge(new_graph, nodes_list, edges)
        add_empty_edge(new_graph, nodes_list, edges)
        add_empty_edge(new_graph, nodes_list, edges)

        mock_reader.return_value = {'nodes': nodes_list, 'edges': edges}

        # act
        gamer.do_move()
        time.sleep(1)

    @patch('SupplementaryFiles.GameDataHandler.Utils')
    def test_trim_data(self, mock_utils):
        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        Utils.read_game_config_file(path.join("..", CONFIG_FILE_PATH))
        Utils.read_graph_config_file(path.join("..", GRAPH_CONFIG_PATH))
        data_handler = GameDataHandler(path.join("../", "graph_config.txt"),
                                       None)
        data_handler.graph.add_node(self.node_1_real.x,
                                    self.node_1_real.y,
                                    serial=self.node_1_real.serial_num)
        data_handler.graph.add_node(self.node_2_unreal.x,
                                    self.node_2_unreal.y,
                                    serial=self.node_2_unreal.serial_num)
        data_handler.graph.add_node(self.node_2_unreal_moved.x,
                                    self.node_2_unreal_moved.y,
                                    serial=self.node_2_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_3_unreal.x,
                                    self.node_3_unreal.y,
                                    serial=self.node_3_unreal.serial_num)
        data_handler.graph.add_node(self.node_3_unreal_moved.x,
                                    self.node_3_unreal_moved.y,
                                    serial=self.node_3_unreal_moved.serial_num)
        data_handler.graph.add_node(self.node_4_real.x,
                                    self.node_4_real.y,
                                    serial=self.node_4_real.serial_num)
        edge_1 = (self.node_1_real, self.node_2_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_2_unreal_moved))
        edge_2 = (self.node_2_unreal, self.node_3_unreal_moved, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_2_unreal,
                               edge2=self.node_3_unreal_moved))
        edge_3 = (self.node_3_unreal, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_3_unreal,
                               edge2=self.node_4_real))
        edge_4 = (self.node_1_real, self.node_4_real, 1,
                  LineEquation(slope=1,
                               const=0,
                               edge1=self.node_1_real,
                               edge2=self.node_4_real))
        data_handler.extra_edges = [edge_1, edge_2, edge_3]

        data_handler.trim_data()
        self.assertEquals(len(data_handler.edges_to_add), 1)
        self.assertEquals(data_handler.edges_to_add[0][0].serial_num,
                          edge_4[0].serial_num)
        self.assertEquals(data_handler.edges_to_add[0][1].serial_num,
                          edge_4[1].serial_num)
Example #14
0
    def test_two_edges_are_one(self, mock_utils):
        # Assemble
        node_1 = NodeObject(serial=1,
                            location={
                                'x': 100,
                                'y': 100
                            },
                            size=1,
                            real=True)
        node_2 = NodeObject(serial=2,
                            location={
                                'x': 200,
                                'y': 200
                            },
                            size=1,
                            real=True)
        node_3 = NodeObject(serial=3,
                            location={
                                'x': 300,
                                'y': 300
                            },
                            size=1,
                            real=True)
        node_4 = NodeObject(serial=4,
                            location={
                                'x': 400,
                                'y': 400
                            },
                            size=1,
                            real=True)
        node_5 = NodeObject(serial=4,
                            location={
                                'x': 100,
                                'y': 400
                            },
                            size=1,
                            real=True)

        mock_utils.graph_config_data = None
        mock_utils.game_config_data = {'Default': {'log_level': 'ERROR'}}
        data_handler = GameDataHandler(None, None)

        edge_35 = (node_3, node_5, node_3.slope(node_5),
                   LineEquation(slope=node_3.slope(node_5),
                                const=1,
                                edge1=node_3,
                                edge2=node_5))
        edge_24 = (node_2, node_4, node_2.slope(node_4),
                   LineEquation(slope=node_2.slope(node_4),
                                const=1,
                                edge1=node_2,
                                edge2=node_4))
        edge_12 = (node_1, node_2, node_1.slope(node_2),
                   LineEquation(slope=node_1.slope(node_2),
                                const=1,
                                edge1=node_1,
                                edge2=node_2))
        edge_13 = (node_1, node_3, node_1.slope(node_3),
                   LineEquation(slope=node_1.slope(node_3),
                                const=1,
                                edge1=node_1,
                                edge2=node_3))
        edge_34 = (node_3, node_4, node_3.slope(node_4),
                   LineEquation(slope=node_3.slope(node_4),
                                const=1,
                                edge1=node_3,
                                edge2=node_4))

        # Act

        res_miss = data_handler.two_edges_are_one(edge_35, edge_24)
        res_not_sure = data_handler.two_edges_are_one(edge_12, edge_34)
        res_hit = data_handler.two_edges_are_one(edge_13, edge_24)
        res_hit_same_point = data_handler.two_edges_are_one(edge_12, edge_13)
        res_same_edge = data_handler.two_edges_are_one(edge_12, edge_12)

        # Assert
        self.assertFalse(res_not_sure)
        self.assertFalse(res_miss)
        self.assertTrue(res_hit)
        self.assertTrue(res_hit_same_point)
        self.assertTrue(res_same_edge)
Example #15
0
 def create_graph(self):
     for i in range(self.node_count):
         self.nodeList.append(NodeObject())