39 lines
949 B
Rust
39 lines
949 B
Rust
//! Player progress saved to `./.gameplay/state.yaml`.
|
|
|
|
use anyhow::Result;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
|
|
use crate::levels::Difficulty;
|
|
|
|
#[derive(Default, Serialize, Deserialize)]
|
|
pub struct Progress {
|
|
pub tier: Option<Difficulty>,
|
|
pub completed: Vec<u8>,
|
|
pub current_level: u8,
|
|
pub current_seed: u64,
|
|
pub attempts: u32,
|
|
}
|
|
|
|
pub fn save_path() -> PathBuf {
|
|
PathBuf::from(".gameplay/state.yaml")
|
|
}
|
|
|
|
pub fn load() -> Result<Progress> {
|
|
let path = save_path();
|
|
if !path.exists() {
|
|
return Ok(Progress::default());
|
|
}
|
|
Ok(serde_yaml::from_str(&std::fs::read_to_string(&path)?)?)
|
|
}
|
|
|
|
pub fn save(progress: &Progress) -> Result<()> {
|
|
let path = save_path();
|
|
// First-run safeguard: `.gameplay/` won't exist yet.
|
|
if let Some(dir) = path.parent() {
|
|
std::fs::create_dir_all(dir)?;
|
|
}
|
|
std::fs::write(&path, serde_yaml::to_string(progress)?)?;
|
|
Ok(())
|
|
}
|