mirror of
https://github.com/zyllian/classics.git
synced 2025-01-18 11:47:14 -08:00
autosaves, resolves #2
This commit is contained in:
parent
bcb77ca194
commit
8f73d8e788
2 changed files with 11 additions and 1 deletions
|
@ -116,6 +116,7 @@ impl Server {
|
||||||
/// function to tick the server
|
/// function to tick the server
|
||||||
async fn handle_ticks(data: Arc<RwLock<ServerData>>) {
|
async fn handle_ticks(data: Arc<RwLock<ServerData>>) {
|
||||||
let mut current_tick = 0;
|
let mut current_tick = 0;
|
||||||
|
let mut last_auto_save = std::time::Instant::now();
|
||||||
loop {
|
loop {
|
||||||
{
|
{
|
||||||
let mut data = data.write().await;
|
let mut data = data.write().await;
|
||||||
|
@ -138,9 +139,15 @@ async fn handle_ticks(data: Arc<RwLock<ServerData>>) {
|
||||||
for player in &mut data.players {
|
for player in &mut data.players {
|
||||||
player.packets_to_send.push(packet.clone());
|
player.packets_to_send.push(packet.clone());
|
||||||
}
|
}
|
||||||
// TODO: save level before exiting
|
|
||||||
break;
|
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);
|
current_tick = current_tick.wrapping_add(1);
|
||||||
|
|
|
@ -24,6 +24,8 @@ pub struct ServerConfig {
|
||||||
pub spawn: Option<ConfigCoordinates>,
|
pub spawn: Option<ConfigCoordinates>,
|
||||||
/// the method to generate the server's level with
|
/// the method to generate the server's level with
|
||||||
pub generation: LevelGeneration,
|
pub generation: LevelGeneration,
|
||||||
|
/// the server should auto save the world every X minutes, 0 to disable
|
||||||
|
pub auto_save_minutes: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OptionalServerConfig {
|
impl OptionalServerConfig {
|
||||||
|
@ -47,6 +49,7 @@ impl Default for ServerConfig {
|
||||||
},
|
},
|
||||||
spawn: None,
|
spawn: None,
|
||||||
generation: LevelGeneration::Flat(crate::level::generation::FlatPreset::StoneAndGrass),
|
generation: LevelGeneration::Flat(crate::level::generation::FlatPreset::StoneAndGrass),
|
||||||
|
auto_save_minutes: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue