mirror of
https://github.com/zyllian/classics.git
synced 2025-01-18 03:32:41 -08:00
write server config on change
This commit is contained in:
parent
5b7b947804
commit
ab4f81b836
3 changed files with 39 additions and 12 deletions
|
@ -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:#?}");
|
||||
|
||||
|
|
|
@ -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<i8>,
|
||||
/// 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<RwLock<ServerData>>) {
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
.get(player_username)
|
||||
{
|
||||
if *current >= player_perms {
|
||||
msg!("&cThis player outranks or is the same rank as you"
|
||||
.to_string());
|
||||
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()
|
||||
|
|
Loading…
Add table
Reference in a new issue