def bot_routine(user_id: int, game_id: int, token: str) -> None: """mod: this function is main function of the module.""" current_map = Map() path_finder = PathFinder() start_position = None next_move = None turbo_flag = False current_state_response = None game_status_flag = True def send_current_state_request() -> None: """mod: this function sends REST API request.""" global api_gateway_urls, get_current_state_method nonlocal user_id, game_id, token, current_state_response current_state_response = requests.get( api_gateway_urls + get_current_state_method, params={'GameId': game_id}) current_state_response.raise_for_status() def send_post_move_request() -> None: global api_gateway_urls, post_move_method """mod: this function sends REST API request.""" nonlocal start_position, next_move, turbo_flag, user_id, game_id, token direction = None if next_move.row_index > start_position.row_index: direction = 'UP' elif next_move.row_index < start_position.row_index: direction = 'DOWN' elif next_move.column_index > start_position.column_index: direction = 'RIGHT' elif next_move.column_index < start_position.column_index: direction = 'LEFT' response = requests.post( api_gateway_urls + post_move_method, json={'Direction': direction, 'UserID': user_id, 'TurboFlag': turbo_flag}) response.raise_for_status() def send_unregister_user_request() -> None: """mod: this function sends REST API request.""" global api_gateway_urls, unregister_user_method nonlocal token response = requests.delete(api_gateway_urls + unregister_user_method, params={"token": token}) response.raise_for_status() def parse_current_state_response() -> None: """mod: this function parses REST API response.""" nonlocal current_state_response, current_map, path_finder, start_position, game_status_flag, user_id game_state = json.loads(current_state_response.form.get('data')) time_to_update = 0 current_map.map_scheme['height'] = game_state['map']['height'] current_map.map_scheme['width'] = game_state['map']['width'] current_map.obstacles_positions = game_state['map']['obstacles'] current_map.bots_positions = game_state['map']['power-ups'] current_map.traces_positions = game_state['map']['tracers'] path_finder.tron_map = current_map players_positions = list() for player in game_state['players']: if player['id'] == user_id: start_position = player['headPosition'] path_finder.start_position = start_position path_finder.turbos_number = player['turboAmount'] time_to_update = player["timeToUpdate"] else: players_positions.append(player['headPosition']) current_map.players_positions = players_positions if not time_to_update: game_status_flag = False elif time_to_update < 1: path_finder.algorithm = 'Random' elif time_to_update < 2: path_finder.algorithm = 'Survival' else: path_finder.algorithm = 'A*' while True: try: send_current_state_request() parse_current_state_response() if not game_status_flag: send_unregister_user_request() return try: next_move, turbo_flag = path_finder.get_direction() send_post_move_request() except NoSolution: continue except requests.exceptions.HTTPError as http_err: print(f'HTTP error occurred: {http_err}') return
class MyTestCase(unittest.TestCase): def setUp(self) -> None: self.current_map = Map() self.path_finder = PathFinder() self.path_finder.tron_map = self.current_map def arrangement_1(self): self.current_map.map_scheme['height'] = 64 self.current_map.map_scheme['width'] = 64 self.current_map.obstacles_positions = list() self.current_map.bots_positions = list() self.current_map.traces_positions = list() self.current_map.players_positions = [Position(63, 63)] self.path_finder.start_position = Position(0, 0) self.path_finder.turbos_number = 0 def arrangement_2(self): self.current_map.map_scheme['height'] = 16 self.current_map.map_scheme['width'] = 16 self.current_map.obstacles_positions = [Position(0, 1), Position(1, 1), Position(2, 1), Position(3, 1), Position(4, 1), Position(5, 1), Position(6, 1), Position(7, 1)] self.current_map.bots_positions = [Position(6, 0)] self.current_map.traces_positions = [Position(8, 0), Position(9, 0), Position(10, 0), Position(11, 0), Position(12, 0), Position(13, 0), Position(14, 0), Position(15, 0)] self.current_map.players_positions = [Position(15, 1)] self.path_finder.start_position = Position(7, 0) self.path_finder.turbos_number = 3 def arrangement_3(self): self.current_map.map_scheme['height'] = 64 self.current_map.map_scheme['width'] = 64 self.current_map.obstacles_positions = list() self.current_map.bots_positions = list() self.current_map.traces_positions = list() self.current_map.players_positions = [Position(63, 63)] self.path_finder.start_position = Position(0, 0) self.path_finder.turbos_number = 1 def arrangement_4(self): self.current_map.map_scheme['height'] = 16 self.current_map.map_scheme['width'] = 16 self.current_map.obstacles_positions = list() self.current_map.bots_positions = list() self.current_map.traces_positions = [Position(0, 7), Position(1, 7), Position(2, 7), Position(3, 7), Position(4, 7), Position(5, 7), Position(6, 7), Position(7, 7)] self.current_map.players_positions = [Position(0, 8)] self.path_finder.start_position = Position(3, 3) self.path_finder.turbos_number = 3 def arrangement_5(self): self.current_map.map_scheme['height'] = 16 self.current_map.map_scheme['width'] = 16 self.current_map.obstacles_positions = [Position(8, 8), Position(8, 9), Position(8, 10), Position(8, 11), Position(8, 12), Position(8, 13), Position(8, 14), Position(8, 15)] self.current_map.bots_positions = list() self.current_map.traces_positions = [Position(0, 7), Position(1, 7), Position(2, 7), Position(3, 7), Position(4, 7), Position(5, 7), Position(6, 7), Position(7, 7)] self.current_map.players_positions = [Position(0, 8)] self.path_finder.start_position = Position(10, 15) self.path_finder.turbos_number = 3 def test_get_direction_1(self): for algorithm in ('Random', 'Survival', 'A*'): self.path_finder.algorithm = algorithm with self.subTest(algorithm=algorithm, arrangement=1): self.arrangement_1() next_move, turbo_flag = self.path_finder.get_direction() self.assertTrue(next_move in self.current_map.get_possible_directions(self.path_finder.start_position)) self.assertFalse(turbo_flag) def test_get_direction_2(self): for algorithm in ('Random', 'Survival', 'A*'): self.path_finder.algorithm = algorithm with self.subTest(algorithm=algorithm, arrangement=2): self.arrangement_2() with self.assertRaises(NoSolution): self.path_finder.get_direction() def test_get_direction_3(self): for algorithm in ('Random', 'Survival', 'A*'): self.path_finder.algorithm = algorithm with self.subTest(algorithm=algorithm, arrangement=3): self.arrangement_3() next_move, turbo_flag = self.path_finder.get_direction() self.assertTrue(next_move in self.current_map.get_possible_directions(self.path_finder.start_position)) if algorithm == 'Random': self.assertFalse(turbo_flag) elif algorithm == 'Survival': self.assertFalse(turbo_flag) elif algorithm == 'A*': self.assertFalse(turbo_flag) else: raise Exception('Unknown algorithm') def test_get_direction_4(self): for algorithm in ('Random', 'Survival', 'A*'): self.path_finder.algorithm = algorithm with self.subTest(algorithm=algorithm, arrangement=4): self.arrangement_4() next_move, turbo_flag = self.path_finder.get_direction() self.assertTrue(next_move in self.current_map.get_possible_directions(self.path_finder.start_position)) if algorithm == 'Random': self.assertFalse(turbo_flag) elif algorithm == 'Survival': self.assertFalse(turbo_flag) elif algorithm == 'A*': self.assertTrue(turbo_flag) else: raise Exception('Unknown algorithm') def test_get_direction_5(self): for algorithm in ('Random', 'Survival', 'A*'): self.path_finder.algorithm = algorithm with self.subTest(algorithm=algorithm, arrangement=5): self.arrangement_5() next_move, turbo_flag = self.path_finder.get_direction() self.assertTrue(next_move in self.current_map.get_possible_directions(self.path_finder.start_position)) if algorithm == 'Random': self.assertFalse(turbo_flag) elif algorithm == 'Survival': self.assertFalse(turbo_flag) elif algorithm == 'A*': self.assertFalse(turbo_flag) else: raise Exception('Unknown algorithm')