Strong typing and prevent infinite loop

This commit is contained in:
Adrien Ufferte
2025-10-09 15:01:07 +02:00
parent 2c511b9100
commit 0a302945a3
3 changed files with 16 additions and 11 deletions
+6 -5
View File
@@ -10,7 +10,7 @@ enum Status{
} }
@export var version: String = ProjectSettings.get_setting("application/config/version") @export var version: String = ProjectSettings.get_setting("application/config/version")
@export var unlocks: Dictionary = {}: @export var unlocks: Dictionary[int, Dictionary] = {}:
set(value): set(value):
unlocks = ensure_data_integrity(value) unlocks = ensure_data_integrity(value)
@export var last_modified: String @export var last_modified: String
@@ -44,13 +44,14 @@ func init_unlocks() -> void:
unlocks[1]["look_and_learn"] = Status.Unlocked unlocks[1]["look_and_learn"] = Status.Unlocked
func ensure_data_integrity(data: Dictionary) -> Dictionary: func ensure_data_integrity(data: Dictionary[int, Dictionary]) -> Dictionary:
var is_init: bool = data.is_empty() var is_init: bool = data.is_empty()
var result: Dictionary = data.duplicate(true) var result: Dictionary[int, Dictionary] = data.duplicate(true)
var number_of_lessons: int = Database.get_lessons_count() var number_of_lessons: int = Database.get_lessons_count()
# Check too much keys # Check too much keys
while result.size() > number_of_lessons: for key: int in result.keys():
result.erase(result.size()) if key > number_of_lessons:
result.erase(key)
# Check missing keys # Check missing keys
var min_key: int = 1 var min_key: int = 1
var max_key: int = number_of_lessons var max_key: int = number_of_lessons
+1 -1
View File
@@ -572,7 +572,7 @@ func save_student_progression_for_code(device: int, code: int, progression: Stud
Log.error("UserDataManager: save_student_progression_for_code(device = %s, code = %s): error %s" % [str(device), str(code), error_string(error)]) Log.error("UserDataManager: save_student_progression_for_code(device = %s, code = %s): error %s" % [str(device), str(code), error_string(error)])
func set_student_progression_data(student_code: int, version: String, new_data: Dictionary, updated_at: String) -> void: func set_student_progression_data(student_code: int, version: String, new_data: Dictionary[int, Dictionary], updated_at: String) -> void:
var current_data: StudentProgression = get_student_progression_for_code(0, student_code) var current_data: StudentProgression = get_student_progression_for_code(0, student_code)
if current_data == null: if current_data == null:
current_data = StudentProgression.new() current_data = StudentProgression.new()
@@ -399,13 +399,17 @@ func _apply_server_response(response_body: Dictionary) -> void:
if response_student_data.has("progression") and (response_student_data.progression as Dictionary).has("version") and (response_student_data.progression as Dictionary).has("unlocked") and (response_student_data.progression as Dictionary).has("updated_at"): if response_student_data.has("progression") and (response_student_data.progression as Dictionary).has("version") and (response_student_data.progression as Dictionary).has("unlocked") and (response_student_data.progression as Dictionary).has("updated_at"):
# Cleaning data because of JSON parsing changing types int / float / string # Cleaning data because of JSON parsing changing types int / float / string
var received_unlock_data: Dictionary = response_student_data.progression.unlocked as Dictionary var received_unlock_data: Dictionary = response_student_data.progression.unlocked as Dictionary
var new_unlock_data: Dictionary = {} var new_unlock_data: Dictionary[int, Dictionary] = {}
for key_lesson: Variant in received_unlock_data.keys(): for key_lesson: Variant in received_unlock_data.keys():
new_unlock_data[int(key_lesson as int)] = {"games": [], "look_and_learn": received_unlock_data[key_lesson]["look_and_learn"] as int} var key_lesson_int: int = int(str(key_lesson)) if str(key_lesson).is_valid_int() else -1
if key_lesson_int == -1:
Log.error("UserDatabaseSynchronizer: Received invalid key for lesson: %s" % str(key_lesson))
continue
new_unlock_data[key_lesson_int] = {"games": [], "look_and_learn": received_unlock_data[key_lesson]["look_and_learn"] as int}
for game_result: Variant in received_unlock_data[key_lesson]["games"]: for game_result: Variant in received_unlock_data[key_lesson]["games"]:
(new_unlock_data[key_lesson as int]["games"] as Array).push_back(game_result as int) (new_unlock_data[key_lesson_int]["games"] as Array).push_back(game_result as int)
(new_unlock_data[int(key_lesson as int)] as Dictionary).merge({"last_duration": PackedInt32Array(received_unlock_data[key_lesson]["last_duration"] as Array)}) new_unlock_data[key_lesson_int].merge({"last_duration": PackedInt32Array(received_unlock_data[key_lesson]["last_duration"] as Array)})
(new_unlock_data[int(key_lesson as int)] as Dictionary).merge({"total_duration": PackedInt32Array(received_unlock_data[key_lesson]["total_duration"] as Array)}) new_unlock_data[key_lesson_int].merge({"total_duration": PackedInt32Array(received_unlock_data[key_lesson]["total_duration"] as Array)})
UserDataManager.set_student_progression_data(int(response_student_code), response_student_data.progression.version as String, new_unlock_data, response_student_data.progression.updated_at as String) UserDataManager.set_student_progression_data(int(response_student_code), response_student_data.progression.version as String, new_unlock_data, response_student_data.progression.updated_at as String)
if response_student_data.has("remediation_gp") and (response_student_data.remediation_gp as Dictionary).has("score_remediation") and (response_student_data.remediation_gp as Dictionary).has("updated_at"): if response_student_data.has("remediation_gp") and (response_student_data.remediation_gp as Dictionary).has("score_remediation") and (response_student_data.remediation_gp as Dictionary).has("updated_at"):
# TODO ADD SECURITY # TODO ADD SECURITY