Added settings editing, set_motd command

This commit is contained in:
Joshua Barretto 2020-06-25 14:56:21 +01:00
parent 0ce7d6c876
commit d9cd37056d
5 changed files with 68 additions and 23 deletions

View File

@ -48,6 +48,7 @@ pub enum ChatCommand {
Players, Players,
RemoveLights, RemoveLights,
SetLevel, SetLevel,
SetMotd,
Spawn, Spawn,
Sudo, Sudo,
Tell, Tell,
@ -80,6 +81,7 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[
ChatCommand::Players, ChatCommand::Players,
ChatCommand::RemoveLights, ChatCommand::RemoveLights,
ChatCommand::SetLevel, ChatCommand::SetLevel,
ChatCommand::SetMotd,
ChatCommand::Spawn, ChatCommand::Spawn,
ChatCommand::Sudo, ChatCommand::Sudo,
ChatCommand::Tell, ChatCommand::Tell,
@ -226,7 +228,7 @@ impl ChatCommand {
"Spawn entity with light", "Spawn entity with light",
true, true,
), ),
ChatCommand::Motd => cmd(vec![Message], "Set the server description", true), ChatCommand::Motd => cmd(vec![Message], "View the server description", false),
ChatCommand::Object => cmd( ChatCommand::Object => cmd(
vec![Enum("object", OBJECTS.clone(), Required)], vec![Enum("object", OBJECTS.clone(), Required)],
"Spawn an object", "Spawn an object",
@ -243,6 +245,11 @@ impl ChatCommand {
"Set player Level", "Set player Level",
true, true,
), ),
ChatCommand::SetMotd => cmd(
vec![Message],
"Set the server description",
true,
),
ChatCommand::Spawn => cmd( ChatCommand::Spawn => cmd(
vec![ vec![
Enum("alignment", ALIGNMENTS.clone(), Required), Enum("alignment", ALIGNMENTS.clone(), Required),
@ -303,6 +310,7 @@ impl ChatCommand {
ChatCommand::Players => "players", ChatCommand::Players => "players",
ChatCommand::RemoveLights => "remove_lights", ChatCommand::RemoveLights => "remove_lights",
ChatCommand::SetLevel => "set_level", ChatCommand::SetLevel => "set_level",
ChatCommand::SetMotd => "set_motd",
ChatCommand::Spawn => "spawn", ChatCommand::Spawn => "spawn",
ChatCommand::Sudo => "sudo", ChatCommand::Sudo => "sudo",
ChatCommand::Tell => "tell", ChatCommand::Tell => "tell",

View File

@ -14,7 +14,7 @@ world = { package = "veloren-world", path = "../world" }
specs-idvs = { git = "https://gitlab.com/veloren/specs-idvs.git" } 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"] } specs = { version = "0.15.1", features = ["shred-derive"] }
vek = "0.11.0" vek = "0.11.0"
uvth = "3.1.1" uvth = "3.1.1"

View File

@ -2,7 +2,7 @@
//! To implement a new command, add an instance of `ChatCommand` to //! To implement a new command, add an instance of `ChatCommand` to
//! `CHAT_COMMANDS` and provide a handler function. //! `CHAT_COMMANDS` and provide a handler function.
use crate::{Server, ServerSettings, StateExt}; use crate::{Server, StateExt};
use chrono::{NaiveTime, Timelike}; use chrono::{NaiveTime, Timelike};
use common::{ use common::{
assets, assets,
@ -83,6 +83,7 @@ fn get_handler(cmd: &ChatCommand) -> CommandHandler {
ChatCommand::Players => handle_players, ChatCommand::Players => handle_players,
ChatCommand::RemoveLights => handle_remove_lights, ChatCommand::RemoveLights => handle_remove_lights,
ChatCommand::SetLevel => handle_set_level, ChatCommand::SetLevel => handle_set_level,
ChatCommand::SetMotd => handle_set_motd,
ChatCommand::Spawn => handle_spawn, ChatCommand::Spawn => handle_spawn,
ChatCommand::Sudo => handle_sudo, ChatCommand::Sudo => handle_sudo,
ChatCommand::Tell => handle_tell, ChatCommand::Tell => handle_tell,
@ -175,24 +176,39 @@ fn handle_motd(
_target: EcsEntity, _target: EcsEntity,
args: String, args: String,
action: &ChatCommand, 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) { match scan_fmt!(&args, &action.arg_fmt(), String) {
Ok(msg) => { Ok(msg) => {
server server
.state_mut() .settings_mut()
.ecs_mut() .edit(|s| s.server_description = msg.clone());
.fetch_mut::<ServerSettings>()
.server_description = msg.clone();
server.server_info.description = msg.clone();
server.notify_client( server.notify_client(
client, client,
ServerMsg::private(format!("Server description set to \"{}\"", msg)), ServerMsg::private(format!("Server description set to \"{}\"", msg)),
); );
}, },
Err(_) => server.notify_client( Err(_) => {
client, server
ServerMsg::broadcast(format!("{}", server.server_info.description)), .settings_mut()
), .edit(|s| s.server_description.clear());
server.notify_client(
client,
ServerMsg::broadcast("Removed server description".to_string()),
);
},
} }
} }

View File

@ -45,6 +45,7 @@ use std::{
i32, i32,
sync::Arc, sync::Arc,
time::{Duration, Instant}, time::{Duration, Instant},
ops::{Deref, DerefMut},
}; };
#[cfg(not(feature = "worldgen"))] #[cfg(not(feature = "worldgen"))]
use test_world::{World, WORLD_SIZE}; use test_world::{World, WORLD_SIZE};
@ -80,7 +81,6 @@ pub struct Server {
thread_pool: ThreadPool, thread_pool: ThreadPool,
server_info: ServerInfo,
metrics: ServerMetrics, metrics: ServerMetrics,
tick_metrics: TickMetrics, tick_metrics: TickMetrics,
} }
@ -237,13 +237,6 @@ impl Server {
.name("veloren-worker".into()) .name("veloren-worker".into())
.build(), .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, metrics,
tick_metrics, tick_metrics,
}; };
@ -264,11 +257,32 @@ impl Server {
Ok(this) Ok(this)
} }
pub fn get_server_info(&self) -> ServerInfo {
let settings = self.state.ecs().fetch::<ServerSettings>();
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 { pub fn with_thread_pool(mut self, thread_pool: ThreadPool) -> Self {
self.thread_pool = thread_pool; self.thread_pool = thread_pool;
self self
} }
/// Get a reference to the server's settings
pub fn settings(&self) -> impl Deref<Target=ServerSettings> + '_ {
self.state.ecs().fetch::<ServerSettings>()
}
/// Get a mutable reference to the server's settings
pub fn settings_mut(&mut self) -> impl DerefMut<Target=ServerSettings> + '_ {
self.state.ecs_mut().fetch_mut::<ServerSettings>()
}
/// Get a reference to the server's game state. /// Get a reference to the server's game state.
pub fn state(&self) -> &State { &self.state } pub fn state(&self) -> &State { &self.state }
@ -596,7 +610,7 @@ impl Server {
// Send client their entity // Send client their entity
entity_package: TrackedComps::fetch(&self.state.ecs()) entity_package: TrackedComps::fetch(&self.state.ecs())
.create_entity_package(entity, None, None, None), .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(), time_of_day: *self.state.ecs().read_resource(),
world_map: (WORLD_SIZE.map(|e| e as u32), self.map.clone()), world_map: (WORLD_SIZE.map(|e| e as u32), self.map.clone()),
}); });

View File

@ -98,8 +98,7 @@ impl ServerSettings {
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()) let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default())
.expect("Failed serialize settings."); .expect("Failed serialize settings.");
config_file config_file
.write_all(s.as_bytes()) .write_all(s.as_bytes())?;
.expect("Failed to write to config file.");
Ok(()) Ok(())
} }
@ -135,4 +134,12 @@ impl ServerSettings {
} }
fn get_settings_path() -> PathBuf { PathBuf::from(r"server_settings.ron") } fn get_settings_path() -> PathBuf { PathBuf::from(r"server_settings.ron") }
pub fn edit<R>(&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
}
} }