2023-01-07 11:03:21 +00:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
signal play_pressed()
|
|
|
|
|
2023-01-09 16:43:16 +00:00
|
|
|
var scores_downloaded := false
|
|
|
|
var scores := [] as Array[Array]
|
|
|
|
|
2023-01-09 16:59:08 +00:00
|
|
|
var score_scene := preload("res://menu/score.tscn")
|
|
|
|
|
2023-01-09 16:43:16 +00:00
|
|
|
var is_uploading := false
|
|
|
|
var should_open_scores_after_upload := false
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
if Singletons.should_upload:
|
|
|
|
# do request...
|
|
|
|
_upload_score()
|
|
|
|
self.should_open_scores_after_upload = true
|
|
|
|
_fetch_scores()
|
|
|
|
|
|
|
|
|
|
|
|
func _upload_score():
|
|
|
|
if not (Singletons.should_upload and len(Singletons.name) > 0 and Singletons.time):
|
|
|
|
return
|
|
|
|
|
|
|
|
var url = "https://arcade.steffo.eu/score/?board=ld52&player=%s" % Singletons.name
|
|
|
|
|
|
|
|
var httpreq = HTTPRequest.new()
|
|
|
|
add_child(httpreq)
|
|
|
|
httpreq.connect("request_completed", func(result, response_code, headers, body):
|
|
|
|
var json = JSON.parse_string(body.get_string_from_utf8())
|
|
|
|
# { score, rank }
|
|
|
|
self.is_uploading = false
|
|
|
|
_fetch_scores(true)
|
|
|
|
httpreq.queue_free()
|
|
|
|
)
|
|
|
|
self.is_uploading = true
|
|
|
|
httpreq.request(url, [
|
|
|
|
"Content-Type: application/json"
|
|
|
|
], true, HTTPClient.METHOD_PUT, str(Singletons.time))
|
|
|
|
|
|
|
|
|
|
|
|
func _fetch_scores(open_after: bool = false):
|
|
|
|
const url = "https://arcade.steffo.eu/board/?board=ld52&offset=0&size=10"
|
|
|
|
var httpreq = HTTPRequest.new()
|
|
|
|
add_child(httpreq)
|
|
|
|
httpreq.connect("request_completed", func(result, response_code, headers, body):
|
|
|
|
var json = JSON.parse_string(body.get_string_from_utf8())
|
|
|
|
self.scores = (json as Array).map(func(element): return [element.name, element.score])
|
2023-01-09 16:59:08 +00:00
|
|
|
|
|
|
|
for child in %ScoresVBox.get_children():
|
|
|
|
child.queue_free()
|
|
|
|
|
|
|
|
for score in self.scores:
|
|
|
|
var score_sc = score_scene.instantiate()
|
|
|
|
score_sc.get_node("HBoxContainer/Name").text = score[0]
|
|
|
|
score_sc.get_node("HBoxContainer/Score").text = "%d" % score[1]
|
|
|
|
%ScoresVBox.add_child(score_sc)
|
|
|
|
|
2023-01-09 16:43:16 +00:00
|
|
|
self.scores_downloaded = true
|
|
|
|
%ScoresButton.disabled = false
|
|
|
|
print(self.scores)
|
2023-01-09 16:59:08 +00:00
|
|
|
if open_after:
|
|
|
|
open_scores()
|
2023-01-09 16:43:16 +00:00
|
|
|
httpreq.queue_free()
|
|
|
|
)
|
|
|
|
httpreq.request(url)
|
|
|
|
|
2023-01-07 11:03:21 +00:00
|
|
|
|
|
|
|
func play():
|
|
|
|
print("Play button was pressed.")
|
|
|
|
emit_signal("play_pressed")
|
2023-01-09 14:15:59 +00:00
|
|
|
|
|
|
|
|
2023-01-09 16:43:16 +00:00
|
|
|
func open_scores():
|
|
|
|
$ScoreboardContainer.visible = true
|
|
|
|
|
|
|
|
func close_scores():
|
|
|
|
$ScoreboardContainer.visible = false
|
|
|
|
|
|
|
|
|
2023-01-09 14:15:59 +00:00
|
|
|
func _on_text_edit_text_changed():
|
|
|
|
Singletons.username = $Content/Inputs/NameInput.text
|
2023-01-09 16:43:16 +00:00
|
|
|
%PlayButton.disabled = false
|