feat(achievements): add RTMIScoreRow model

This commit is contained in:
2025-10-28 19:37:38 +01:00
parent dcd1082e67
commit 3e1bf6a8af
4 changed files with 29 additions and 9 deletions

View File

@@ -0,0 +1,10 @@
class_name RTMIScoreRow
var time: int
var date: String
func _init(document: Dictionary) -> void:
time = document.get("time")
date = document.get("$createdAt")

View File

@@ -0,0 +1 @@
uid://8c4xr4s71geu

View File

@@ -15,7 +15,7 @@ func _init(scene) -> void:
scene.add_child(http_requests[FASTEST])
func get_scores(collection: String) -> Variant:
func get_scores(collection: String) -> Array[RTMIScoreRow]:
var http_request = http_requests[collection]
var error = http_request.request(
"https://app.fosil.eu/v1/databases/68fff91900295af283bf/collections/%s/documents" % collection,
@@ -28,9 +28,18 @@ func get_scores(collection: String) -> Variant:
return _http_request_completed.callv(response)
func _http_request_completed(result: int, http_status: int, _headers: PackedStringArray, body: PackedByteArray) -> Variant:
func _http_request_completed(result: int, http_status: int, _headers: PackedStringArray, body: PackedByteArray) -> Array[RTMIScoreRow]:
if result != HTTPRequest.RESULT_SUCCESS or http_status < 200 or http_status >= 300:
push_error("HTTP request returned an error: result=%s http_status=%s" % [result, http_status])
return null
return []
return JSON.parse_string(body.get_string_from_utf8())
var response = JSON.parse_string(body.get_string_from_utf8())
if not response or not response.get("documents"):
push_error("Error parsing HTTP body.")
return []
var scores: Array[RTMIScoreRow] = []
for document in response.get("documents"):
scores.append(RTMIScoreRow.new(document))
return scores

View File

@@ -13,12 +13,12 @@ func refresh() -> void:
_get_scores()
func _fill_scores(scores):
func _fill_scores(scores: Array[RTMIScoreRow]):
_clear_scores()
for score in scores:
# ToDo create and add rich label
var label = Label.new()
label.text = "Time: {time} createAt: {$createdAt}".format(score)
label.text = "Time: {time} createAt: {date}".format(score)
%Scores.add_child(label)
@@ -32,6 +32,6 @@ func _get_scores() -> void:
if gymkhana.score_manager == null:
return
var scores = await gymkhana.score_manager.get_scores(collection)
if (scores != null):
_fill_scores(scores.documents)
var scores := await gymkhana.score_manager.get_scores(collection)
if not scores.is_empty():
_fill_scores(scores)