Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for images #4

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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="."]
70 changes: 70 additions & 0 deletions scenes/question.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3

[node name="Image" type="TextureRect" parent="Quiz/Question"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -103.0
offset_top = -103.0
offset_right = 97.0
offset_bottom = 97.0
grow_horizontal = 2
grow_vertical = 2

[node name="Answers" type="Control" parent="Quiz"]
layout_mode = 1
anchors_preset = 8
Expand Down Expand Up @@ -108,6 +122,20 @@ horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3

[node name="Image" type="TextureRect" parent="Quiz/Answers/A"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -103.0
offset_top = -103.0
offset_right = 97.0
offset_bottom = 97.0
grow_horizontal = 2
grow_vertical = 2

[node name="B" type="ColorRect" parent="Quiz/Answers"]
layout_mode = 1
anchors_preset = 8
Expand Down Expand Up @@ -146,6 +174,20 @@ horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3

[node name="Image" type="TextureRect" parent="Quiz/Answers/B"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -103.0
offset_top = -103.0
offset_right = 97.0
offset_bottom = 97.0
grow_horizontal = 2
grow_vertical = 2

[node name="C" type="ColorRect" parent="Quiz/Answers"]
layout_mode = 1
anchors_preset = 8
Expand Down Expand Up @@ -184,6 +226,20 @@ horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3

[node name="Image" type="TextureRect" parent="Quiz/Answers/C"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -103.0
offset_top = -103.0
offset_right = 97.0
offset_bottom = 97.0
grow_horizontal = 2
grow_vertical = 2

[node name="D" type="ColorRect" parent="Quiz/Answers"]
layout_mode = 1
anchors_preset = 8
Expand Down Expand Up @@ -225,6 +281,20 @@ horizontal_alignment = 1
vertical_alignment = 1
autowrap_mode = 3

[node name="Image" type="TextureRect" parent="Quiz/Answers/D"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -103.0
offset_top = -103.0
offset_right = 97.0
offset_bottom = 97.0
grow_horizontal = 2
grow_vertical = 2

[node name="StartCountdown" type="Label" parent="."]
anchors_preset = 8
anchor_left = 0.5
Expand Down
26 changes: 23 additions & 3 deletions 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,14 +28,15 @@ 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)
print("api loaded")

func _process(_delta: float):
var msg: Dictionary = read_from_ws()
if got_ws_message&&len(msg) > 0:
if got_ws_message && len(msg) > 0:
got_ws_message.call(msg)

## category gets the available categories from the server.
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 Expand Up @@ -174,7 +194,7 @@ func read_from_ws() -> Dictionary:
elif state == WebSocketPeer.STATE_CLOSED:
var code = ws.get_close_code()
var reason = ws.get_close_reason()
print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != - 1])
print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1])
set_process(false)

return {}
Expand Down
10 changes: 5 additions & 5 deletions scripts/game_end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ func show_round_data(data: Dictionary):
print(data)
$RoundCounter.text = "Runde %d/%d (%s)" % [data.current_round, data.max_round, data.category.title]

$Quiz/Question/Label.text = data.question
$Quiz/Answers/A/Label.text = data.answers[0]
$Quiz/Answers/B/Label.text = data.answers[1]
$Quiz/Question/Label.text = data.question.text
$Quiz/Answers/A/Label.text = data.answers[0].text
$Quiz/Answers/B/Label.text = data.answers[1].text
if len(data.answers) >= 3:
$Quiz/Answers/C/Label.text = data.answers[2]
$Quiz/Answers/C/Label.text = data.answers[2].text
$Quiz/Answers/C.show()
else:
$Quiz/Answers/C.hide()
if len(data.answers) >= 4:
$Quiz/Answers/D/Label.text = data.answers[3]
$Quiz/Answers/D/Label.text = data.answers[3].text
$Quiz/Answers/D.show()
else:
$Quiz/Answers/D.hide()
Expand Down
78 changes: 68 additions & 10 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 All @@ -21,10 +22,10 @@ func _process(delta: float):
check_vote()

func check_vote():
if Input.is_key_pressed(KEY_A)||Input.is_key_pressed(KEY_1)||Input.is_key_pressed(KEY_KP_1): api.streamervote("1", on_streamervote_response)
elif Input.is_key_pressed(KEY_B)||Input.is_key_pressed(KEY_2)||Input.is_key_pressed(KEY_KP_2): api.streamervote("2", on_streamervote_response)
elif Input.is_key_pressed(KEY_C)||Input.is_key_pressed(KEY_3)||Input.is_key_pressed(KEY_KP_3): api.streamervote("3", on_streamervote_response)
elif Input.is_key_pressed(KEY_D)||Input.is_key_pressed(KEY_4)||Input.is_key_pressed(KEY_KP_4): api.streamervote("4", on_streamervote_response)
if Input.is_key_pressed(KEY_A) || Input.is_key_pressed(KEY_1) || Input.is_key_pressed(KEY_KP_1): api.streamervote("1", on_streamervote_response)
elif Input.is_key_pressed(KEY_B) || Input.is_key_pressed(KEY_2) || Input.is_key_pressed(KEY_KP_2): api.streamervote("2", on_streamervote_response)
elif Input.is_key_pressed(KEY_C) || Input.is_key_pressed(KEY_3) || Input.is_key_pressed(KEY_KP_3): api.streamervote("3", on_streamervote_response)
elif Input.is_key_pressed(KEY_D) || Input.is_key_pressed(KEY_4) || Input.is_key_pressed(KEY_KP_4): api.streamervote("4", on_streamervote_response)
else: return
voted = true
$Quiz/Answers/VoteIcon/Icon.self_modulate = Color.ORANGE
Expand Down Expand Up @@ -55,7 +56,7 @@ func start_next_round():
api.round_next(on_round_next_response)
quizOn = true

func on_round_next_response(success: bool, data: Dictionary={}):
func on_round_next_response(success: bool, data: Dictionary = {}):
if !success:
print("Faild to get next data!")
return
Expand All @@ -68,19 +69,59 @@ func show_round_data(data: Dictionary):
$RoundCounter.text = "Runde %d/%d (%s)" % [data.current_round, data.max_round, data.category.title]
$RoundCounter.show()

$Quiz/Question/Label.text = data.question
$Quiz/Answers/A/Label.text = data.answers[0]
$Quiz/Answers/B/Label.text = data.answers[1]
if data.question.type == 0:
$Quiz/Question/Image.hide()
$Quiz/Question/Label.text = data.question.text
$Quiz/Question/Label.show()
elif data.question.type == 1:
$Quiz/Question/Label.hide()
$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()
$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()
$Quiz/Answers/B/Image.hide()
mediaDict[data.answers[1].text] = $Quiz/Answers/B/Image

if len(data.answers) >= 3:
$Quiz/Answers/C/Label.text = data.answers[2]
if data.answers[2].type == 0:
$Quiz/Answers/C/Image.hide()
$Quiz/Answers/C/Label.text = data.answers[2].text
$Quiz/Answers/C/Label.show()
elif data.answers[2].type == 1:
$Quiz/Answers/C/Label.hide()
$Quiz/Answers/C/Image.hide()
mediaDict[data.answers[2].text] = $Quiz/Answers/C/Image
$Quiz/Answers/C.show()
else:
$Quiz/Answers/C.hide()
if len(data.answers) >= 4:
$Quiz/Answers/D/Label.text = data.answers[3]
if data.answers[3].type == 0:
$Quiz/Answers/D/Image.hide()
$Quiz/Answers/D/Label.text = data.answers[3].text
$Quiz/Answers/D/Label.show()
elif data.answers[3].type == 1:
$Quiz/Answers/D/Label.hide()
$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 @@ -112,3 +153,20 @@ 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:
print("media ", media)
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)
10 changes: 5 additions & 5 deletions scripts/question_end.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ func show_round_data(data: Dictionary):
print(data)
$RoundCounter.text = "Runde %d/%d (%s)" % [data.current_round, data.max_round, data.category.title]

$Quiz/Question/Label.text = data.question
$Quiz/Answers/A/Label.text = data.answers[0]
$Quiz/Answers/B/Label.text = data.answers[1]
$Quiz/Question/Label.text = data.question.text
$Quiz/Answers/A/Label.text = data.answers[0].text
$Quiz/Answers/B/Label.text = data.answers[1].text
if len(data.answers) >= 3:
$Quiz/Answers/C/Label.text = data.answers[2]
$Quiz/Answers/C/Label.text = data.answers[2].text
$Quiz/Answers/C.show()
else:
$Quiz/Answers/C.hide()
if len(data.answers) >= 4:
$Quiz/Answers/D/Label.text = data.answers[3]
$Quiz/Answers/D/Label.text = data.answers[3].text
$Quiz/Answers/D.show()
else:
$Quiz/Answers/D.hide()
Expand Down
Loading