diff options
Diffstat (limited to 'Game/Lobby/game_lobby.gd')
-rw-r--r-- | Game/Lobby/game_lobby.gd | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/Game/Lobby/game_lobby.gd b/Game/Lobby/game_lobby.gd new file mode 100644 index 0000000..6bfa8be --- /dev/null +++ b/Game/Lobby/game_lobby.gd @@ -0,0 +1,89 @@ +extends Node + + +class HTTPRequestConfig extends Resource: + var headers: PackedStringArray + var method: HTTPClient.Method + var data: Dictionary + var node: HTTPRequest + + @warning_ignore("shadowed_variable") + func _init( + headers := PackedStringArray(), + method := HTTPClient.METHOD_GET, + data := {}, + node := HTTPRequest.new() + ) -> void: + self.headers = headers + self.method = method + self.data = data + self.node = node + + +class HTTPResponse extends Resource: + var result_code: HTTPRequest.Result + var response_code: int + var headers: PackedStringArray + var body: PackedByteArray + + func parse_json() -> Dictionary: + return JSON.parse_string(body.get_string_from_utf8()) + + +var server_base_url: String = Client.get_config().get_value("general", "game_lobby_server_base_url") +var current_game_id: String +var current_secret: String + + +func request( + url: String, + config: HTTPRequestConfig = HTTPRequestConfig.new() +) -> HTTPResponse: + get_tree().root.add_child(config.node) + + if config.method == HTTPClient.METHOD_POST: + config.set("id", current_game_id) + config.set("secret", current_secret) + + var response = HTTPResponse.new() + config.node.request_completed.connect( + func ( + result_code: int, + response_code: int, + headers: PackedStringArray, + body: PackedByteArray + ): + response.result_code = result_code + response.response_code = response_code + response.headers = headers + response.body = body + + get_tree().root.remove_child(config.node) + ) + config.node.request( + "%s%s" % [server_base_url, url], + config.headers, + config.method, + JSON.stringify(config.data) + ) + + await config.node.request_completed + return response + + +func request_get(url: String, headers := PackedStringArray()): + var config := HTTPRequestConfig.new(headers) + + return await request(url, config) + + +func request_post(url: String, data: Dictionary = {}, headers := PackedStringArray()): + var config := HTTPRequestConfig.new(headers, HTTPClient.METHOD_POST, data) + + return await request(url, config) + + +func check_available() -> bool: + var response = await GameLobby.request_get("") + + return response.result_code == HTTPRequest.RESULT_SUCCESS |