From d9cd37056dec2d3b390ac6b9dbf7c01b13fb2332 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 25 Jun 2020 14:56:21 +0100 Subject: [PATCH] Added settings editing, set_motd command --- common/src/cmd.rs | 10 +++++++++- server/Cargo.toml | 2 +- server/src/cmd.rs | 36 ++++++++++++++++++++++++++---------- server/src/lib.rs | 32 +++++++++++++++++++++++--------- server/src/settings.rs | 11 +++++++++-- 5 files changed, 68 insertions(+), 23 deletions(-) diff --git a/common/src/cmd.rs b/common/src/cmd.rs index 97284b4237..c84fa074b3 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -48,6 +48,7 @@ pub enum ChatCommand { Players, RemoveLights, SetLevel, + SetMotd, Spawn, Sudo, Tell, @@ -80,6 +81,7 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[ ChatCommand::Players, ChatCommand::RemoveLights, ChatCommand::SetLevel, + ChatCommand::SetMotd, ChatCommand::Spawn, ChatCommand::Sudo, ChatCommand::Tell, @@ -226,7 +228,7 @@ impl ChatCommand { "Spawn entity with light", true, ), - ChatCommand::Motd => cmd(vec![Message], "Set the server description", true), + ChatCommand::Motd => cmd(vec![Message], "View the server description", false), ChatCommand::Object => cmd( vec![Enum("object", OBJECTS.clone(), Required)], "Spawn an object", @@ -243,6 +245,11 @@ impl ChatCommand { "Set player Level", true, ), + ChatCommand::SetMotd => cmd( + vec![Message], + "Set the server description", + true, + ), ChatCommand::Spawn => cmd( vec![ Enum("alignment", ALIGNMENTS.clone(), Required), @@ -303,6 +310,7 @@ impl ChatCommand { ChatCommand::Players => "players", ChatCommand::RemoveLights => "remove_lights", ChatCommand::SetLevel => "set_level", + ChatCommand::SetMotd => "set_motd", ChatCommand::Spawn => "spawn", ChatCommand::Sudo => "sudo", ChatCommand::Tell => "tell", diff --git a/server/Cargo.toml b/server/Cargo.toml index d5f624269e..0a33b95e71 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -14,7 +14,7 @@ world = { package = "veloren-world", path = "../world" } specs-idvs = { git = "https://gitlab.com/veloren/specs-idvs.git" } -tracing = { version = "0.1", default-features = true } +tracing = "0.1" specs = { version = "0.15.1", features = ["shred-derive"] } vek = "0.11.0" uvth = "3.1.1" diff --git a/server/src/cmd.rs b/server/src/cmd.rs index ced1a459ab..91a4fc1e35 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -2,7 +2,7 @@ //! To implement a new command, add an instance of `ChatCommand` to //! `CHAT_COMMANDS` and provide a handler function. -use crate::{Server, ServerSettings, StateExt}; +use crate::{Server, StateExt}; use chrono::{NaiveTime, Timelike}; use common::{ assets, @@ -83,6 +83,7 @@ fn get_handler(cmd: &ChatCommand) -> CommandHandler { ChatCommand::Players => handle_players, ChatCommand::RemoveLights => handle_remove_lights, ChatCommand::SetLevel => handle_set_level, + ChatCommand::SetMotd => handle_set_motd, ChatCommand::Spawn => handle_spawn, ChatCommand::Sudo => handle_sudo, ChatCommand::Tell => handle_tell, @@ -175,24 +176,39 @@ fn handle_motd( _target: EcsEntity, args: String, action: &ChatCommand, +) { + server.notify_client( + client, + ServerMsg::broadcast(format!("{}", server.settings().server_description)), + ); +} + +fn handle_set_motd( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + args: String, + action: &ChatCommand, ) { match scan_fmt!(&args, &action.arg_fmt(), String) { Ok(msg) => { server - .state_mut() - .ecs_mut() - .fetch_mut::() - .server_description = msg.clone(); - server.server_info.description = msg.clone(); + .settings_mut() + .edit(|s| s.server_description = msg.clone()); server.notify_client( client, ServerMsg::private(format!("Server description set to \"{}\"", msg)), ); }, - Err(_) => server.notify_client( - client, - ServerMsg::broadcast(format!("{}", server.server_info.description)), - ), + Err(_) => { + server + .settings_mut() + .edit(|s| s.server_description.clear()); + server.notify_client( + client, + ServerMsg::broadcast("Removed server description".to_string()), + ); + }, } } diff --git a/server/src/lib.rs b/server/src/lib.rs index eb302c4a44..cd8e36ec34 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -45,6 +45,7 @@ use std::{ i32, sync::Arc, time::{Duration, Instant}, + ops::{Deref, DerefMut}, }; #[cfg(not(feature = "worldgen"))] use test_world::{World, WORLD_SIZE}; @@ -80,7 +81,6 @@ pub struct Server { thread_pool: ThreadPool, - server_info: ServerInfo, metrics: ServerMetrics, tick_metrics: TickMetrics, } @@ -237,13 +237,6 @@ impl Server { .name("veloren-worker".into()) .build(), - server_info: ServerInfo { - name: settings.server_name.clone(), - description: settings.server_description.clone(), - git_hash: common::util::GIT_HASH.to_string(), - git_date: common::util::GIT_DATE.to_string(), - auth_provider: settings.auth_server_address.clone(), - }, metrics, tick_metrics, }; @@ -264,11 +257,32 @@ impl Server { Ok(this) } + pub fn get_server_info(&self) -> ServerInfo { + let settings = self.state.ecs().fetch::(); + ServerInfo { + name: settings.server_name.clone(), + description: settings.server_description.clone(), + git_hash: common::util::GIT_HASH.to_string(), + git_date: common::util::GIT_DATE.to_string(), + auth_provider: settings.auth_server_address.clone(), + } + } + pub fn with_thread_pool(mut self, thread_pool: ThreadPool) -> Self { self.thread_pool = thread_pool; self } + /// Get a reference to the server's settings + pub fn settings(&self) -> impl Deref + '_ { + self.state.ecs().fetch::() + } + + /// Get a mutable reference to the server's settings + pub fn settings_mut(&mut self) -> impl DerefMut + '_ { + self.state.ecs_mut().fetch_mut::() + } + /// Get a reference to the server's game state. pub fn state(&self) -> &State { &self.state } @@ -596,7 +610,7 @@ impl Server { // Send client their entity entity_package: TrackedComps::fetch(&self.state.ecs()) .create_entity_package(entity, None, None, None), - server_info: self.server_info.clone(), + server_info: self.get_server_info(), time_of_day: *self.state.ecs().read_resource(), world_map: (WORLD_SIZE.map(|e| e as u32), self.map.clone()), }); diff --git a/server/src/settings.rs b/server/src/settings.rs index 311c20fcfb..0ee4e88cba 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -98,8 +98,7 @@ impl ServerSettings { let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()) .expect("Failed serialize settings."); config_file - .write_all(s.as_bytes()) - .expect("Failed to write to config file."); + .write_all(s.as_bytes())?; Ok(()) } @@ -135,4 +134,12 @@ impl ServerSettings { } fn get_settings_path() -> PathBuf { PathBuf::from(r"server_settings.ron") } + + pub fn edit(&mut self, f: impl FnOnce(&mut Self) -> R) -> R { + let r = f(self); + self + .save_to_file() + .unwrap_or_else(|err| warn!("Failed to save settings: {:?}", err)); + r + } }