1
Fork 0
mirror of https://github.com/Steffo99/pineapple-surf.git synced 2024-11-25 01:04:18 +00:00
pineapple-surf/menu/ScoreboardContainer.gd

77 lines
2.1 KiB
GDScript3
Raw Normal View History

extends Control
2023-01-09 20:01:41 +00:00
var scores_downloaded := false
var scores := [] as Array[Array]
2023-01-14 20:28:28 +00:00
var score_scene := preload("res://menu/ScoreBox.tscn")
2023-01-09 20:01:41 +00:00
var is_uploading := false
2023-01-14 20:28:28 +00:00
var prev_mouse_mode: Input.MouseMode = Input.MOUSE_MODE_VISIBLE
2023-01-09 20:01:41 +00:00
signal fetched_scores()
func upload_score(score: float):
var url = "https://arcade.steffo.eu/score/?board=test&player=%s" % Singletons.username
var httpreq = HTTPRequest.new()
add_child(httpreq)
2023-01-14 20:28:28 +00:00
httpreq.connect("request_completed", func(_result, _response_code, _headers, body):
2023-01-09 20:01:41 +00:00
var json = JSON.parse_string(body.get_string_from_utf8())
# { score, rank }
print(json)
self.is_uploading = false
httpreq.queue_free()
2023-01-14 20:28:28 +00:00
await get_tree().create_timer(1.75).timeout
fetch_scores(true)
2023-01-09 20:01:41 +00:00
)
self.is_uploading = true
httpreq.request(url, [
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Bearer hahaha-ronaldinho-soccer"
], true, HTTPClient.METHOD_PUT, str(score))
2023-01-14 20:28:28 +00:00
func fetch_scores(open_after: bool = false):
2023-01-09 20:01:41 +00:00
const url = "https://arcade.steffo.eu/board/?board=test&offset=0&size=10"
var httpreq = HTTPRequest.new()
add_child(httpreq)
2023-01-14 20:28:28 +00:00
httpreq.connect("request_completed", func(_result, _response_code, _headers, body):
2023-01-09 20:01:41 +00:00
var json = JSON.parse_string(body.get_string_from_utf8())
self.scores = (json as Array).map(func(element): return [element.name, element.score])
for child in $Panel/VBoxContainer/ScrollContainer/ScoresVBox.get_children():
child.queue_free()
for score in self.scores:
2023-01-14 20:28:28 +00:00
var score_sc: ScoreBox = score_scene.instantiate()
2023-01-09 20:01:41 +00:00
$Panel/VBoxContainer/ScrollContainer/ScoresVBox.add_child(score_sc)
2023-01-14 20:28:28 +00:00
score_sc.username = score[0]
score_sc.score = score[1]
2023-01-09 20:01:41 +00:00
self.scores_downloaded = true
emit_signal("fetched_scores")
if open_after:
show_scores()
httpreq.queue_free()
)
httpreq.request(url)
func show_scores():
2023-01-14 20:28:28 +00:00
print("Displaying scores...")
prev_mouse_mode = Input.mouse_mode as Input.MouseMode
2023-01-09 20:01:41 +00:00
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
visible = true
func hide_scores():
2023-01-14 20:28:28 +00:00
print("Hiding scores...")
2023-01-09 20:01:41 +00:00
Input.set_mouse_mode(prev_mouse_mode)
visible = false