diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-11-27 14:36:20 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-11-27 14:36:20 +0100 |
commit | a854a1862a30632e49520f6e1e11333d5c8ff241 (patch) | |
tree | 2b10c4d19bb66b270a7726d660ed85d7fcf9482e /Game/Lobby/game_lobby.gd | |
parent | cb9bb9d1c757b53707c741a3c8c218b513ffb5ec (diff) |
next commit
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 |