From 7328ab1349162e5b1d6e016eb9ae7fffd709b87c Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 3 Oct 2023 00:08:26 +0200 Subject: [PATCH] Scoreboard Part 1 --- src/gui/menu/scores.gd | 35 +++++++++++++++++++-------- src/gui/menu/scores.tscn | 47 ++++++++++++++++++++----------------- src/model/apirequest.gd | 2 -- src/model/requestfactory.gd | 10 ++++++-- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/gui/menu/scores.gd b/src/gui/menu/scores.gd index 1449bd7..2830ed0 100644 --- a/src/gui/menu/scores.gd +++ b/src/gui/menu/scores.gd @@ -1,16 +1,16 @@ extends Control @export var level_select: Control -@export var first_selection: Control @export var level_first_selection: Control @export var leaderboard: Control +@export var scrollboard_vbox: Control +@export var scrollboard_item: Control # Called when the node enters the scene tree for the first time. func _ready(): - first_selection.grab_focus() - - + level_first_selection.grab_focus() + scrollboard_item.hide() func _on_quit_button_pressed(): get_tree().quit() @@ -19,12 +19,27 @@ func _on_back_button_pressed(): level_select.hide() -func _on_level_button_pressed(level:int): - load_level("res://src/game/levels/level_%s.tscn" % level) +func _on_level_button_pressed(level: int): + load_level(level) -func load_level(path: String): +func load_level(level: int): leaderboard.show() - load_leaderboard_content() + load_leaderboard_content(level) -func load_leaderboard_content(level: String): - +func load_leaderboard_content(level: int): + var factory = RequestFactory.new($HTTPRequest, Globals.config.HighscoreServerAddr) + factory.resolve_list_request(func (result, response_code, h, body): + if response_code == 200: + var json = JSON.parse_string(body.get_string_from_utf8()) + for k in json: + var node = scrollboard_item.duplicate() + var children = node.get_children() + children[0].text = k + 1 + children[1].text = json[k].player + children[2].text = str(json[k].score) + scrollboard_vbox.add_child(node) + + else: + return + # TODO: Show dialog + , "level" + str(level)) diff --git a/src/gui/menu/scores.tscn b/src/gui/menu/scores.tscn index 1d84b0e..272f915 100644 --- a/src/gui/menu/scores.tscn +++ b/src/gui/menu/scores.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=8 format=3 uid="uid://daki40cyvwaoy"] -[ext_resource type="Script" path="res://src/gui/menu/main.gd" id="1_1gfw2"] +[ext_resource type="Script" path="res://src/gui/menu/scores.gd" id="1_im4lk"] [ext_resource type="AudioStream" uid="uid://dh5jxxwim7ra8" path="res://assets/sounds/music/death_by_waveshaper.wav" id="2_t8ixe"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_2u7je"] @@ -54,7 +54,7 @@ Button/styles/normal = SubResource("StyleBoxFlat_uo2yv") Button/styles/pressed = SubResource("StyleBoxFlat_sjhib") Panel/styles/panel = SubResource("StyleBoxFlat_5it3g") -[node name="Menu" type="Control" node_paths=PackedStringArray("level_select", "level_first_selection")] +[node name="ScoreList" type="Control" node_paths=PackedStringArray("level_select", "level_first_selection", "leaderboard", "scrollboard_vbox", "scrollboard_item")] layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 @@ -64,9 +64,12 @@ offset_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 theme = SubResource("Theme_67fiq") -script = ExtResource("1_1gfw2") +script = ExtResource("1_im4lk") level_select = NodePath("LevelSelect") level_first_selection = NodePath("LevelSelect/VBoxContainer/VBoxContainer2/Row1/1") +leaderboard = NodePath("Leaderboard") +scrollboard_vbox = NodePath("Leaderboard/ScrollContainer/VBoxContainer") +scrollboard_item = NodePath("Leaderboard/ScrollContainer/VBoxContainer/HBoxContainer") [node name="LevelSelect" type="Panel" parent="."] layout_mode = 1 @@ -223,7 +226,7 @@ anchors_preset = 0 offset_right = 40.0 offset_bottom = 40.0 -[node name="LevelSelect2" type="Panel" parent="."] +[node name="Leaderboard" type="Panel" parent="."] visible = false layout_mode = 1 anchors_preset = -1 @@ -238,34 +241,34 @@ offset_bottom = 219.0 grow_horizontal = 2 grow_vertical = 2 -[node name="ScrollContainer" type="ScrollContainer" parent="LevelSelect2"] +[node name="ScrollContainer" type="ScrollContainer" parent="Leaderboard"] layout_mode = 2 offset_left = 50.0 offset_top = 100.0 offset_right = 450.0 offset_bottom = 400.0 -[node name="VBoxContainer" type="VBoxContainer" parent="LevelSelect2/ScrollContainer"] +[node name="VBoxContainer" type="VBoxContainer" parent="Leaderboard/ScrollContainer"] layout_mode = 2 -[node name="HBoxContainer" type="HBoxContainer" parent="LevelSelect2/ScrollContainer/VBoxContainer"] +[node name="HBoxContainer" type="HBoxContainer" parent="Leaderboard/ScrollContainer/VBoxContainer"] layout_mode = 2 -[node name="Label" type="Label" parent="LevelSelect2/ScrollContainer/VBoxContainer/HBoxContainer"] +[node name="Label" type="Label" parent="Leaderboard/ScrollContainer/VBoxContainer/HBoxContainer"] custom_minimum_size = Vector2(50, 2.08165e-12) layout_mode = 2 text = "1" -[node name="Label2" type="Label" parent="LevelSelect2/ScrollContainer/VBoxContainer/HBoxContainer"] +[node name="Label2" type="Label" parent="Leaderboard/ScrollContainer/VBoxContainer/HBoxContainer"] custom_minimum_size = Vector2(170, 2.08165e-12) layout_mode = 2 text = "Username" -[node name="Label3" type="Label" parent="LevelSelect2/ScrollContainer/VBoxContainer/HBoxContainer"] +[node name="Label3" type="Label" parent="Leaderboard/ScrollContainer/VBoxContainer/HBoxContainer"] layout_mode = 2 -text = "Username" +text = "12345678" -[node name="Label" type="Label" parent="LevelSelect2"] +[node name="Label" type="Label" parent="Leaderboard"] layout_mode = 1 anchors_preset = 5 anchor_left = 0.5 @@ -278,7 +281,7 @@ grow_horizontal = 2 text = "LEADERBOARD" horizontal_alignment = 1 -[node name="BackButton" type="Button" parent="LevelSelect2"] +[node name="BackButton" type="Button" parent="Leaderboard"] custom_minimum_size = Vector2(120, 40) layout_mode = 1 anchors_preset = 7 @@ -297,7 +300,7 @@ size_flags_vertical = 8 focus_neighbor_bottom = NodePath("../StartButton") text = "BACK" -[node name="BackButton" type="Button" parent="LevelSelect2/BackButton"] +[node name="BackButton" type="Button" parent="Leaderboard/BackButton"] custom_minimum_size = Vector2(120, 40) layout_mode = 1 anchors_preset = 7 @@ -316,7 +319,7 @@ focus_neighbor_bottom = NodePath("../StartButton") text = "NEXT " -[node name="BackButton" type="Button" parent="LevelSelect2/BackButton/BackButton"] +[node name="BackButton" type="Button" parent="Leaderboard/BackButton/BackButton"] custom_minimum_size = Vector2(120, 40) layout_mode = 1 anchors_preset = 7 @@ -336,7 +339,7 @@ focus_neighbor_bottom = NodePath("../StartButton") text = "PREVIOUS " -[node name="Label2" type="Label" parent="LevelSelect2"] +[node name="Label2" type="Label" parent="Leaderboard"] layout_mode = 0 offset_left = 50.0 offset_top = 66.0 @@ -344,7 +347,7 @@ offset_right = 107.0 offset_bottom = 92.0 text = "Rank" -[node name="Label3" type="Label" parent="LevelSelect2"] +[node name="Label3" type="Label" parent="Leaderboard"] layout_mode = 0 offset_left = 106.0 offset_top = 66.0 @@ -353,7 +356,7 @@ offset_bottom = 92.0 text = "Name " -[node name="Label4" type="Label" parent="LevelSelect2"] +[node name="Label4" type="Label" parent="Leaderboard"] layout_mode = 0 offset_left = 277.0 offset_top = 66.0 @@ -362,6 +365,8 @@ offset_bottom = 92.0 text = "Score " +[node name="HTTPRequest" type="HTTPRequest" parent="."] + [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/1" to="." method="_on_level_button_pressed" binds= [1]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/2" to="." method="_on_level_button_pressed" binds= [2]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row1/3" to="." method="_on_level_button_pressed" binds= [3]] @@ -375,6 +380,6 @@ text = "Score [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row3/11" to="." method="_on_level_button_pressed" binds= [11]] [connection signal="pressed" from="LevelSelect/VBoxContainer/VBoxContainer2/Row3/12" to="." method="_on_level_button_pressed" binds= [12]] [connection signal="pressed" from="LevelSelect/BackButton" to="." method="_on_back_button_pressed"] -[connection signal="pressed" from="LevelSelect2/BackButton" to="." method="_on_back_button_pressed"] -[connection signal="pressed" from="LevelSelect2/BackButton/BackButton" to="." method="_on_back_button_pressed"] -[connection signal="pressed" from="LevelSelect2/BackButton/BackButton/BackButton" to="." method="_on_back_button_pressed"] +[connection signal="pressed" from="Leaderboard/BackButton" to="." method="_on_back_button_pressed"] +[connection signal="pressed" from="Leaderboard/BackButton/BackButton" to="." method="_on_back_button_pressed"] +[connection signal="pressed" from="Leaderboard/BackButton/BackButton/BackButton" to="." method="_on_back_button_pressed"] diff --git a/src/model/apirequest.gd b/src/model/apirequest.gd index 1258d28..4d1fc69 100644 --- a/src/model/apirequest.gd +++ b/src/model/apirequest.gd @@ -8,7 +8,6 @@ var method: HTTPClient.Method var callback: Callable func _init(u: String, h: PackedStringArray, m: HTTPClient.Method, b: String = ""): - super() self.url = u self.headers = h self.method = m @@ -21,4 +20,3 @@ func resolve(f: Callable) -> Error: return error return OK - diff --git a/src/model/requestfactory.gd b/src/model/requestfactory.gd index 0fdb912..8d08d77 100644 --- a/src/model/requestfactory.gd +++ b/src/model/requestfactory.gd @@ -2,13 +2,15 @@ class_name RequestFactory const headers = [ "Content-Type: application/json", - "Authentication: Bearer " + Config.Secrets.ApiToken + "Authorization: Bearer " + Config.Secrets.ApiToken ] const api_base_path = "/api/v2" var base_uri: String +var node: HTTPRequest -func _init(base_uri: String): +func _init(node: HTTPRequest, base_uri: String): self.base_uri = base_uri + api_base_path + self.node = node func new_submit_request(score: Score) -> ApiRequest: score.ValKey = score.valKey() @@ -20,3 +22,7 @@ func new_list_request(key: String, offset: int = 0, limit: int = -1) -> ApiReque return ApiRequest.new( self.base_uri + "/score/list/" + Config.ProjectName.to_lower() + "?key=" + key + "&offset=" + str(offset) + "&limit=" + str(limit), self.headers, HTTPClient.METHOD_GET) + +func resolve_list_request(f: Callable, key: String, offset: int = 0, limit: int = -1): + self.node.request_completed.connect(f) + self.node.request(self.base_uri + "/score/list/" + Config.ProjectName.to_lower() + "?key=" + key + "&offset=" + str(offset) + "&limit=" + str(limit), headers, HTTPClient.METHOD_GET)