diff --git a/client/src/lib.rs b/client/src/lib.rs index f7dd86e9d7..df7aa88a1d 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -492,8 +492,12 @@ impl Client { frontend_events.push(Event::Chat { chat_type, message }) } ServerMsg::SetPlayerEntity(uid) => { - self.entity = self.state.ecs().entity_from_uid(uid).unwrap() - } // TODO: Don't unwrap here! + if let Some(entity) = self.state.ecs().entity_from_uid(uid) { + self.entity = entity; + } else { + return Err(Error::Other("Failed to find entity from uid.".to_owned())); + } + } ServerMsg::EcsSync(sync_package) => { self.state.ecs_mut().sync_with_package(sync_package) } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 5b334f8dba..39586624bf 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -470,7 +470,11 @@ fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action let header_message: String = format!("{} online players: \n", count); if count > 0 { let mut player_iter = players.join(); - let first = player_iter.next().unwrap().alias.to_owned(); + let first = player_iter + .next() + .expect("Player iterator returned none.") + .alias + .to_owned(); let player_list = player_iter.fold(first, |mut s, p| { s += ",\n"; s += &p.alias; diff --git a/server/src/lib.rs b/server/src/lib.rs index ed3bfc0f06..ecce8c1850 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -126,7 +126,8 @@ impl Server { git_hash: common::util::GIT_HASH.to_string(), git_date: common::util::GIT_DATE.to_string(), }, - metrics: ServerMetrics::new(settings.metrics_address), + metrics: ServerMetrics::new(settings.metrics_address) + .expect("Failed to initialize server metrics submodule."), accounts: AuthProvider::new(), server_settings: settings.clone(), }; @@ -245,7 +246,7 @@ impl Server { .ecs() .read_storage::() .get(entity) - .unwrap() + .expect("Failed to fetch entity.") .alias, ) { state.write_component(entity, comp::Admin); @@ -303,7 +304,7 @@ impl Server { .ecs() .read_storage::() .get(entity) - .unwrap() + .expect("Failed to fetch entity") .0; // TODO: Player height diff --git a/server/src/metrics.rs b/server/src/metrics.rs index 497187717c..778f71fa7e 100644 --- a/server/src/metrics.rs +++ b/server/src/metrics.rs @@ -1,6 +1,7 @@ use log::info; use prometheus::{Encoder, Gauge, IntGauge, IntGaugeVec, Opts, Registry, TextEncoder}; use rouille::{router, Server}; +use std::error::Error; use std::{ convert::TryInto, net::SocketAddr, @@ -28,66 +29,65 @@ pub struct ServerMetrics { } impl ServerMetrics { - pub fn new(addr: SocketAddr) -> Self { + pub fn new(addr: SocketAddr) -> Result> { let opts = Opts::new( "player_online", "shows the number of clients connected to the server", ); - let player_online = IntGauge::with_opts(opts).unwrap(); + let player_online = IntGauge::with_opts(opts)?; let opts = Opts::new( "entity_count", "number of all entities currently active on the server", ); - let entity_count = IntGauge::with_opts(opts).unwrap(); + let entity_count = IntGauge::with_opts(opts)?; let opts = Opts::new("veloren_build_info", "Build information") .const_label("hash", &common::util::GIT_HASH) .const_label("version", ""); - let build_info = IntGauge::with_opts(opts).unwrap(); + let build_info = IntGauge::with_opts(opts)?; let opts = Opts::new( "veloren_start_time", "start time of the server in seconds since EPOCH", ); - let start_time = IntGauge::with_opts(opts).unwrap(); + let start_time = IntGauge::with_opts(opts)?; let opts = Opts::new("time_of_day", "ingame time in ingame-seconds"); - let time_of_day = Gauge::with_opts(opts).unwrap(); + let time_of_day = Gauge::with_opts(opts)?; let opts = Opts::new( "light_count", "number of all lights currently active on the server", ); - let light_count = IntGauge::with_opts(opts).unwrap(); + let light_count = IntGauge::with_opts(opts)?; let opts = Opts::new( "chonks_count", "number of all chonks currently active on the server", ); - let chonks_count = IntGauge::with_opts(opts).unwrap(); + let chonks_count = IntGauge::with_opts(opts)?; let opts = Opts::new( "chunks_count", "number of all chunks currently active on the server", ); - let chunks_count = IntGauge::with_opts(opts).unwrap(); + let chunks_count = IntGauge::with_opts(opts)?; let vec = IntGaugeVec::new( Opts::new("tick_time", "time in ns requiered for a tick of the server"), &["period"], - ) - .unwrap(); + )?; let tick_time = IntGaugeVec::from(vec); let since_the_epoch = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Time went backwards"); - start_time.set(since_the_epoch.as_secs().try_into().unwrap()); + start_time.set(since_the_epoch.as_secs().try_into()?); let registry = Registry::new(); //registry.register(Box::new(chonks_count.clone())).unwrap(); - registry.register(Box::new(player_online.clone())).unwrap(); - registry.register(Box::new(entity_count.clone())).unwrap(); - registry.register(Box::new(build_info.clone())).unwrap(); - registry.register(Box::new(start_time.clone())).unwrap(); - registry.register(Box::new(time_of_day.clone())).unwrap(); + registry.register(Box::new(player_online.clone()))?; + registry.register(Box::new(entity_count.clone()))?; + registry.register(Box::new(build_info.clone()))?; + registry.register(Box::new(start_time.clone()))?; + registry.register(Box::new(time_of_day.clone()))?; //registry.register(Box::new(light_count.clone())).unwrap(); - registry.register(Box::new(chonks_count.clone())).unwrap(); - registry.register(Box::new(chunks_count.clone())).unwrap(); - registry.register(Box::new(tick_time.clone())).unwrap(); + registry.register(Box::new(chonks_count.clone()))?; + registry.register(Box::new(chunks_count.clone()))?; + registry.register(Box::new(tick_time.clone()))?; let running = Arc::new(AtomicBool::new(true)); let running2 = running.clone(); @@ -100,8 +100,8 @@ impl ServerMetrics { let encoder = TextEncoder::new(); let mut buffer = vec![]; let mf = registry.gather(); - encoder.encode(&mf, &mut buffer).unwrap(); - rouille::Response::text(String::from_utf8(buffer).unwrap()) + encoder.encode(&mf, &mut buffer).expect("Failed to encoder metrics text."); + rouille::Response::text(String::from_utf8(buffer).expect("Failed to parse bytes as a string.")) }, _ => rouille::Response::empty_404() ) @@ -115,7 +115,7 @@ impl ServerMetrics { } })); - Self { + Ok(Self { chonks_count, chunks_count, player_online, @@ -128,7 +128,7 @@ impl ServerMetrics { running, handle, every_100th: 0, - } + }) } pub fn is_100th_tick(&mut self) -> bool { @@ -147,7 +147,7 @@ impl Drop for ServerMetrics { self.running.store(false, Ordering::Relaxed); let handle = self.handle.take(); handle - .unwrap() + .expect("ServerMetrics worker handle does not exist.") .join() .expect("Error shutting down prometheus metric exporter"); } diff --git a/server/src/settings.rs b/server/src/settings.rs index 40fdde6ae0..533c3891c4 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -27,23 +27,26 @@ impl Default for ServerSettings { server_description: "This is the best Veloren server.".to_owned(), max_players: 100, start_time: 9.0 * 3600.0, - admins: vec![ - "Pfau".to_owned(), - "zesterer".to_owned(), - "xMAC94x".to_owned(), - "Timo".to_owned(), - "Songtronix".to_owned(), - "Slipped".to_owned(), - "Sharp".to_owned(), - "Acrimon".to_owned(), - "imbris".to_owned(), - "YuriMomo".to_owned(), - "Vechro".to_owned(), - "AngelOnFira".to_owned(), - "Nancok".to_owned(), - "Qutrin".to_owned(), - "Mckol".to_owned(), - ], + admins: [ + "Pfau", + "zesterer", + "xMAC94x", + "Timo", + "Songtronix", + "Slipped", + "Sharp", + "Acrimon", + "imbris", + "YuriMomo", + "Vechro", + "AngelOnFira", + "Nancok", + "Qutrin", + "Mckol", + ] + .iter() + .map(|n| n.to_string()) + .collect(), } } } @@ -75,8 +78,11 @@ impl ServerSettings { let path = ServerSettings::get_settings_path(); let mut config_file = fs::File::create(path)?; - let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap(); - config_file.write_all(s.as_bytes()).unwrap(); + 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."); Ok(()) }