diff --git a/src/main.rs b/src/main.rs index c7adf6c..a37349f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,10 +26,6 @@ async fn main() -> std::io::Result<()> { } else { 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:#?}"); diff --git a/src/server.rs b/src/server.rs index bf5235d..90043ca 100644 --- a/src/server.rs +++ b/src/server.rs @@ -12,6 +12,7 @@ use crate::{ }, player::Player, util::neighbors_minus_up, + CONFIG_FILE, }; use self::config::ServerConfig; @@ -38,6 +39,8 @@ pub struct ServerData { pub free_player_ids: Vec, /// the server's config pub config: ServerConfig, + /// whether the server config needs to be resaved or not + pub config_needs_saving: bool, } impl Server { @@ -66,6 +69,7 @@ impl Server { players: Default::default(), free_player_ids: Vec::new(), config, + config_needs_saving: true, })), listener, }) @@ -94,7 +98,21 @@ impl Server { async fn handle_ticks(data: Arc>) { let mut current_tick = 0; 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); tokio::time::sleep(TICK_DURATION).await; } diff --git a/src/server/network.rs b/src/server/network.rs index 765a627..92ff546 100644 --- a/src/server/network.rs +++ b/src/server/network.rs @@ -393,18 +393,31 @@ async fn handle_stream_inner( serde_json::to_string(&permissions) .expect("should never fail"); - let current = data + if let Some(current) = data .config .player_perms - .entry(player_username.to_string()) - .or_default(); - if *current >= player_perms { - msg!("&cThis player outranks or is the same rank as you" + .get(player_username) + { + if *current >= player_perms { + msg!("&cThis player outranks or is the same rank as you" .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 .players .iter_mut()