Browse Source

refactor(game): consolidate all config variables to `Env`

Bryan Lee 1 year ago
parent
commit
999e5c9179

+ 3 - 0
project/.gitignore

@@ -1,2 +1,5 @@
 # Godot 4+ specific ignores
 .godot/
+
+# Environment files
+env.gd

+ 1 - 1
project/authentication/authentication.gd

@@ -161,7 +161,7 @@ func refresh_access_token() -> Result:
 	
 	is_token_refreshing = true
 	var request_result: Result = await HTTPUtils.fetch(
-		Program.AUTH_SERVER_URI + AUTH_SERVER_REFRESH_PATH,
+		Env.AUTH_SERVER_URI + AUTH_SERVER_REFRESH_PATH,
 		["Content-Type: application/json"],
 		HTTPClient.METHOD_POST,
 		JSON.stringify({ "refresh_token": refresh_token.unwrap() })

+ 2 - 2
project/authentication/providers/apple_game_center_auth_provider.gd

@@ -34,7 +34,7 @@ func server_sign_in() -> Result:
 	var id_signature = id_signature_result.unwrap()
 	
 	var request_result: Result = await HTTPUtils.fetch(
-		Program.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
+		Env.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
 		["Content-Type: application/json"],
 		HTTPClient.METHOD_POST,
 		JSON.stringify({
@@ -44,7 +44,7 @@ func server_sign_in() -> Result:
 			"timestamp": id_signature.timestamp,
 			"player_id": id_signature.player_id,
 			"user_name": user_name.unwrap_or(null),
-			"bundle_id": Program.IOS_BUNDLE_ID,
+			"bundle_id": Env.IOS_BUNDLE_ID,
 		}),
 	).settled
 	

+ 2 - 2
project/authentication/providers/google_play_games_auth_provider.gd

@@ -36,7 +36,7 @@ func server_sign_in() -> Result:
 	if provider_id.is_none():
 		return Result.Err("not authenticated locally")
 	
-	google_play_games.core.sign_in_client.request_server_side_access(Program.AUTH_SERVER_PLAY_GAMES_OAUTH_CLIENT_ID, false)
+	google_play_games.core.sign_in_client.request_server_side_access(Env.AUTH_SERVER_PLAY_GAMES_OAUTH_CLIENT_ID, false)
 	var auth_code_result = await google_play_games.core.sign_in_client.server_side_access_requested
 	var token_success: bool = auth_code_result[0]
 	if not token_success:
@@ -44,7 +44,7 @@ func server_sign_in() -> Result:
 	var auth_code: String = auth_code_result[1]
 
 	var request_result: Result = await HTTPUtils.fetch(
-		Program.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
+		Env.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
 		["Content-Type: text/plain"],
 		HTTPClient.METHOD_POST,
 		auth_code,

+ 2 - 2
project/authentication/providers/steam_auth_provider.gd

@@ -17,7 +17,7 @@ func initialize() -> Result:
 
 const AUTH_SERVER_SIGN_IN_PATH := "/auth/steam/sign-in"
 func server_sign_in() -> Result:
-	Steam.getAuthTicketForWebApi(Program.AUTH_SERVER_STEAM_IDENTITY)
+	Steam.getAuthTicketForWebApi(Env.AUTH_SERVER_STEAM_IDENTITY)
 	var ticket_payload = await Steam.get_ticket_for_web_api
 
 	var auth_ticket_result: Steam.Result = ticket_payload[1]
@@ -29,7 +29,7 @@ func server_sign_in() -> Result:
 	var encoded_auth_ticket = "".join(auth_ticket.map(func (b): return "%02X" % b))
 
 	var request_result: Result = await HTTPUtils.fetch(
-		Program.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
+		Env.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
 		["Content-Type: text/plain"],
 		HTTPClient.METHOD_POST,
 		encoded_auth_ticket,

+ 1 - 1
project/authentication/providers/web_oauth2_auth_provider.gd

@@ -39,7 +39,7 @@ func server_sign_in() -> Result:
 	var access_token = access_token_result.unwrap()
 	
 	var request_result: Result = await HTTPUtils.fetch(
-		Program.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
+		Env.AUTH_SERVER_URI + AUTH_SERVER_SIGN_IN_PATH,
 		["Authorization: Bearer %s" % access_token],
 		HTTPClient.METHOD_POST,
 	).settled

+ 10 - 0
project/env.sample.gd

@@ -0,0 +1,10 @@
+# Copy this file to env.gd and uncomment the class_name below
+# class_name Env
+
+const SSL_ENABLED := false
+const AUTH_SERVER_URI := "http://localhost:8000"
+const AUTH_SERVER_PLAY_GAMES_OAUTH_CLIENT_ID := ""
+const AUTH_SERVER_STEAM_IDENTITY := "authentication"
+const IOS_BUNDLE_ID := "com.example.game"
+const STEAM_APP_ID := 480
+const STEAM_GAME_ID := 480

+ 1 - 1
project/game/game_client.gd

@@ -19,7 +19,7 @@ func _init() -> void:
 ## @returns Result<null, int>
 ## [/codeblock]
 func start() -> Result:
-	var protocol := "wss://" if Program.SSL_ENABLED else "ws://"
+	var protocol := "wss://" if Env.SSL_ENABLED else "ws://"
 	var address := protocol + SERVER_HOST + ":" + str(SERVER_PORT)
 	Logger.client_log(["starting client connection to game server at: ", address], ["init"])
 	var start_result := Result.from_gderr(peer.create_client(address))

+ 0 - 8
project/program.gd

@@ -1,14 +1,6 @@
 extends Node
 
 var is_dedicated_server := "--server" in OS.get_cmdline_args()
-const SSL_ENABLED := false
-const AUTH_SERVER_URI := "http://localhost:8000"
-const AUTH_SERVER_PLAY_GAMES_OAUTH_CLIENT_ID := "865539732998-0ln5v17qagvfja9hlnb4rtf62ps21p2k.apps.googleusercontent.com"
-const AUTH_SERVER_STEAM_IDENTITY = "authentication"
-const IOS_BUNDLE_ID = "com.bryanmylee.multiplayer-base"
-const STEAM_APP_ID = 2843770
-const STEAM_GAME_ID = 2843770
-
 
 var main: Main
 var game_server: GameServer

+ 2 - 2
project/services/steam_service.gd

@@ -7,8 +7,8 @@ func _init() -> void:
 
 
 func initialize() -> Result:
-	OS.set_environment("SteamAppId", str(Program.STEAM_APP_ID))
-	OS.set_environment("SteamGameId", str(Program.STEAM_GAME_ID))
+	OS.set_environment("SteamAppId", str(Env.STEAM_APP_ID))
+	OS.set_environment("SteamGameId", str(Env.STEAM_GAME_ID))
 
 	Steam.steamInit()
 	if not Steam.isSteamRunning():