diff --git a/src/server.rs b/src/server.rs index f90c269..b62a4ee 100644 --- a/src/server.rs +++ b/src/server.rs @@ -116,6 +116,7 @@ impl Server { /// function to tick the server async fn handle_ticks(data: Arc>) { let mut current_tick = 0; + let mut last_auto_save = std::time::Instant::now(); loop { { let mut data = data.write().await; @@ -138,9 +139,15 @@ async fn handle_ticks(data: Arc>) { for player in &mut data.players { player.packets_to_send.push(packet.clone()); } - // TODO: save level before exiting break; } + + if data.config.auto_save_minutes != 0 + && last_auto_save.elapsed().as_secs() / 60 >= data.config.auto_save_minutes + { + data.level.save(LEVEL_PATH).await; + last_auto_save = std::time::Instant::now(); + } } current_tick = current_tick.wrapping_add(1); diff --git a/src/server/config.rs b/src/server/config.rs index 401be62..0749a0e 100644 --- a/src/server/config.rs +++ b/src/server/config.rs @@ -24,6 +24,8 @@ pub struct ServerConfig { pub spawn: Option, /// the method to generate the server's level with pub generation: LevelGeneration, + /// the server should auto save the world every X minutes, 0 to disable + pub auto_save_minutes: u64, } impl OptionalServerConfig { @@ -47,6 +49,7 @@ impl Default for ServerConfig { }, spawn: None, generation: LevelGeneration::Flat(crate::level::generation::FlatPreset::StoneAndGrass), + auto_save_minutes: 1, } } }