mirror of https://github.com/zyllian/classics.git synced 2025-05-10 05:46:40 -07:00

save level on stop (loading soon)

This commit is contained in:
Zoey 2024-04-23 01:28:32 -07:00
parent d5530a1449
commit fae40178af
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
5 changed files with 40 additions and 3 deletions

View file

@ -1,5 +1,7 @@
use std::collections::BTreeSet;
use bincode::{Decode, Encode};
use crate::{packet::server::ServerPacket, util::neighbors};
use self::block::BLOCK_INFO;
@ -8,7 +10,7 @@ pub mod block;
pub mod generation;
/// a classic level
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Encode, Decode)]
pub struct Level {
/// the size of the level in the X direction
pub x_size: usize,
@ -91,7 +93,7 @@ impl Level {
}
/// struct describing a block update for the level to handle
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Encode, Decode)]
pub struct BlockUpdate {
/// the index of the block to be updated
pub index: usize,

View file

@ -94,12 +94,20 @@ impl Server {
});
}
});
handle_ticks(self.data).await;
handle_ticks(self.data.clone()).await;
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
// TODO: cancel pending tasks/send out "Server is stopping" messages *here* instead of elsewhere
// rn the message isn't guaranteed to actually go out........
let data = self.data.read().await;
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::best());
bincode::encode_into_std_write(&data.level, &mut encoder, bincode::config::standard())
.unwrap();
tokio::fs::write("level.clw", encoder.finish().unwrap())
.await
.unwrap();
Ok(())
}
}