diff --git a/src/gui/menu/score_submit.gd b/src/gui/menu/score_submit.gd index de55f9b..554423a 100644 --- a/src/gui/menu/score_submit.gd +++ b/src/gui/menu/score_submit.gd @@ -7,20 +7,26 @@ var highscore var level_nr func _ready(): - score_label.set_text("%.f" % highscore) + score_label.set_text(Globals.config.Username) -func _on_sumbit_button_pressed(): - pass #insert score sumbitting function here - #then proceed to highscore screen +func _on_submit_button_pressed(): + var factory = RequestFactory.new($HTTPRequest, Globals.config.HighscoreServerAddr) + var score = Score.new(name_field.text, {"level"+str(level_nr): highscore}) + factory.resolve_submit_request(func (result, response_code, h, body): + if response_code == 200: + var json = JSON.parse_string(body.get_string_from_utf8()) + print(json) + var scores = load("res://src/gui/menu/scores.tscn").instantiate() + scores.scores = json + scores.level_nr = level_nr + get_parent().add_child((scores)) + else: + return # TODO: Show Dialog + , score) func _on_retry_button_pressed(): get_tree().reload_current_scene() - -func _on_submit_button_pressed(): - pass # Replace with function body. - - func _on_next_button_pressed(): var next_level = level_nr + 1 if next_level > 12: diff --git a/src/gui/menu/score_submit.tscn b/src/gui/menu/score_submit.tscn index 7ecce8d..14a4a05 100644 --- a/src/gui/menu/score_submit.tscn +++ b/src/gui/menu/score_submit.tscn @@ -127,6 +127,8 @@ size_flags_vertical = 8 focus_neighbor_bottom = NodePath("../StartButton") text = "NEXT LEVEL" +[node name="HTTPRequest" type="HTTPRequest" parent="."] + [connection signal="pressed" from="Panel/RetryButton" to="." method="_on_retry_button_pressed"] [connection signal="pressed" from="Panel/SubmitButton" to="." method="_on_submit_button_pressed"] [connection signal="pressed" from="Panel/NextButton" to="." method="_on_next_button_pressed"] diff --git a/src/gui/menu/scores.gd b/src/gui/menu/scores.gd index 2830ed0..a2c803d 100644 --- a/src/gui/menu/scores.gd +++ b/src/gui/menu/scores.gd @@ -6,18 +6,22 @@ extends Control @export var scrollboard_vbox: Control @export var scrollboard_item: Control +var scores +var level_nr # Called when the node enters the scene tree for the first time. func _ready(): level_first_selection.grab_focus() scrollboard_item.hide() + if level_nr != null: + level_select.hide() + load_level(level_nr) func _on_quit_button_pressed(): get_tree().quit() func _on_back_button_pressed(): level_select.hide() - func _on_level_button_pressed(level: int): load_level(level) @@ -38,8 +42,6 @@ func load_leaderboard_content(level: int): children[1].text = json[k].player children[2].text = str(json[k].score) scrollboard_vbox.add_child(node) - else: - return - # TODO: Show dialog + return # TODO: Show dialog , "level" + str(level)) diff --git a/src/gui/menu/scores.tscn b/src/gui/menu/scores.tscn index 272f915..9d59511 100644 --- a/src/gui/menu/scores.tscn +++ b/src/gui/menu/scores.tscn @@ -298,7 +298,7 @@ grow_vertical = 0 size_flags_horizontal = 4 size_flags_vertical = 8 focus_neighbor_bottom = NodePath("../StartButton") -text = "BACK" +text = "PLAY LEVEL" [node name="BackButton" type="Button" parent="Leaderboard/BackButton"] custom_minimum_size = Vector2(120, 40) @@ -316,8 +316,7 @@ grow_vertical = 0 size_flags_horizontal = 4 size_flags_vertical = 8 focus_neighbor_bottom = NodePath("../StartButton") -text = "NEXT -" +text = "PLAY NEXT" [node name="BackButton" type="Button" parent="Leaderboard/BackButton/BackButton"] custom_minimum_size = Vector2(120, 40) @@ -336,8 +335,7 @@ grow_vertical = 0 size_flags_horizontal = 4 size_flags_vertical = 8 focus_neighbor_bottom = NodePath("../StartButton") -text = "PREVIOUS -" +text = "BACK" [node name="Label2" type="Label" parent="Leaderboard"] layout_mode = 0 diff --git a/src/model/requestfactory.gd b/src/model/requestfactory.gd index 8d08d77..9f347ef 100644 --- a/src/model/requestfactory.gd +++ b/src/model/requestfactory.gd @@ -13,9 +13,8 @@ func _init(node: HTTPRequest, base_uri: String): self.node = node func new_submit_request(score: Score) -> ApiRequest: - score.ValKey = score.valKey() + score.valkey = score.valKey() var body = JSON.stringify(score) - print(body) return ApiRequest.new(self.base_uri + "/score/submit/" + Config.ProjectName.to_lower(), self.headers, HTTPClient.METHOD_POST, body) func new_list_request(key: String, offset: int = 0, limit: int = -1) -> ApiRequest: @@ -23,6 +22,12 @@ func new_list_request(key: String, offset: int = 0, limit: int = -1) -> ApiReque self.base_uri + "/score/list/" + Config.ProjectName.to_lower() + "?key=" + key + "&offset=" + str(offset) + "&limit=" + str(limit), self.headers, HTTPClient.METHOD_GET) +func resolve_submit_request(f: Callable, score: Score): + score.valkey = score.valKey() + var body = JSON.stringify(score) + self.node.request_completed.connect(f) + self.node.request(self.base_uri + "/score/submit/" + Config.ProjectName.to_lower(), headers, HTTPClient.METHOD_GET, body) + 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) diff --git a/src/model/score.gd b/src/model/score.gd index 6924a5c..0373e8c 100644 --- a/src/model/score.gd +++ b/src/model/score.gd @@ -1,10 +1,15 @@ class_name Score -var Name: String -var Scores: Dictionary +var name: String +var scores: Dictionary +var valkey: String + +func _init(name: String, scores: Dictionary): + self.name = name + self.scores = scores func valKey() -> String: var scorestr = "" - for i in self.Scores: - scorestr += i + self.Scores[i] - return String(Config.ProjectName.to_lower() + self.Name + scorestr + Secrets.ValKey).sha256_text() + for i in self.scores: + scorestr += i + str(self.scores[i]) + return String(Config.ProjectName.to_lower() + self.name + scorestr + Secrets.ValKey).sha256_text()