From 29bb6d7169bfa3e78166d8b5167cf74069d7bacc Mon Sep 17 00:00:00 2001 From: 2ManyProjects Date: Thu, 20 Feb 2025 22:25:25 -0600 Subject: [PATCH] Setup and server installation tested on Windows and Linux --- .../fairy-chess-server/engine-path.js | 2 ++ Systems/FairyStockfish/ServerManager.gd | 34 ++++++++++++++++++- export_presets.cfg | 2 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Assets/ChessEngines/fairy-chess-server/engine-path.js b/Assets/ChessEngines/fairy-chess-server/engine-path.js index bd4569f..1268e17 100644 --- a/Assets/ChessEngines/fairy-chess-server/engine-path.js +++ b/Assets/ChessEngines/fairy-chess-server/engine-path.js @@ -17,9 +17,11 @@ function getEnginePath() { // Get the current directory (fairy-chess-server) const currentDir = __dirname; + console.log(currentDir ) // Navigate up to Assets/ChessEngines const engineBaseDir = path.join(currentDir, '..', '..'); + console.log(engineBaseDir ) // Determine OS and set appropriate path if (os.platform() === 'win32') { diff --git a/Systems/FairyStockfish/ServerManager.gd b/Systems/FairyStockfish/ServerManager.gd index 5133b56..a6d1cff 100644 --- a/Systems/FairyStockfish/ServerManager.gd +++ b/Systems/FairyStockfish/ServerManager.gd @@ -6,6 +6,7 @@ var log_dir: String = "" var log_string: String = "" var running := false var server_process_id: int = -50 +var request_timer: Timer var server_url = "http://localhost:27531" @@ -121,17 +122,48 @@ func start_server() -> bool: add_child(http_request) # http_request.connect("request_completed", self._on_init_request_completed) http_request.request_completed.connect(self._on_init_request_completed) + + request_timer = Timer.new() + add_child(request_timer) + request_timer.wait_time = 2.0 # 2 seconds + request_timer.one_shot = true + request_timer.timeout.connect(self._on_request_timeout) + request_timer.start() http_request.request(server_url + "/health") return true + +func _on_request_timeout(): + write_log("Request timed out, starting server manually") + # Clean up timer + request_timer.queue_free() + request_timer = null + # Call your secondary processing function here + write_log("HTTP Request failed, starting server process") + write_log(server_path + "/index.js") + server_process_id = OS.create_process("node", [server_path + "/index.js"]) + write_log("SERVER PATH " + server_path) + if server_process_id <= 0: + printerr("Failed to start server") + write_log("ERROR: Failed to start server, process ID: " + str(server_process_id)) + + return false + + running = true + write_log("Chess server started with PID: " + str(server_process_id)) + return + func _on_init_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray): write_log("*****************_on_init_request_completed************") + if request_timer: + request_timer.stop() + request_timer.queue_free() + request_timer = null var json = JSON.new() json.parse(body.get_string_from_utf8()) var response = json.get_data() - # Will write_log the user agent string used by the HTTPRequest node (as recognized by httpbin.org). write_log("Init request completed") write_log("Result: " + str(response)) if response == null: diff --git a/export_presets.cfg b/export_presets.cfg index 566193e..8cad33b 100644 --- a/export_presets.cfg +++ b/export_presets.cfg @@ -47,7 +47,7 @@ advanced_options=true dedicated_server=false custom_features="" export_filter="all_resources" -include_filter="" +include_filter="Assets/*" exclude_filter="" export_path="build/Windows/ChessBuilder.exe" encryption_include_filters=""