Add models, globals, persistent config, move secrets

This commit is contained in:
Manuel 2023-10-02 20:42:44 +02:00
parent 8749e11fd0
commit 01908fbcb6
Signed by: Manuel
GPG key ID: 4085037435E1F07A
14 changed files with 129 additions and 20 deletions

2
.gitignore vendored
View file

@ -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

View file

@ -18,6 +18,7 @@ config/icon="res://icon.svg"
[autoload]
GlobalWorldEnvironment="*res://src/world_environment.tscn"
Globals="*res://src/globals.gd"
[dotnet]

View file

@ -1,10 +0,0 @@
extends Node
class_name Config
var secrets = preload("secrets.gd")
var HighscoreServerAddr: String
var ApiToken: String
var ValKey: String

3
src/globals.gd Normal file
View file

@ -0,0 +1,3 @@
extends Node
var config = Config.new()

View file

@ -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)

View file

@ -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

View file

@ -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):

View file

@ -9,5 +9,6 @@ func _ready():
first_selection.grab_focus()
func _on_back_button_pressed():
Globals.config.save()
last_menu.show()
queue_free()

View file

@ -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))

24
src/model/apirequest.gd Normal file
View file

@ -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

57
src/model/config.gd Normal file
View file

@ -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

View file

@ -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)

10
src/model/score.gd Normal file
View file

@ -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()

View file

@ -1,7 +0,0 @@
extends Node
class_name Score
var Name: String
var Score: int
var Game: String