mirror of
https://github.com/deunix-educ/aventuriers-des-mots.git
synced 2026-10-13 02:11:58 +02:00
Add profile and save persistence, wired into the three autoloads
scripts/profiles/: SaveIO handles real file I/O in user://saves (one file per profile_id, atomic write via temp-file-then-rename, backup copy before each overwrite, corrupted-main falls back to backup, schema-invalid content is rejected like corruption). SaveMigration and SaveFactory are pure; SaveMigration refuses a schema_version it doesn't recognize (older or newer) rather than guessing. ProgressUpdates is pure and wires ReviewScheduler/DifficultyManager onto a save's skills dict, including per-session overdue-review marking. The three autoloads (SaveManager, ProfileManager, ProgressManager) stay thin: in-memory current-profile state and orchestration only, no logic of their own, so the actual behavior lives in the tested pure/file-IO modules above rather than in untestable glue. 26 new headless tests, including the failure modes called out in the launch prompt: a truncated .tmp file never corrupts the readable save, a corrupted main file falls back to its backup, two profiles never mix (down through the real autoload layer, not just SaveIO), and an unknown future schema_version is rejected outright. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,35 @@
|
||||
extends Node
|
||||
## Creation, selection et suppression des profils enfants.
|
||||
## Logique detaillee a partir de l'etape "Profils et sauvegardes".
|
||||
## Creation, selection et suppression des profils enfants. Chaque profil a un
|
||||
## profile_id distinct ; SaveManager garantit qu'un profil ne lit ni n'ecrit
|
||||
## jamais le fichier d'un autre (voir "Points durs" du prompt de lancement).
|
||||
|
||||
var current_profile_id: String = ""
|
||||
|
||||
|
||||
func create_profile(display_name: String, school_level: String) -> String:
|
||||
var profile_id := _generate_profile_id()
|
||||
SaveManager.load_or_create(profile_id, display_name, school_level)
|
||||
return profile_id
|
||||
|
||||
|
||||
## Charge le profil dans ProgressManager et le marque comme profil courant.
|
||||
## Ne fait rien si le profil n'existe pas.
|
||||
func select_profile(profile_id: String) -> bool:
|
||||
if not ProgressManager.load_profile(profile_id):
|
||||
return false
|
||||
current_profile_id = profile_id
|
||||
return true
|
||||
|
||||
|
||||
func delete_profile(profile_id: String) -> void:
|
||||
SaveManager.delete(profile_id)
|
||||
if current_profile_id == profile_id:
|
||||
current_profile_id = ""
|
||||
|
||||
|
||||
func list_profiles() -> Array[String]:
|
||||
return SaveManager.list_profile_ids()
|
||||
|
||||
|
||||
func _generate_profile_id() -> String:
|
||||
return "profile_%d_%d" % [Time.get_unix_time_from_system(), randi() % 10000]
|
||||
|
||||
@@ -1,3 +1,58 @@
|
||||
extends Node
|
||||
## Competences, missions debloquees et planification des revisions par profil.
|
||||
## Logique detaillee a partir de l'etape "Logique pure" (voir docs/specs/decisions.md, point 5).
|
||||
## Competences, missions debloquees et planification des revisions du profil
|
||||
## selectionne. Delegue les calculs a ProgressUpdates (scripts/profiles/), qui
|
||||
## s'appuie sur ReviewScheduler et DifficultyManager (scripts/pedagogy/).
|
||||
## L'ecriture sur disque reste groupee : save_now() est appele en fin de
|
||||
## mission, pas a chaque tentative (docs/specs/decisions.md, point 7).
|
||||
|
||||
var _current_save: Dictionary = {}
|
||||
var _current_profile_id: String = ""
|
||||
|
||||
|
||||
## Charge la sauvegarde existante d'un profil et demarre une nouvelle seance
|
||||
## (voir ProgressUpdates.start_session). Faux si le profil n'a pas de
|
||||
## sauvegarde lisible.
|
||||
func load_profile(profile_id: String) -> bool:
|
||||
var data: Variant = SaveManager.load(profile_id)
|
||||
if data == null:
|
||||
return false
|
||||
_current_profile_id = profile_id
|
||||
_current_save = ProgressUpdates.start_session(data)
|
||||
return true
|
||||
|
||||
|
||||
func record_attempt(
|
||||
skill_id: String, success: bool, hint_used: bool, exercise_type: String
|
||||
) -> void:
|
||||
var attempt := {
|
||||
"skill": skill_id,
|
||||
"success": success,
|
||||
"hint_used": hint_used,
|
||||
"exercise_type": exercise_type,
|
||||
}
|
||||
_current_save = ProgressUpdates.record_attempt(_current_save, attempt)
|
||||
|
||||
|
||||
func unlock_region(region_id: String) -> void:
|
||||
_current_save = ProgressUpdates.unlock_region(_current_save, region_id)
|
||||
|
||||
|
||||
func next_difficulty_for_skill(skill_id: String, current_difficulty: int) -> int:
|
||||
return ProgressUpdates.next_difficulty_for_skill(_current_save, skill_id, current_difficulty)
|
||||
|
||||
|
||||
func get_skill_state(skill_id: String) -> Dictionary:
|
||||
var skills: Dictionary = _current_save.get("skills", {})
|
||||
return skills.get(skill_id, ReviewScheduler.initial_state())
|
||||
|
||||
|
||||
func current_save() -> Dictionary:
|
||||
return _current_save
|
||||
|
||||
|
||||
## A appeler en fin de mission (pas apres chaque tentative) pour persister la
|
||||
## progression du profil courant.
|
||||
func save_now() -> bool:
|
||||
if _current_profile_id.is_empty():
|
||||
return false
|
||||
return SaveManager.save(_current_profile_id, _current_save)
|
||||
|
||||
@@ -1,4 +1,39 @@
|
||||
extends Node
|
||||
## Serialisation des sauvegardes par profil dans user:// (ecriture atomique,
|
||||
## copie de secours, migration).
|
||||
## Logique detaillee a partir de l'etape "Profils et sauvegardes".
|
||||
## copie de secours, migration). Delegue le travail reel a SaveIO,
|
||||
## SaveFactory et SaveMigration (scripts/profiles/), qui restent testables
|
||||
## sans arbre de scenes.
|
||||
|
||||
var _save_schema: Dictionary = {}
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_save_schema = DataLoader.load_schema("save")
|
||||
|
||||
|
||||
## Cree une nouvelle sauvegarde si le profil n'en a pas encore.
|
||||
func load_or_create(profile_id: String, display_name: String, school_level: String) -> Dictionary:
|
||||
var existing: Variant = SaveIO.read(profile_id, _save_schema)
|
||||
if existing != null:
|
||||
return existing
|
||||
var created := SaveFactory.new_save(profile_id, display_name, school_level)
|
||||
SaveIO.write(profile_id, created)
|
||||
return created
|
||||
|
||||
|
||||
## Lit la sauvegarde d'un profil existant. Null si elle est introuvable (et sa
|
||||
## copie de secours aussi).
|
||||
func load(profile_id: String) -> Variant:
|
||||
return SaveIO.read(profile_id, _save_schema)
|
||||
|
||||
|
||||
func save(profile_id: String, data: Dictionary) -> bool:
|
||||
return SaveIO.write(profile_id, data)
|
||||
|
||||
|
||||
func delete(profile_id: String) -> void:
|
||||
SaveIO.delete(profile_id)
|
||||
|
||||
|
||||
func list_profile_ids() -> Array[String]:
|
||||
return SaveIO.list_profile_ids()
|
||||
|
||||
Reference in New Issue
Block a user