class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok)
class TestRaycast(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.ball.set_position(Position(100, 0), 0) def test_raycast(self): self.assertTrue( raycast(self.game_state, Position(100, 100), 200, -pi / 2, BALL_RADIUS, [], [], False)) def test_raycast2(self): pass
class TestNode(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].update(Pose(Position(-4450, 0), 0)) self.tactic1 = GoalKeeper(self.game_state, 0) self.tactic2 = Stop(self.game_state, 1) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip("thinkin we should have generic tactic for test purpose, this is infuriating") def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand(0, AICommandType.STOP)#, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_str(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) expected_string = "Tactic: GoalKeeper Vertices: " for vertex in self.node1.vertices: expected_string += "\n " + str(vertex) self.assertEqual(str(self.node1), expected_string) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class Framework(object): """ La classe contient la logique nécessaire pour communiquer avec les différentes parties(simulation, vision, uidebug et/ou autres), maintenir l'état du monde (jeu, referree, debug, etc...) et appeller l'ia. """ def __init__(self): """ Constructeur de la classe, établis les propriétés de bases et construit les objets qui sont toujours necéssaire à son fonctionnement correct. """ # config self.cfg = ConfigService() # time self.last_frame_number = 0 self.time_stamp = None # time.time() self.last_camera_time = time.time() self.time_of_last_loop = time.time() self.ai_timestamp = float(self.cfg.config_dict["GAME"]["ai_timestamp"]) # thread self.ia_running_thread = None self.thread_terminate = threading.Event() # Communication self.robot_command_sender = None self.vision = None self.referee_command_receiver = None self.uidebug_command_sender = None self.uidebug_command_receiver = None self.uidebug_vision_sender = None self.uidebug_robot_monitor = None # because this thing below is a callable! can be used without being set self.vision_redirection_routine = lambda *args: None self.vision_routine = self._sim_vision # self._normal_vision # self._test_vision self._redirected_vision self._choose_vision_routines() # Debug self.incoming_debug = [] self.debug = DebugInterface() self.outgoing_debug = self.debug.debug_state self._init_communication() # Game elements self.reference_transfer_object = None self.game = None self.ai_coach = None self.referee = None self.team_color_service = None self._create_game_world() # VISION self.image_transformer = ImageTransformerFactory.get_image_transformer( ) # ia couplage self.ia_coach_mainloop = None self.ia_coach_initializer = None # for testing purposes self.frame_number = 0 # self.debug.add_log(1, "Framework started in {} s".format(time.time() - self.time_stamp)) def _choose_vision_routines(self): if self.cfg.config_dict["IMAGE"]["kalman"] == "true": self.vision_routine = self._kalman_vision def _init_communication(self): # first make sure we are not already running if self.ia_running_thread is None: # where do we send the robots command (serial for bluetooth and rf) self.robot_command_sender = RobotCommandSenderFactory.get_sender() # Referee self.referee_command_receiver = RefereeReceiver() # Vision self.vision = VisionReceiver() # do we use the UIDebug? if self.cfg.config_dict["DEBUG"]["using_debug"] == "true": self.uidebug_command_sender = UIDebugCommandSender() self.uidebug_command_receiver = UIDebugCommandReceiver() # Monitor robot if we are communicating with an actual robot self.uidebug_robot_monitor = UIDebugRobotMonitor( self.robot_command_sender, self.debug) else: self.stop_game() def game_thread_main_loop(self): """ Fonction exécuté et agissant comme boucle principale. """ self._wait_for_first_frame() self._wait_for_first_geometry_packet() while not self.thread_terminate.is_set(): self.time_stamp = time.time() self.vision_routine() time.sleep(0.01) def start_game(self, p_ia_coach_mainloop, p_ia_coach_initializer): """ Démarrage du moteur de l'IA initial, ajustement de l'équipe de l'ia et démarrage du/des thread/s""" # IA COUPLING self.ia_coach_mainloop = p_ia_coach_mainloop self.ia_coach_initializer = p_ia_coach_initializer # GAME_WORLD TEAM ADJUSTMENT self.team_color_service = TeamColorService() self.reference_transfer_object.team_color_svc = self.team_color_service print("Framework partie avec équipe", self.cfg.config_dict["GAME"]["our_color"]) self.ia_coach_initializer(self.reference_transfer_object) signal.signal(signal.SIGINT, self._sigint_handler) self.ia_running_thread = threading.Thread( target=self.game_thread_main_loop) self.ia_running_thread.start() self.ia_running_thread.join() def _create_game_world(self): """ Créé le GameWorld pour contenir les éléments d'une partie normale: l'arbitre, la Game (Field, teams, players). C'est un data transfer object pour les références du RULEngine vers l'IA """ self.referee = Referee() self.game = Game() self.game.set_referee(self.referee) self.reference_transfer_object = ReferenceTransferObject(self.game) self.reference_transfer_object.set_debug(self.incoming_debug) def _update_players_and_ball(self, vision_frame): """ Met à jour le GameState selon la frame de vision obtenue. """ time_delta = self._compute_vision_time_delta(vision_frame) # print(time_delta) self.game.update(vision_frame, time_delta) def _is_frame_number_different(self, vision_frame): # print(vision_frame.detection.frame_number) if vision_frame is not None: return vision_frame.detection.frame_number != self.last_frame_number else: return False def _compute_vision_time_delta(self, vision_frame): self.last_frame_number = vision_frame.detection.frame_number this_time = vision_frame.detection.t_capture # time.time() # vision_frame.detection.t_capture time_delta = this_time - self.last_camera_time self.last_camera_time = this_time # FIXME: hack return time_delta def _update_debug_info(self): self.incoming_debug += self.uidebug_command_receiver.receive_command() def _sim_vision(self): vision_frame = self._acquire_last_vision_frame() if vision_frame.detection.frame_number != self.last_frame_number: time_delta = self._compute_vision_time_delta(vision_frame) self.game.update(vision_frame, time_delta) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self._send_debug_commands() def _kalman_vision(self): vision_frames = self.vision.pop_frames() new_image_packet = self.image_transformer.update(vision_frames) referee_frames = self.referee_command_receiver.pop_frames() self.game.referee.update(referee_frames) new_time_delta = time.time() - self.time_of_last_loop if new_time_delta > self.ai_timestamp: time_delta = new_time_delta self.time_of_last_loop = time.time() self.game.update(new_image_packet, time_delta) self.game.field.update_field_dimensions(vision_frames) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self._send_debug_commands() #self._send_new_vision_packet() if time_delta > self.ai_timestamp * 1.3: warnings.warn( "Update loop took {:5.3f}s instead of {}s!".format( time_delta, self.ai_timestamp), RuntimeWarning, stacklevel=2) """ def _test_vision(self): vision_frames = self.vision.pop_frames() new_image_packet = self.image_transformer.update(vision_frames) if time.time() - self.last_loop > 0.05: time_delta = time.time() - self.last_time self.game.update_kalman(new_image_packet, time_delta) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self.game.set_command(robot_commands) self._send_debug_commands() self.last_loop = time.time() time.sleep(0) """ def _acquire_last_vision_frame(self): return self.vision.get_latest_frame() def _acquire_all_vision_frames(self): return self.vision.pop_frames() def stop_game(self): """ Nettoie les ressources acquises pour pouvoir terminer l'exécution. """ self.thread_terminate.set() self.ia_running_thread.join() self.thread_terminate.clear() self.robot_command_sender.stop() if self.uidebug_robot_monitor: self.uidebug_robot_monitor.stop() try: team = self.game.friends # FIXME: hack for grsim for player in team.available_players.values(): if player.ai_command is not None: command = Stop(player) self.robot_command_sender.send_command(command) except Exception as e: print("Could not stop players") print("Au nettoyage il a été impossible d'arrêter les joueurs.") raise e # raise StopPlayerError("Au nettoyage il a été impossible d'arrêter les joueurs.") def _wait_for_first_frame(self): while not self.vision.get_latest_frame( ) and not self.thread_terminate.is_set(): time.sleep(0.1) print("En attente d'une image de la vision.") def _wait_for_first_geometry_packet(self): while not self.game.field.update_field_dimensions(self.vision.pop_frames()) and\ not self.thread_terminate.is_set(): time.sleep(0.01) print("En attente du premier packet de géométrie du terrain.") def _send_robot_commands(self, commands): """ Envoi les commades des robots au serveur. """ for command in commands: self.robot_command_sender.send_command(command) def _send_debug_commands(self): """ Envoie les commandes de debug au serveur. """ packet_represented_commands = [ c.get_packet_repr() for c in self.outgoing_debug ] if self.uidebug_command_sender is not None: self.uidebug_command_sender.send_command( packet_represented_commands) self.incoming_debug.clear() self.outgoing_debug.clear() # for testing purposes def _send_new_vision_packet(self): pb_sslwrapper = ssl_wrapper.SSL_WrapperPacket() pb_sslwrapper.detection.camera_id = 0 pb_sslwrapper.detection.t_sent = 0 pck_ball = pb_sslwrapper.detection.balls.add() pck_ball.x = self.game.field.ball.position.x pck_ball.y = self.game.field.ball.position.y pck_ball.z = self.game.field.ball.position.z # required for the packet no use for us at this stage pck_ball.confidence = 0.999 pck_ball.pixel_x = self.game.field.ball.position.x pck_ball.pixel_y = self.game.field.ball.position.y for p in self.game.blue_team.available_players.values(): packet_robot = pb_sslwrapper.detection.robots_blue.add() packet_robot.confidence = 0.999 packet_robot.robot_id = p.id packet_robot.x = p.pose.position.x packet_robot.y = p.pose.position.y packet_robot.orientation = p.pose.orientation packet_robot.pixel_x = 0. packet_robot.pixel_y = 0. for p in self.game.yellow_team.available_players.values(): packet_robot = pb_sslwrapper.detection.robots_yellow.add() packet_robot.confidence = 0.999 packet_robot.robot_id = p.id packet_robot.x = p.pose.position.x packet_robot.y = p.pose.position.y packet_robot.orientation = p.pose.orientation packet_robot.pixel_x = 0. packet_robot.pixel_y = 0. self.frame_number += 1 pb_sslwrapper.detection.t_capture = 0 pb_sslwrapper.detection.frame_number = self.frame_number try: self.vision_redirection_routine(pb_sslwrapper.SerializeToString()) except: print("Fail to send in vision redirection") def _sigint_handler(self, *args): self.stop_game()
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") def test_move_to(self): self.pose = Pose(Position(0, 0, 0), orientation=0.0) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual(self.move.exec(), AICommand(1, AICommandType.MOVE, **{"pose_goal": self.pose})) self.pose = Pose(Position(0.5, 0.3, 0.2), orientation=3.2) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual(MoveToPosition.exec(self.move), AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": self.pose})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.player_id) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = get_angle(self.game_state.get_player_pose(self.player_id).position, ball_position) destination_pose = {"pose_goal": Pose(ball_position, destination_orientation)} ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(-5, 5), 0) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(Pose(Position(-5, 5), 3*pi/4), 0) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("LAZY ME OH HELL") def test_MoveWithBall(self): self.move_with_ball = MoveToDribblingBall(self.game_state, self.player_id, Position(100, 0)) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(5, 2), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), atan(2/5))}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(0, 100), pi/2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(250, 250), -3*pi/4)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-60, 50), 0.5235)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-179, 25), -pi/2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.player_id, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273, -415), 0.9882)}) # AICommand(Pose(Position(-273, -415), 0.9882), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.player_id, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249), 1.5707)}) # AICommand(Pose(Position(1000, -249), 1.5707), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.player_id, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(675, -200), -3.1415)}) self.assertEqual(aicmd_obtenu, aicmd_cible) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_kick(self): # test avec la valeur 0 (nulle) self.kick = Kick(self.game_state, self.player_id, 0) current_pose = self.game_state.get_player_pose(self.player_id) current_pose_string = AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose}) self.assertEqual(Kick.exec(self.kick), current_pose_string) # test avec la valeur 1 (force maximale) self.kick = Kick(self.game_state, self.player_id, 1) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual(Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) # test avec la valeur 0.3 (force intermediaire) self.kick = Kick(self.game_state, self.player_id, 0.3) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual(Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestGraph(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID) self.tactic1 = Stop(self.game_state, self.a_player) self.tactic2 = GoToPositionNoPathfinder(self.game_state, self.a_player, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = Idle(self.game_state, self.a_player).exec() self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT)
class TestGraph(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.tactic1 = Stop(self.game_state, 1) self.tactic2 = GoToPositionNoPathfinder(self.game_state, 0, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = AICommand(1, AICommandType.STOP) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand(0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT) def test_str(self): expected_string = "" for i in range(len(self.graph1.nodes)): expected_string += "Node " + str(i) + ": " + str(self.graph1.nodes[i]) + "\n" self.assertEqual(str(self.graph1), expected_string)
class TestNode(unittest.TestCase): def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].pose = Pose( Position(-4450, 0), 0) self.tactic1 = GoalKeeper( self.game_state, self.game_state.game.friends.players[A_GOAL_PLAYER_ID]) self.tactic2 = Stop(self.game_state, self.game_state.game.friends.players[A_PLAYER_ID]) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) self.a_player = OurPlayer(TeamColor.BLUE, 0) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual( len(self.node1.vertices), 1 ) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip( "With the current implemention of aiCommand, testing this is more or less impossible" ) def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand( self.a_player, AICommandType.STOP ) #, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.YELLOW_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = GameWorld(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager2.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = GameWorld(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = GameWorld(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) def test_get_player_pose(self): self.assertIs(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[0].pose) self.assertIs(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[1].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[1].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.friends.players[0].pose) def test_get_player_position(self): self.assertIs(self.GameStateManager1.get_player_position(0, True), self.game.friends.players[0].pose.position) self.assertIs(self.GameStateManager2.get_player_position(0, False), self.game.enemies.players[0].pose.position)
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") def test_move_to(self): self.pose = Pose(Position(0, 0, 0), orientation=0.0) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual( self.move.exec(), AICommand(1, AICommandType.MOVE, **{"pose_goal": self.pose})) self.pose = Pose(Position(0.5, 0.3, 0.2), orientation=3.2) self.move = MoveToPosition(self.game_state, self.player_id, self.pose) self.assertEqual( MoveToPosition.exec(self.move), AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": self.pose})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.player_id) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = get_angle( self.game_state.get_player_pose(self.player_id).position, ball_position) destination_pose = { "pose_goal": Pose(ball_position, destination_orientation) } ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(-5, 5), 0) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(Pose(Position(-5, 5), 3 * pi / 4), 0) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("LAZY ME OH HELL") def test_MoveWithBall(self): self.move_with_ball = MoveToDribblingBall(self.game_state, self.player_id, Position(100, 0)) self.game_state.game.ball.set_position(Position(5, 0), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) self.game_state.game.ball.set_position(Position(5, 2), 0) ai_cmd = self.move_with_ball.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), atan(2 / 5))}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(0, 100), pi / 2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(250, 250), -3 * pi / 4)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-60, 50), 0.5235)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-179, 25), -pi / 2)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.player_id, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273, -415), 0.9882)}) # AICommand(Pose(Position(-273, -415), 0.9882), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.player_id, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249), 1.5707)}) # AICommand(Pose(Position(1000, -249), 1.5707), 0) self.assertEqual(aicmd_obtenu, aicmd_cible) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.player_id, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(675, -200), -3.1415)}) self.assertEqual(aicmd_obtenu, aicmd_cible) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_kick(self): # test avec la valeur 0 (nulle) self.kick = Kick(self.game_state, self.player_id, 0) current_pose = self.game_state.get_player_pose(self.player_id) current_pose_string = AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose}) self.assertEqual(Kick.exec(self.kick), current_pose_string) # test avec la valeur 1 (force maximale) self.kick = Kick(self.game_state, self.player_id, 1) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual( Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) # test avec la valeur 0.3 (force intermediaire) self.kick = Kick(self.game_state, self.player_id, 0.3) current_pose = self.game_state.get_player_pose(self.player_id) self.assertEqual( Kick.exec(self.kick), AICommand(self.player_id, AICommandType.KICK, **{"pose_goal": current_pose})) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestActions(unittest.TestCase): def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID) def test_move_to(self): A_CRUISE_SPEED = 0.1 self.pose = Pose(Position(0, 0), 0.0) self.move = MoveToPosition(self.game_state, self.a_player, self.pose, False, A_CRUISE_SPEED) return_cmd = self.move.exec() expected_cmd = AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": self.pose, "pathfinder_on": False, "cruise_speed": A_CRUISE_SPEED }) self.assertEqual(return_cmd, expected_cmd) self.pose = Pose(Position(0.5, 0.3), 3.2) self.move = MoveToPosition(self.game_state, self.a_player, self.pose, False, A_CRUISE_SPEED) self.assertEqual( MoveToPosition.exec(self.move), AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": self.pose, "pathfinder_on": False, "cruise_speed": A_CRUISE_SPEED })) def test_idle(self): idle = Idle(self.game_state, self.a_player) expected_command = AICommand( self.a_player, AICommandType.STOP, control_loop_type=AIControlLoopType.POSITION) actual_command = Idle.exec(idle) self.assertEqual(actual_command, expected_command) def test_GrabBall(self): self.grab_ball = GetBall(self.game_state, self.a_player) self.game_state.set_ball_position(Position(5, 0), A_DELTA_T) ai_cmd = self.grab_ball.exec() ball_position = self.game_state.get_ball_position() destination_orientation = (ball_position - self.a_player.pose.position).angle() destination_pose = Pose(ball_position, destination_orientation) ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": destination_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) grab_pose = Pose(Position(-5, 5), 3 * pi / 4) self.game_state.set_ball_position(Position(-5, 5), A_DELTA_T) ai_cmd = self.grab_ball.exec() ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": grab_pose}) self.assertEqual(ai_cmd, ai_cmd_expected) @unittest.skip("GoBetween does not actually go in between") def test_GoBetween(self): # test avec une droite verticale POS_TOP = Position(100, 100) POS_BOTTOM = Position(100, -100) POS_TARGET = Position(200, 0) POS_INBETWEEN = Position(100, 0) self.go_between = GoBetween(self.game_state, self.a_player, POS_TOP, POS_BOTTOM, POS_TARGET) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(POS_INBETWEEN, 0) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.a_player, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(0, 100), pi / 2) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite quelconque self.go_between = GoBetween(self.game_state, self.a_player, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(250, 250), -3 * pi / 4) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position1 self.go_between = GoBetween(self.game_state, self.a_player, Position(1000, 75), Position(1500, -250), Position(0, 0), 0) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(1000, 75), -3.067) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.a_player, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(-60, 50), 0.5235) self.assertEqual(ai_cmd, ai_cmd_expected) # test correction pour respecter la distance minimale self.go_between = GoBetween(self.game_state, self.a_player, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec().pose_goal ai_cmd_expected = Pose(Position(-179, 25), -pi / 2) self.assertEqual(ai_cmd, ai_cmd_expected) # test distance entre les positions insuffisantes self.assertRaises(AssertionError, GoBetween, self.game_state, self.a_player, Position(1, 1), Position(-1, -1), 50) def test_GoBehind(self): # TODO: faire davantage de cas de test distance_behind = 500 # test avec une droite verticale self.go_behind = GoBehind(self.game_state, self.a_player, Position(1000, 250.3), Position(1000, 725.8), distance_behind) aicmd_obtenu = self.go_behind.exec() aicmd_expected = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(1000, -249.700), 1.5707)}) self.assertEqual(aicmd_obtenu, aicmd_expected) # test avec une droite quelconque self.go_behind = GoBehind(self.game_state, self.a_player, Position(1.5, 2.3), Position(18.3, 27.8), distance_behind) aicmd_obtenu = self.go_behind.exec() aicmd_expected = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(-273.579, -415.230), 0.9882)}) self.assertEqual(aicmd_obtenu, aicmd_expected) # test avec une droite horizontale self.go_behind = GoBehind(self.game_state, self.a_player, Position(175.8, -200.34), Position(-276.8, -200.34), distance_behind) aicmd_obtenu = GoBehind.exec(self.go_behind) aicmd_cible = AICommand( self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(675.800, 99.660), -2.601)}) self.assertEqual(aicmd_obtenu, aicmd_cible) def test_kick(self): # test avec la valeur 0 (nulle) target = Pose(Position(1, 1)) ball_position = Position(5, 0) self.game_state.set_ball_position(ball_position, A_DELTA_T) expected_cmd = AICommand( self.a_player, AICommandType.MOVE, **{ "pose_goal": Pose(ball_position, 0.785), "charge_kick": True, "kick": True, "pathfinder_on": True, "cruise_speed": 0.1, "end_speed": 0 }) return_cmd = Kick(self.game_state, self.a_player, force=0, target=target).exec() self.assertEqual(expected_cmd, return_cmd) # test avec la valeur 1 (force maximale) expected_cmd.kick_strength = 1 return_cmd = Kick(self.game_state, self.a_player, 1, target=target).exec() self.assertEqual(return_cmd, expected_cmd) # test avec la valeur 0.3 (force intermediaire) expected_cmd.kick_strength = 0.3 return_cmd = Kick(self.game_state, self.a_player, 0.3, target=target).exec() self.assertEqual(return_cmd, expected_cmd) @unittest.skip("I got lazy, didn't want to review all of the protectgoal.") def test_ProtectGoal(self): # test de base self.game_state.game.friends.players[0].update(Pose(Position(4450, 10))) self.game_state.game.ball.set_position(Position(0, 0), 0) self.protectGoal = ProtectGoal(self.game_state, 0) aicmd_obtenu = self.protectGoal.exec() aicmd_cible = AICommand(self.a_player, AICommandType.MOVE, **{"pose_goal": Pose(Position(4000, 0), -pi)}) self.assertEqual(aicmd_obtenu, aicmd_cible) # test distance max < distance min self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0, True, 50, 40)
class TestNode(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].update( Pose(Position(-4450, 0), 0)) self.tactic1 = GoalKeeper(self.game_state, 0) self.tactic2 = Stop(self.game_state, 1) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) def test_init(self): self.assertRaises(AssertionError, Node, "not a tactic") self.assertIsInstance(self.node2.tactic, Tactic) self.assertEqual(len(self.node2.vertices), 0) def test_add_vertex(self): self.assertRaises(AssertionError, self.node1.add_vertex, "not a vertex") self.node1.add_vertex(self.vertex1) self.assertEqual(len(self.node1.vertices), 1) self.node1.add_vertex(self.vertex1) self.assertEqual( len(self.node1.vertices), 1 ) # il ne peut y avoir qu'un vertex entre deux noeuds dans un sens self.node1.add_vertex(self.vertex2) self.assertEqual(len(self.node1.vertices), 2) def test_remove_vertex(self): self.assertRaises(AssertionError, self.node1.remove_vertex, "not an int") self.assertRaises(AssertionError, self.node1.remove_vertex, -1) self.node1.add_vertex(self.vertex1) self.node1.remove_vertex(420) self.assertEqual(len(self.node1.vertices), 1) self.node1.remove_vertex(0) self.assertEqual(len(self.node1.vertices), 0) @unittest.skip( "thinkin we should have generic tactic for test purpose, this is infuriating" ) def test_exec(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) next_ai_command, next_node = self.node1.exec() self.assertEqual(next_node, 0) expected_aicmd = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(-4000, 0), 0)}) self.assertEqual(next_ai_command, expected_aicmd) self.node2.add_vertex(self.vertex2) expected_aicmd = AICommand( 0, AICommandType.STOP ) #, **{"pose_goal": Pose(Position(-4000, 0), 0)}) next_ai_command, next_node = self.node2.exec() self.assertEqual(next_ai_command, expected_aicmd) self.assertEqual(next_node, -1) def test_str(self): self.node1.add_vertex(self.vertex1) self.node1.add_vertex(self.vertex2) expected_string = "Tactic: GoalKeeper Vertices: " for vertex in self.node1.vertices: expected_string += "\n " + str(vertex) self.assertEqual(str(self.node1), expected_string) def test_set_flag(self): self.assertRaises(AssertionError, self.node1.set_flag, "not a flag") self.node1.set_flag(Flags.SUCCESS) self.assertEqual(self.node1.tactic.status_flag, Flags.SUCCESS)
class TestGraph(unittest.TestCase): def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.tactic1 = Stop(self.game_state, 1) self.tactic2 = GoToPositionNoPathfinder(self.game_state, 0, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo) def test_init(self): self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(len(self.empty_graph.nodes), 0) def test_get_current_tactic_name(self): self.assertEqual(self.graph1.get_current_tactic_name(), "Stop") self.assertEqual(self.empty_graph.get_current_tactic_name(), None) self.empty_graph.add_node(self.node2) self.assertEqual(self.empty_graph.get_current_tactic_name(), "GoToPositionNoPathfinder") def test_get_current_tactic(self): self.assertIsInstance(self.graph1.get_current_tactic(), Stop) self.assertEqual(self.empty_graph.get_current_tactic(), None) self.empty_graph.add_node(self.node2) self.assertIsInstance(self.empty_graph.get_current_tactic(), GoToPositionNoPathfinder) def test_add_node(self): self.assertEqual(len(self.graph1.nodes), 2) self.assertRaises(AssertionError, self.graph1.add_node, "not a node") def test_remove_node(self): self.assertRaises(AssertionError, self.graph1.remove_node, "not an int") self.assertRaises(AssertionError, self.graph1.remove_node, -1) self.assertRaises(AssertionError, self.graph1.remove_node, 420) self.graph1.remove_node(1) self.assertEqual(len(self.graph1.nodes), 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) def test_add_vertex(self): self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.assertRaises(AssertionError, self.graph1.add_vertex, "not an int", 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, -1, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 420, 1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, "not an int", foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, -1, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 420, foo) self.assertRaises(AssertionError, self.graph1.add_vertex, 0, 1, "not a callable") self.graph1.add_vertex(0, 1, foo) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) def test_remove_vertex(self): self.assertRaises(AssertionError, self.graph1.remove_vertex, "not an int", 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, -1, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 420, 1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, "not an int") self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, -1) self.assertRaises(AssertionError, self.graph1.remove_vertex, 0, 420) self.graph1.add_node(self.node2) self.graph1.remove_vertex(0, 2) self.assertEqual(len(self.graph1.nodes[0].vertices), 1) self.graph1.remove_vertex(0, 1) self.assertEqual(len(self.graph1.nodes[0].vertices), 0) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = AICommand(1, AICommandType.STOP) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(next_ai_command, expected_ai_command) self.assertRaises(EmptyGraphException, self.empty_graph.exec) self.empty_graph.add_node(self.node2) self.empty_graph.add_node(self.node1) self.empty_graph.add_vertex(0, 1, foo2) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) next_ai_command = self.empty_graph.exec() expected_ai_command = AICommand( 0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))}) self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command) def test_set_current_node(self): self.assertRaises(AssertionError, self.graph1.set_current_node, "not an int") self.assertRaises(AssertionError, self.graph1.set_current_node, -1) self.assertRaises(AssertionError, self.graph1.set_current_node, 420) self.graph1.nodes[0].set_flag(Flags.WIP) self.graph1.set_current_node(1) self.assertEqual(self.graph1.current_node, 1) self.assertEqual(self.graph1.nodes[0].tactic.status_flag, Flags.INIT) def test_str(self): expected_string = "" for i in range(len(self.graph1.nodes)): expected_string += "Node " + str(i) + ": " + str( self.graph1.nodes[i]) + "\n" self.assertEqual(str(self.graph1), expected_string)
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) def test_get_player_pose(self): self.assertIs(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[0].pose) self.assertIs(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[1].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[1].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.friends.players[0].pose) def test_get_player_position(self): self.assertIs(self.GameStateManager1.get_player_position(0, True), self.game.friends.players[0].pose.position) self.assertIs(self.GameStateManager2.get_player_position(0, False), self.game.enemies.players[0].pose.position)