Skip to content

Commit

Permalink
added media endpoint for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Nov 1, 2024
1 parent d4511a4 commit 1148589
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
2 changes: 2 additions & 0 deletions scenes/api.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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="."]
22 changes: 21 additions & 1 deletion scripts/api.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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()

Expand Down
57 changes: 30 additions & 27 deletions scripts/question.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,38 +75,25 @@ 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()
$Quiz/Answers/A/Label.text = data.answers[0].text
$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:
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -166,3 +153,19 @@ 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:
var img: Image = Image.new()
img.load_png_from_buffer(data)
mediaDict[media].texture = ImageTexture.create_from_image(img)
mediaDict[media].show()
else:
print("Faild to get media %s data: %s" % [media, data.get_string_from_ascii()])

mediaDict.erase(media)
if len(mediaDict) == 0:
return

var next_media = mediaDict.keys()[0]
api.round_media(next_media, on_round_media_response)

0 comments on commit 1148589

Please sign in to comment.