Browse Source

feat(game): define server config via arguments instead

Bryan Lee 1 year ago
parent
commit
681c6951d3
4 changed files with 40 additions and 19 deletions
  1. 5 5
      README.md
  2. 18 8
      project/game/game.gd
  3. 4 5
      project/game/game_server.gd
  4. 13 1
      project/program.gd

+ 5 - 5
README.md

@@ -83,12 +83,12 @@ An NGINX proxy provides TLS by forwarding ports defined below:
 | --------------------- | ------------------- | --------- | ---------------- |
 | `19000-19249`         | `9000-9249`         | WebSocket | The game server. |
 
-### Environment Variables
+### Configuration
 
-| Variable    | Description                                      | Default value |
-| ----------- | ------------------------------------------------ | ------------- |
-| `SERVER_ID` | The unique ID to identify the server in logs.    | `randi()`     |
-| `PORT`      | The port that the server listens for clients on. | `9000`        |
+| Argument      | Description                                      | Default value |
+| ------------- | ------------------------------------------------ | ------------- |
+| `--server-id` | The unique ID to identify the server in logs.    | `randi()`     |
+| `--port`      | The port that the server listens for clients on. | `9000`        |
 
 ## Game Client
 

+ 18 - 8
project/game/game.gd

@@ -5,25 +5,34 @@ class_name Game
 
 
 func _ready() -> void:
+	# Production server build.
 	if Program.is_dedicated_server:
 		var server_result := _start_server()
 		if server_result.is_err():
 			OS.kill(OS.get_process_id())
 		return
 	
-	if OS.is_debug_build():
-		# In debug builds, we try loading the server on all programs since only one
-		# program will be able to bind to the server port. This leaves us with just
-		# one server.
-		var server_result := _start_server()
-		if server_result.is_err():
-			_start_client()
+	# Production client build.
+	if not OS.is_debug_build():
+		var client_result := _start_client()
+		print(client_result)
 		return
 	
-	# Production client build.
+	#_try_debug_server_or_client()
 	_start_client()
 
 
+func _try_debug_server_or_client() -> void:
+	# In debug builds, we try loading the server on all programs since only one
+	# program will be able to bind to the server port. This leaves us with just
+	# one server.
+	var server_result := _start_server()
+	print(server_result)
+	if server_result.is_err():
+		var client_result := _start_client()
+		print(client_result)
+
+
 ## [codeblock]
 ## @returns Result<null, int>
 ## [/codeblock]
@@ -50,6 +59,7 @@ func _start_client() -> Result:
 	var start_result := Program.game_client.start()
 	if start_result.is_err():
 		Program.game_client = null
+		return start_result
 	Logger.client_log(["started client"], ["init"])
 
 	return start_result

+ 4 - 5
project/game/game_server.gd

@@ -1,12 +1,11 @@
 extends Node
 class_name GameServer
 
-const _DEFAULT_PORT := 9000
-var env_port := OS.get_environment("PORT")
-var port := int(env_port) if env_port else _DEFAULT_PORT
+var port_str: String = Program.cmdline_args.get("port", "9000")
+var port := int(port_str)
 
-var env_id := OS.get_environment("SERVER_ID")
-var id := int(env_id) if env_id else randi()
+var id_str: String = Program.cmdline_args.get("server-id")
+var id := int(id_str) if id_str else randi()
 
 var peer := WebSocketMultiplayerPeer.new()
 var world_spawner: MultiplayerSpawner

+ 13 - 1
project/program.gd

@@ -1,6 +1,18 @@
 extends Node
 
-var is_dedicated_server := "--server" in OS.get_cmdline_args()
+
+func _get_cmdline_args() -> Dictionary:
+	var args := {}
+	for arg in OS.get_cmdline_args():
+		if arg.find("=") > -1:
+			var key_value = arg.lstrip("-").split("=")
+			args[key_value[0]] = key_value[1]
+		else:
+			args[arg.lstrip("-")] = true
+	return args
+
+var cmdline_args := _get_cmdline_args()
+var is_dedicated_server := "server" in cmdline_args
 
 var main: Main
 var game_server: GameServer