Skip to content

Commit

Permalink
Merge pull request #92 from red-life-project/#69-saves-loeschen
Browse files Browse the repository at this point in the history
Delete Saves reworked
  • Loading branch information
Flippchen authored Nov 24, 2022
2 parents b5aa197 + 73e3b0e commit 9151406
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 19 additions & 0 deletions game/src/backend/gamestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}
3 changes: 1 addition & 2 deletions game/src/main_menu/mainmenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)))?;
}
Expand Down

0 comments on commit 9151406

Please sign in to comment.