autosaves, resolves #2

This commit is contained in:
Zoey 2024-04-23 09:41:22 -07:00
parent bcb77ca194
commit 8f73d8e788
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
2 changed files with 11 additions and 1 deletions

View file

@ -116,6 +116,7 @@ impl Server {
/// function to tick the server
async fn handle_ticks(data: Arc<RwLock<ServerData>>) {
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<RwLock<ServerData>>) {
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);

View file

@ -24,6 +24,8 @@ pub struct ServerConfig {
pub spawn: Option<ConfigCoordinates>,
/// 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,
}
}
}