diff --git a/game/src/backend/gamestate.rs b/game/src/backend/gamestate.rs index 60ea773..addbb79 100644 --- a/game/src/backend/gamestate.rs +++ b/game/src/backend/gamestate.rs @@ -254,6 +254,21 @@ impl GameState { _ => {} } } + /// Deletes all files in the directory saves, returns Ok if saves directory does not exist + pub(crate) fn delete_saves() -> RLResult { + info!("deleting saves"); + let existing_files = fs::read_dir("./saves"); + if existing_files.is_err() { + return Ok(()); + } + for entry in existing_files? { + let file = entry?; + if file.metadata()?.is_file() { + fs::remove_file(file.path())?; + } + } + Ok(()) + } } impl Screen for GameState { @@ -326,4 +341,8 @@ mod test { GameState::default().save(true).unwrap(); let _gamestate_loaded = GameState::load(true).unwrap(); } + #[test] + fn test_delete_saves() { + GameState::delete_saves().unwrap(); + } } diff --git a/game/src/main_menu/mainmenu.rs b/game/src/main_menu/mainmenu.rs index b668b31..296d37b 100644 --- a/game/src/main_menu/mainmenu.rs +++ b/game/src/main_menu/mainmenu.rs @@ -77,8 +77,7 @@ impl Screen for MainMenu { match msg { Exit => std::process::exit(0), NewGame => { - fs::remove_file("./saves/autosave.yaml"); - fs::remove_file("./saves/milestone.yaml"); + GameState::delete_saves()?; self.screen_sender .send(StackCommand::Push(Box::new(GameState::new(ctx)?)))?; }