From 4653eb63c44337d1664d8fb28708cfa0a45f983d Mon Sep 17 00:00:00 2001 From: tylerlowrey Date: Sat, 8 Aug 2020 00:11:30 -0400 Subject: [PATCH] Converted banlist from vector of tuples to HashMap --- server/src/cmd.rs | 6 +++--- server/src/login_provider.rs | 6 +++--- server/src/settings.rs | 5 +++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index d55f30a832..798b1eb4f2 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -1868,14 +1868,14 @@ fn handle_ban( let banlist = server.settings().banlist.clone(); - if let Some(_) = banlist.iter().find(|x| x.0 == target_alias) { + if banlist.contains_key(&target_alias) { server.notify_client( client, ChatType::CommandError.server_msg(format!("{} is already on the banlist", target_alias)) ) } else { server.settings_mut().edit(|s| { - s.banlist.push((target_alias.clone(), reason.clone())) + s.banlist.insert(target_alias.clone(), reason.clone()); }); server.notify_client( client, @@ -1913,7 +1913,7 @@ fn handle_unban( ) { if let Ok(username) = scan_fmt!(&args, &action.arg_fmt(), String) { server.settings_mut().edit(|s| { - s.banlist.retain(|x| !(x.0).eq_ignore_ascii_case(&username)) + s.banlist.remove(&username); }); server.notify_client( client, diff --git a/server/src/login_provider.rs b/server/src/login_provider.rs index 1bf3f7bf24..fc09e05c88 100644 --- a/server/src/login_provider.rs +++ b/server/src/login_provider.rs @@ -53,7 +53,7 @@ impl LoginProvider { &mut self, username_or_token: &str, whitelist: &[String], - banlist: &[(String, String)] + banlist: &HashMap ) -> Result<(String, Uuid), RegisterError> { self // resolve user information @@ -61,9 +61,9 @@ impl LoginProvider { // if found, check name against whitelist or if user is admin .and_then(|(username, uuid)| { // user cannot join if they are listed on the banlist - if let Some(ban_record) = banlist.iter().find(|x| x.0.eq_ignore_ascii_case(&username)) { + if let Some(ban_record) = banlist.get(&username) { // Pull reason string out of ban record and send a copy of it - return Err(RegisterError::Banned(ban_record.1.clone())); + return Err(RegisterError::Banned(ban_record.clone())); } // user can only join if he is admin, the whitelist is empty (everyone can join) diff --git a/server/src/settings.rs b/server/src/settings.rs index b52e73eada..643102a39c 100644 --- a/server/src/settings.rs +++ b/server/src/settings.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf, time::Duration}; use tracing::{error, warn}; use world::sim::FileOpts; +use hashbrown::HashMap; const DEFAULT_WORLD_SEED: u32 = 59686; @@ -20,7 +21,7 @@ pub struct ServerSettings { pub start_time: f64, pub admins: Vec, pub whitelist: Vec, - pub banlist: Vec<(String, String)>, + pub banlist: HashMap, /// When set to None, loads the default map file (if available); otherwise, /// uses the value of the file options to decide how to proceed. pub map_file: Option, @@ -45,7 +46,7 @@ impl Default for ServerSettings { map_file: None, admins: Vec::new(), whitelist: Vec::new(), - banlist: Vec::new(), + banlist: HashMap::new(), persistence_db_dir: "saves".to_owned(), max_view_distance: Some(30), banned_words_files: Vec::new(),