from server_mc_manager import MinecraftServerManager from http import HTTPStatus import file_manager mc_manager: MinecraftServerManager = MinecraftServerManager() def account_create(port: str) -> HTTPStatus: try: file_manager.create_folder("users/" + port) return HTTPStatus.CREATED except Exception as e: print(f"Error creating account folder: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def server_create(port: str, name: str, version: str) -> HTTPStatus: server_path: str = f"users/{port}/{name}" server_template_path: str = "servers/paper/" + version try: file_manager.create_folder(server_path) file_manager.copy_folder_contents(server_template_path, server_path) file_manager.copy_folder_contents("servers/shared", server_path) file_manager.update_server_property(server_path + "/server.properties", "server-port", port) return HTTPStatus.CREATED except Exception as e: print(f"Error creating server: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def server_delete(port: str, name: str) -> HTTPStatus: server_path: str = f"users/{port}/{name}" try: file_manager.delete_non_empty_folder(server_path) return HTTPStatus.NO_CONTENT except Exception as e: print(f"Error deleting server: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def account_delete(port: str) -> HTTPStatus: try: file_manager.delete_non_empty_folder("users/" + port) return HTTPStatus.NO_CONTENT except Exception as e: print(f"Error deleting account: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def server_run(port: str, name: str) -> HTTPStatus: try: server_id = mc_manager.start_server(f"users/{port}/{name}") mc_manager.servers[server_id]['port'] = int(port) return HTTPStatus.ACCEPTED except Exception as e: print(f"Error starting server: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def server_stop(port: str, name: str) -> HTTPStatus: try: server_id = mc_manager.get_server_id_by_port(int(port)) mc_manager.stop_server(server_id) return HTTPStatus.ACCEPTED except Exception as e: print(f"Error stopping server: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR def update_property(port: str, name: str, prop: str, value: str) -> HTTPStatus: property_file_path: str = f"users/{port}/{name}/server.properties" try: file_manager.update_server_property(property_file_path, prop, value) return HTTPStatus.OK except ValueError as e: return HTTPStatus.BAD_REQUEST except Exception as e: print(f"Unhandled error: {type(e).__name__}, {str(e)}") return HTTPStatus.INTERNAL_SERVER_ERROR def run_command(port: str, command: str) -> HTTPStatus: try: server_id = mc_manager.get_server_id_by_port(int(port)) if server_id is None: return HTTPStatus.NOT_FOUND mc_manager.execute_server_command(server_id, command) return HTTPStatus.ACCEPTED except Exception as e: print(f"Error executing command: {e}") return HTTPStatus.INTERNAL_SERVER_ERROR