load individual levels from user/levels/

This commit is contained in:
Crispy 2024-12-24 23:44:55 +01:00
parent 74d142191c
commit bce482482a

View file

@ -322,32 +322,42 @@ impl Game {
fn get_levels() -> Vec<LevelListEntry> { fn get_levels() -> Vec<LevelListEntry> {
let mut chapters = Vec::<Chapter>::new(); let mut chapters = Vec::<Chapter>::new();
let mut add_level = |path| {
let l = read_to_string(path)
.ok()
.as_deref()
.and_then(|s| serde_json::from_str(s).ok());
if let Some(level) = l {
chapters.push(level);
}
};
for d in read_dir("levels").unwrap().flatten() { for d in read_dir("levels").unwrap().flatten() {
if d.path().is_dir() { if let Ok(text) = read_to_string(d.path()) {
for d in read_dir(d.path()).unwrap().flatten() { if let Ok(chapter) = serde_json::from_str(&text) {
add_level(d.path()); chapters.push(chapter);
} }
} else {
add_level(d.path());
} }
} }
chapters.sort_unstable_by_key(|c| c.title.clone()); chapters.sort_unstable_by_key(|c| c.title.clone());
let mut levels = Vec::new(); let mut level_list = Vec::new();
for c in chapters { for c in chapters {
levels.push(LevelListEntry::Chapter(c.title, c.levels.len())); level_list.push(LevelListEntry::Chapter(c.title, c.levels.len()));
levels.extend(c.levels.into_iter().map(LevelListEntry::Level)); level_list.extend(c.levels.into_iter().map(LevelListEntry::Level));
} }
levels
// user levels
let mut custom_levels = Vec::new();
let custom_level_dir = userdata_dir().join("levels");
if custom_level_dir.is_dir() {
for d in read_dir(custom_level_dir).unwrap().flatten() {
if let Ok(text) = read_to_string(d.path()) {
if let Ok(level) = serde_json::from_str(&text) {
custom_levels.push(level);
}
}
}
}
level_list.push(LevelListEntry::Chapter(
"Custom levels".into(),
custom_levels.len(),
));
for l in custom_levels {
level_list.push(LevelListEntry::Level(l));
}
level_list
} }
fn get_solutions() -> HashMap<String, Vec<Solution>> { fn get_solutions() -> HashMap<String, Vec<Solution>> {