diff --git a/rlbot_gui/bot_management/bot_creation.py b/rlbot_gui/bot_management/bot_creation.py index aaa3e97b..e40ddd85 100644 --- a/rlbot_gui/bot_management/bot_creation.py +++ b/rlbot_gui/bot_management/bot_creation.py @@ -105,3 +105,37 @@ def bootstrap_scratch_bot(bot_name, directory): replace_all(config_file, r'port = .*$', 'port = ' + str(random.randint(20000, 65000))) return config_file + + +def bootstrap_python_hivemind(hive_name, directory): + sanitized_name = convert_to_filename(hive_name) + bot_directory = Path(directory or '.') + top_dir = bot_directory / sanitized_name + if os.path.exists(top_dir): + raise FileExistsError(f'There is already a bot named {sanitized_name}, please choose a different name!') + + with tempfile.TemporaryDirectory() as tmpdirname: + tmpdir = Path(tmpdirname) + print('created temporary directory', tmpdir) + + download_and_extract_zip( + download_url='https://github.com/ViliamVadocz/RLBotPythonHivemindExample/archive/master.zip', + local_folder_path=tmpdir) + + safe_move(tmpdir / 'RLBotPythonHivemindExample-master', top_dir) + + + config_file = top_dir / 'config.cfg' + drone_file = top_dir / 'src' / 'drone.py' + hive_file = top_dir / 'src' / 'hive.py' + + replace_all(config_file, r'name = .*$', f'name = {hive_name}') + replace_all(drone_file, r'hive_name = .*$', f'hive_name = "{hive_name} Hivemind"') + replace_all(drone_file, r'hive_key = .*$', f'hive_key = "{random.randint(100000, 999999) + hash(hive_name)}"') + replace_all(hive_file, r'class .*\(PythonHivemind\)', f'class {hive_name}Hivemind(PythonHivemind)') + + # This is intended to open the example python file in the default system editor for .py files. + # Hopefully this will be VS Code or notepad++ or something. If it gets executed as a python script, no harm done. + os.startfile(hive_file) + + return config_file diff --git a/rlbot_gui/gui.py b/rlbot_gui/gui.py index 4f038cf7..669cfe0a 100644 --- a/rlbot_gui/gui.py +++ b/rlbot_gui/gui.py @@ -17,7 +17,7 @@ boost_strength_mutator_types, gravity_mutator_types, demolish_mutator_types, respawn_time_mutator_types, \ existing_match_behavior_types -from rlbot_gui.bot_management.bot_creation import bootstrap_python_bot, bootstrap_scratch_bot, convert_to_filename +from rlbot_gui.bot_management.bot_creation import bootstrap_python_bot, bootstrap_scratch_bot, bootstrap_python_hivemind, convert_to_filename from rlbot_gui.bot_management.downloader import BotpackDownloader, get_json_from_url from rlbot_gui.match_runner.match_runner import hot_reload_bots, shut_down, start_match_helper, \ do_infinite_loop_content, spawn_car_in_showroom, set_game_state, fetch_game_tick_packet @@ -419,6 +419,17 @@ def begin_scratch_bot(bot_name): return {'error': str(e)} +@eel.expose +def begin_python_hivemind(hive_name): + bot_directory = ensure_bot_directory() + + try: + config_file = bootstrap_python_hivemind(hive_name, bot_directory) + return {'bots': load_bundle(config_file)} + except FileExistsError as e: + return {'error': str(e)} + + @eel.expose def set_state(state): set_game_state(state) diff --git a/rlbot_gui/gui/main.vue b/rlbot_gui/gui/main.vue index b6f2f784..9178fe51 100644 --- a/rlbot_gui/gui/main.vue +++ b/rlbot_gui/gui/main.vue @@ -337,6 +337,7 @@