From 8203d340f297aa358e68bd1dbd1e85945402901a Mon Sep 17 00:00:00 2001 From: Kesuaheli Date: Fri, 1 Nov 2024 18:49:12 +0100 Subject: [PATCH] added media endpoint for images --- scenes/api.tscn | 2 ++ scripts/api.gd | 22 ++++++++++++++++++- scripts/question.gd | 51 +++++++++++++++++++++------------------------ 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/scenes/api.tscn b/scenes/api.tscn index 8a59343..31a4238 100644 --- a/scenes/api.tscn +++ b/scenes/api.tscn @@ -19,6 +19,8 @@ script = ExtResource("1_l1n5m") [node name="HTTP_RoundInfo" type="HTTPRequest" parent="."] +[node name="HTTP_RoundMedia" type="HTTPRequest" parent="."] + [node name="HTTP_RoundNext" type="HTTPRequest" parent="."] [node name="HTTP_StreamerVote" type="HTTPRequest" parent="."] diff --git a/scripts/api.gd b/scripts/api.gd index d22a285..caa298e 100644 --- a/scripts/api.gd +++ b/scripts/api.gd @@ -8,6 +8,8 @@ var category_callback: Callable var game_start_callback: Callable var got_ws_message: Callable var login_callback: Callable +var round_media_callback: Callable +var round_media_media: String var round_next_callback: Callable var streamervote_callback: Callable @@ -26,6 +28,7 @@ func _ready(): $HTTP_Login.request_completed.connect(login_resp) $HTTP_Logout.request_completed.connect(logout_resp) $HTTP_RoundInfo.request_completed.connect(round_info_resp) + $HTTP_RoundMedia.request_completed.connect(round_media_resp) $HTTP_RoundNext.request_completed.connect(round_next_resp) $HTTP_StreamerVote.request_completed.connect(streamervote_resp) set_process(false) @@ -106,6 +109,23 @@ func round_info(): func round_info_resp(_result, response_code: int, _headers: PackedStringArray, body: PackedByteArray): print("Response Round Info: " + str(response_code) + "\n" + body.get_string_from_ascii()) +## round_media gets the named media from the current round. +func round_media(media: String, callback: Callable): + round_media_callback = callback + round_media_media = media + var error: Error = $HTTP_RoundMedia.request(host + "/round/media/" + media, ["Authorization: Q4E " + api_token], HTTPClient.METHOD_GET) + if error != OK: + print("Error requesting round media: %s" % error) + +func round_media_resp(result, response_code: int, _headers: PackedStringArray, body: PackedByteArray): + if response_code == HTTPClient.RESPONSE_OK: + round_media_callback.call(true, round_media_media, body) + else: + print("Failed to get round media: (%s) got %d expected %d: %s" % [result, response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()]) + round_media_callback.call(false, round_media_media, body) + round_media_callback = Callable() + round_media_media = "" + ## round_next advances the game to the round and returning information about the new active round. This is also ## required for the first round after a freshly created game. func round_next(callback: Callable): @@ -116,7 +136,7 @@ func round_next_resp(_result, response_code: int, _headers: PackedStringArray, b if response_code == HTTPClient.RESPONSE_OK: round_next_callback.call(true, json_parse(body)) else: - print("Failed to advance to next sound: got %d expected %d: %s" % [response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()]) + print("Failed to advance to next round: got %d expected %d: %s" % [response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()]) round_next_callback.call(false) round_next_callback = Callable() diff --git a/scripts/question.gd b/scripts/question.gd index 6c01a71..6b3c759 100644 --- a/scripts/question.gd +++ b/scripts/question.gd @@ -4,6 +4,7 @@ var quizOn: bool = false var voted: bool = false var countdown: float var round_data: Dictionary = {} +var mediaDict: Dictionary = {} func _ready(): countdown = scene_manager.round_duration @@ -74,17 +75,8 @@ func show_round_data(data: Dictionary): $Quiz/Question/Label.show() elif data.question.type == 1: $Quiz/Question/Label.hide() - var img: Image = Image.new() - var err: Error = img.load_png_from_buffer(data.question.text.to_utf8_buffer()) - if err != OK: - print("img error ", err) - print("img ", img) - - var imgtex: ImageTexture = ImageTexture.new() - imgtex.set_image(img) - print("imgtex ", imgtex) - $Quiz/Question/Image.texture = imgtex - $Quiz/Question/Image.show() + $Quiz/Question/Image.hide() + mediaDict[data.question.text] = $Quiz/Question/Image if data.answers[0].type == 0: $Quiz/Answers/A/Image.hide() @@ -92,20 +84,16 @@ func show_round_data(data: Dictionary): $Quiz/Answers/A/Label.show() elif data.answers[0].type == 1: $Quiz/Answers/A/Label.hide() - var img: Image = Image.new() - img.load_png_from_buffer(data.question.text.to_utf8_buffer()) - $Quiz/Answers/A/Quiz/Answers/A/Image.Texture = ImageTexture.create_from_image(img) - $Quiz/Answers/A/Image.show() + $Quiz/Answers/A/Image.hide() + mediaDict[data.answers[0].text] = $Quiz/Question/A/Image if data.answers[1].type == 0: $Quiz/Answers/B/Image.hide() $Quiz/Answers/B/Label.text = data.answers[1].text $Quiz/Answers/B/Label.show() elif data.answers[1].type == 1: $Quiz/Answers/B/Label.hide() - var img: Image = Image.new() - img.load_png_from_buffer(data.question.text.to_utf8_buffer()) - $Quiz/Answers/B/Image.Texture = ImageTexture.create_from_image(img) - $Quiz/Answers/B/Image.show() + $Quiz/Answers/B/Image.hide() + mediaDict[data.answers[1].text] = $Quiz/Answers/B/Image if len(data.answers) >= 3: if data.answers[2].type == 0: @@ -114,10 +102,8 @@ func show_round_data(data: Dictionary): $Quiz/Answers/C/Label.show() elif data.answers[2].type == 1: $Quiz/Answers/C/Label.hide() - var img: Image = Image.new() - img.load_png_from_buffer(data.question.text.to_utf8_buffer()) - $Quiz/Answers/C/Image.Texture = ImageTexture.create_from_image(img) - $Quiz/Answers/C/Image.show() + $Quiz/Answers/C/Image.hide() + mediaDict[data.answers[2].text] = $Quiz/Answers/C/Image $Quiz/Answers/C.show() else: $Quiz/Answers/C.hide() @@ -128,13 +114,14 @@ func show_round_data(data: Dictionary): $Quiz/Answers/D/Label.show() elif data.answers[3].type == 1: $Quiz/Answers/D/Label.hide() - var img: Image = Image.new() - img.load_png_from_buffer(data.question.text.to_utf8_buffer()) - $Quiz/Answers/D/Image.Texture = ImageTexture.create_from_image(img) - $Quiz/Answers/D/Image.show() + $Quiz/Answers/D/Image.hide() + mediaDict[data.answers[3].text] = $Quiz/Answers/D/Image $Quiz/Answers/D.show() else: $Quiz/Answers/D.hide() + + if len(mediaDict) > 0: + api.round_media(data.question.text, on_round_media_response) $Quiz/Answers/VoteIcon/Icon.self_modulate = Color.WHITE $Quiz/Answers/VoteIcon.show() @@ -166,3 +153,13 @@ func on_server_api_got_ws_message(msg: Dictionary): on_round_end(msg) _: print("Got unkown ws message type: '%s': %s" % [msg.type, msg]) + +func on_round_media_response(success: bool, media: String, data: PackedByteArray): + if success: + img.load_png_from_buffer(data) + mediaDict[media].show() + + mediaDict.erase(media) + return + var next_media = mediaDict.keys()[0] + api.round_media(next_media, on_round_media_response)