write server config on change

This commit is contained in:
Zoey 2024-04-23 00:53:36 -07:00
parent 5b7b947804
commit ab4f81b836
No known key found for this signature in database
GPG key ID: 8611B896D1AAFAF2
3 changed files with 39 additions and 12 deletions

View file

@ -26,10 +26,6 @@ async fn main() -> std::io::Result<()> {
} else { } else {
ServerConfig::default() ServerConfig::default()
}; };
std::fs::write(
config_path,
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
)?;
println!("starting server with config: {config:#?}"); println!("starting server with config: {config:#?}");

View file

@ -12,6 +12,7 @@ use crate::{
}, },
player::Player, player::Player,
util::neighbors_minus_up, util::neighbors_minus_up,
CONFIG_FILE,
}; };
use self::config::ServerConfig; use self::config::ServerConfig;
@ -38,6 +39,8 @@ pub struct ServerData {
pub free_player_ids: Vec<i8>, pub free_player_ids: Vec<i8>,
/// the server's config /// the server's config
pub config: ServerConfig, pub config: ServerConfig,
/// whether the server config needs to be resaved or not
pub config_needs_saving: bool,
} }
impl Server { impl Server {
@ -66,6 +69,7 @@ impl Server {
players: Default::default(), players: Default::default(),
free_player_ids: Vec::new(), free_player_ids: Vec::new(),
config, config,
config_needs_saving: true,
})), })),
listener, listener,
}) })
@ -94,7 +98,21 @@ impl 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;
loop { loop {
tick(&mut *data.write().await, current_tick); {
let mut data = data.write().await;
tick(&mut data, current_tick);
if data.config_needs_saving {
std::fs::write(
CONFIG_FILE,
serde_json::to_string_pretty(&data.config)
.expect("failed to serialize default config"),
)
.expect("failed to save config file");
data.config_needs_saving = false;
}
}
current_tick = current_tick.wrapping_add(1); current_tick = current_tick.wrapping_add(1);
tokio::time::sleep(TICK_DURATION).await; tokio::time::sleep(TICK_DURATION).await;
} }

View file

@ -393,18 +393,31 @@ async fn handle_stream_inner(
serde_json::to_string(&permissions) serde_json::to_string(&permissions)
.expect("should never fail"); .expect("should never fail");
let current = data if let Some(current) = data
.config .config
.player_perms .player_perms
.entry(player_username.to_string()) .get(player_username)
.or_default(); {
if *current >= player_perms { if *current >= player_perms {
msg!("&cThis player outranks or is the same rank as you" msg!("&cThis player outranks or is the same rank as you"
.to_string()); .to_string());
continue; continue;
}
} }
*current = permissions; data.config_needs_saving = true;
if matches!(permissions, PlayerType::Normal)
{
data.config
.player_perms
.remove(player_username);
} else {
data.config.player_perms.insert(
player_username.to_string(),
permissions,
);
}
if let Some(p) = data if let Some(p) = data
.players .players
.iter_mut() .iter_mut()