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,
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",

View File

@ -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"

View File

@ -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::<ServerSettings>()
.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()),
);
},
}
}

View File

@ -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::<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 {
self.thread_pool = thread_pool;
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.
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()),
});

View File

@ -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<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
}
}