diff --git a/.gitignore b/.gitignore index 0dc6c7c..cd93522 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ addons/ # Don't push our secrets -src/secrets.gd +src/model/secrets.gd # Imported translations (automatically generated from CSV files) *.translation diff --git a/project.godot b/project.godot index 8e04b6b..e7a0df0 100644 --- a/project.godot +++ b/project.godot @@ -18,6 +18,7 @@ config/icon="res://icon.svg" [autoload] GlobalWorldEnvironment="*res://src/world_environment.tscn" +Globals="*res://src/globals.gd" [dotnet] diff --git a/src/config.gd b/src/config.gd deleted file mode 100644 index 39e7cc0..0000000 --- a/src/config.gd +++ /dev/null @@ -1,10 +0,0 @@ -extends Node - -class_name Config - -var secrets = preload("secrets.gd") - -var HighscoreServerAddr: String -var ApiToken: String -var ValKey: String - diff --git a/src/globals.gd b/src/globals.gd new file mode 100644 index 0000000..e2565fc --- /dev/null +++ b/src/globals.gd @@ -0,0 +1,3 @@ +extends Node + +var config = Config.new() diff --git a/src/gui/hud/hud_controller.gd b/src/gui/hud/hud_controller.gd index ed678ec..25cca3d 100644 --- a/src/gui/hud/hud_controller.gd +++ b/src/gui/hud/hud_controller.gd @@ -6,7 +6,7 @@ extends Control @export var distance: Label func _on_player_speed_changed(speed_float:float): - var speed_string = "%.f" % speed_float + var speed_string = "%.f" % (speed_float / 10) # print_debug("changing speed display to ", speed_string) speed.set_text(speed_string) diff --git a/src/gui/menu/brightness.gd b/src/gui/menu/brightness.gd index 5fb2c6b..57d8b06 100644 --- a/src/gui/menu/brightness.gd +++ b/src/gui/menu/brightness.gd @@ -6,3 +6,4 @@ func _ready(): func _on_value_changed(value): print_debug("setting brightness to ", value) GlobalWorldEnvironment.environment.adjustment_brightness = value + Globals.config.Brightness = value diff --git a/src/gui/menu/main.gd b/src/gui/menu/main.gd index e3d3d09..8baa201 100644 --- a/src/gui/menu/main.gd +++ b/src/gui/menu/main.gd @@ -9,7 +9,12 @@ extends Control # Called when the node enters the scene tree for the first time. func _ready(): first_selection.grab_focus() - + var err = Globals.config.load() + if err: + print("Config does not yet exist, skipping load") + else: + print("Config loaded successfully") + # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): diff --git a/src/gui/menu/options.gd b/src/gui/menu/options.gd index 933688a..71459ae 100644 --- a/src/gui/menu/options.gd +++ b/src/gui/menu/options.gd @@ -9,5 +9,6 @@ func _ready(): first_selection.grab_focus() func _on_back_button_pressed(): + Globals.config.save() last_menu.show() queue_free() diff --git a/src/gui/menu/volume_slider.gd b/src/gui/menu/volume_slider.gd index ddf117c..a1cf002 100644 --- a/src/gui/menu/volume_slider.gd +++ b/src/gui/menu/volume_slider.gd @@ -12,3 +12,4 @@ func _ready() -> void: func _on_value_changed(value: float) -> void: AudioServer.set_bus_volume_db(bus_index, linear_to_db(value)) + #Globals.config.setVolume(bus_index, linear_to_db(value)) diff --git a/src/model/apirequest.gd b/src/model/apirequest.gd new file mode 100644 index 0000000..1258d28 --- /dev/null +++ b/src/model/apirequest.gd @@ -0,0 +1,24 @@ +class_name ApiRequest +extends HTTPRequest + +var url: String +var headers: PackedStringArray +var body: String +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 + self.body = b + +func resolve(f: Callable) -> Error: + self.request_completed.connect(f) + var error = super.request(self.url, self.headers, self.method, self.body) + if error != OK: + return error + + return OK + diff --git a/src/model/config.gd b/src/model/config.gd new file mode 100644 index 0000000..a3970a2 --- /dev/null +++ b/src/model/config.gd @@ -0,0 +1,57 @@ +class_name Config +extends Node + +const Secrets = preload("secrets.gd") +const Path = "config.cfg" +const ProjectName = "Distanz" + +var Username: String +var HighscoreServerAddr: String +var VolumeMain: int +var VolumeMusic: int +var VolumeSFX: int +var Brightness: float + +const _defHighscoreServerAddr = "https://grg.snrd.eu" +const _defVolumeMain = 100 +const _defVolumeMusic = 75 +const _defVolumeSFX = 100 +const _defBrightness = 1.0 + +func _init(): + self.HighscoreServerAddr = _defHighscoreServerAddr + self.VolumeMain = _defVolumeMain + self.VolumeMusic = _defVolumeMusic + self.VolumeSFX = _defVolumeSFX + self.Brightness = _defBrightness + +func save() -> Error: + var config = ConfigFile.new() + config.set_value("Settings", "Username", self.HighscoreServerAddr) + config.set_value("Settings", "HighscoreServerAddr", self.HighscoreServerAddr) + config.set_value("Settings", "VolumeMain", self.VolumeMain) + config.set_value("Settings", "VolumeMusic", self.VolumeMusic) + config.set_value("Settings", "VolumeSFX", self.VolumeSFX) + config.set_value("Settings", "Brightness", self.VolumeSFX) + + config.save("user://" + Path) + + return OK + +func exists() -> bool: + return FileAccess.file_exists("user://" + Path) + +func load() -> Error: + var config = ConfigFile.new() + var err = config.load("user://" + Path) + + if err: + return err + + self.HighscoreServerAddr = config.get_value("Settings", "HighscoreServerAddr", self._defHighscoreServerAddr) + self.VolumeMain = config.get_value("Settings", "VolumeMain", self._defVolumeMain) + self.VolumeMusic = config.get_value("Settings", "VolumeMusic", self._defVolumeMusic) + self.VolumeSFX = config.get_value("Settings", "VolumeSFX", self._defVolumeSFX) + self.VolumeSFX = config.get_value("Settings", "Brightness", self._defBrightness) + + return OK diff --git a/src/model/requestfactory.gd b/src/model/requestfactory.gd new file mode 100644 index 0000000..ea05cb9 --- /dev/null +++ b/src/model/requestfactory.gd @@ -0,0 +1,23 @@ +class_name RequestFactory +extends Object + +const headers = [ + "Content-Type: application/json", + "Authentication: Bearer " + Config.Secrets.ApiToken +] +const api_base_path = "/api/v2" +var base_uri: String + +func _init(base_uri: String): + self.base_uri = base_uri + api_base_path + +func new_submit_request(score: Score) -> ApiRequest: + 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: + 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) diff --git a/src/model/score.gd b/src/model/score.gd new file mode 100644 index 0000000..6924a5c --- /dev/null +++ b/src/model/score.gd @@ -0,0 +1,10 @@ +class_name Score + +var Name: String +var Scores: Dictionary + +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() diff --git a/src/score.gd b/src/score.gd deleted file mode 100644 index 995c82f..0000000 --- a/src/score.gd +++ /dev/null @@ -1,7 +0,0 @@ -extends Node - -class_name Score - -var Name: String -var Score: int -var Game: String