Skip to content

Commit 8203d34

Browse files
committed
added media endpoint for images
1 parent d4511a4 commit 8203d34

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

scenes/api.tscn

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ script = ExtResource("1_l1n5m")
1919

2020
[node name="HTTP_RoundInfo" type="HTTPRequest" parent="."]
2121

22+
[node name="HTTP_RoundMedia" type="HTTPRequest" parent="."]
23+
2224
[node name="HTTP_RoundNext" type="HTTPRequest" parent="."]
2325

2426
[node name="HTTP_StreamerVote" type="HTTPRequest" parent="."]

scripts/api.gd

+21-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var category_callback: Callable
88
var game_start_callback: Callable
99
var got_ws_message: Callable
1010
var login_callback: Callable
11+
var round_media_callback: Callable
12+
var round_media_media: String
1113
var round_next_callback: Callable
1214
var streamervote_callback: Callable
1315

@@ -26,6 +28,7 @@ func _ready():
2628
$HTTP_Login.request_completed.connect(login_resp)
2729
$HTTP_Logout.request_completed.connect(logout_resp)
2830
$HTTP_RoundInfo.request_completed.connect(round_info_resp)
31+
$HTTP_RoundMedia.request_completed.connect(round_media_resp)
2932
$HTTP_RoundNext.request_completed.connect(round_next_resp)
3033
$HTTP_StreamerVote.request_completed.connect(streamervote_resp)
3134
set_process(false)
@@ -106,6 +109,23 @@ func round_info():
106109
func round_info_resp(_result, response_code: int, _headers: PackedStringArray, body: PackedByteArray):
107110
print("Response Round Info: " + str(response_code) + "\n" + body.get_string_from_ascii())
108111

112+
## round_media gets the named media from the current round.
113+
func round_media(media: String, callback: Callable):
114+
round_media_callback = callback
115+
round_media_media = media
116+
var error: Error = $HTTP_RoundMedia.request(host + "/round/media/" + media, ["Authorization: Q4E " + api_token], HTTPClient.METHOD_GET)
117+
if error != OK:
118+
print("Error requesting round media: %s" % error)
119+
120+
func round_media_resp(result, response_code: int, _headers: PackedStringArray, body: PackedByteArray):
121+
if response_code == HTTPClient.RESPONSE_OK:
122+
round_media_callback.call(true, round_media_media, body)
123+
else:
124+
print("Failed to get round media: (%s) got %d expected %d: %s" % [result, response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()])
125+
round_media_callback.call(false, round_media_media, body)
126+
round_media_callback = Callable()
127+
round_media_media = ""
128+
109129
## round_next advances the game to the round and returning information about the new active round. This is also
110130
## required for the first round after a freshly created game.
111131
func round_next(callback: Callable):
@@ -116,7 +136,7 @@ func round_next_resp(_result, response_code: int, _headers: PackedStringArray, b
116136
if response_code == HTTPClient.RESPONSE_OK:
117137
round_next_callback.call(true, json_parse(body))
118138
else:
119-
print("Failed to advance to next sound: got %d expected %d: %s" % [response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()])
139+
print("Failed to advance to next round: got %d expected %d: %s" % [response_code, HTTPClient.RESPONSE_OK, body.get_string_from_ascii()])
120140
round_next_callback.call(false)
121141
round_next_callback = Callable()
122142

scripts/question.gd

+24-27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var quizOn: bool = false
44
var voted: bool = false
55
var countdown: float
66
var round_data: Dictionary = {}
7+
var mediaDict: Dictionary = {}
78

89
func _ready():
910
countdown = scene_manager.round_duration
@@ -74,38 +75,25 @@ func show_round_data(data: Dictionary):
7475
$Quiz/Question/Label.show()
7576
elif data.question.type == 1:
7677
$Quiz/Question/Label.hide()
77-
var img: Image = Image.new()
78-
var err: Error = img.load_png_from_buffer(data.question.text.to_utf8_buffer())
79-
if err != OK:
80-
print("img error ", err)
81-
print("img ", img)
82-
83-
var imgtex: ImageTexture = ImageTexture.new()
84-
imgtex.set_image(img)
85-
print("imgtex ", imgtex)
86-
$Quiz/Question/Image.texture = imgtex
87-
$Quiz/Question/Image.show()
78+
$Quiz/Question/Image.hide()
79+
mediaDict[data.question.text] = $Quiz/Question/Image
8880

8981
if data.answers[0].type == 0:
9082
$Quiz/Answers/A/Image.hide()
9183
$Quiz/Answers/A/Label.text = data.answers[0].text
9284
$Quiz/Answers/A/Label.show()
9385
elif data.answers[0].type == 1:
9486
$Quiz/Answers/A/Label.hide()
95-
var img: Image = Image.new()
96-
img.load_png_from_buffer(data.question.text.to_utf8_buffer())
97-
$Quiz/Answers/A/Quiz/Answers/A/Image.Texture = ImageTexture.create_from_image(img)
98-
$Quiz/Answers/A/Image.show()
87+
$Quiz/Answers/A/Image.hide()
88+
mediaDict[data.answers[0].text] = $Quiz/Question/A/Image
9989
if data.answers[1].type == 0:
10090
$Quiz/Answers/B/Image.hide()
10191
$Quiz/Answers/B/Label.text = data.answers[1].text
10292
$Quiz/Answers/B/Label.show()
10393
elif data.answers[1].type == 1:
10494
$Quiz/Answers/B/Label.hide()
105-
var img: Image = Image.new()
106-
img.load_png_from_buffer(data.question.text.to_utf8_buffer())
107-
$Quiz/Answers/B/Image.Texture = ImageTexture.create_from_image(img)
108-
$Quiz/Answers/B/Image.show()
95+
$Quiz/Answers/B/Image.hide()
96+
mediaDict[data.answers[1].text] = $Quiz/Answers/B/Image
10997

11098
if len(data.answers) >= 3:
11199
if data.answers[2].type == 0:
@@ -114,10 +102,8 @@ func show_round_data(data: Dictionary):
114102
$Quiz/Answers/C/Label.show()
115103
elif data.answers[2].type == 1:
116104
$Quiz/Answers/C/Label.hide()
117-
var img: Image = Image.new()
118-
img.load_png_from_buffer(data.question.text.to_utf8_buffer())
119-
$Quiz/Answers/C/Image.Texture = ImageTexture.create_from_image(img)
120-
$Quiz/Answers/C/Image.show()
105+
$Quiz/Answers/C/Image.hide()
106+
mediaDict[data.answers[2].text] = $Quiz/Answers/C/Image
121107
$Quiz/Answers/C.show()
122108
else:
123109
$Quiz/Answers/C.hide()
@@ -128,13 +114,14 @@ func show_round_data(data: Dictionary):
128114
$Quiz/Answers/D/Label.show()
129115
elif data.answers[3].type == 1:
130116
$Quiz/Answers/D/Label.hide()
131-
var img: Image = Image.new()
132-
img.load_png_from_buffer(data.question.text.to_utf8_buffer())
133-
$Quiz/Answers/D/Image.Texture = ImageTexture.create_from_image(img)
134-
$Quiz/Answers/D/Image.show()
117+
$Quiz/Answers/D/Image.hide()
118+
mediaDict[data.answers[3].text] = $Quiz/Answers/D/Image
135119
$Quiz/Answers/D.show()
136120
else:
137121
$Quiz/Answers/D.hide()
122+
123+
if len(mediaDict) > 0:
124+
api.round_media(data.question.text, on_round_media_response)
138125

139126
$Quiz/Answers/VoteIcon/Icon.self_modulate = Color.WHITE
140127
$Quiz/Answers/VoteIcon.show()
@@ -166,3 +153,13 @@ func on_server_api_got_ws_message(msg: Dictionary):
166153
on_round_end(msg)
167154
_:
168155
print("Got unkown ws message type: '%s': %s" % [msg.type, msg])
156+
157+
func on_round_media_response(success: bool, media: String, data: PackedByteArray):
158+
if success:
159+
img.load_png_from_buffer(data)
160+
mediaDict[media].show()
161+
162+
mediaDict.erase(media)
163+
return
164+
var next_media = mediaDict.keys()[0]
165+
api.round_media(next_media, on_round_media_response)

0 commit comments

Comments
 (0)