feat(achievements): add RTMIScoreRow model
This commit is contained in:
@@ -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")
|
||||
@@ -0,0 +1 @@
|
||||
uid://8c4xr4s71geu
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user