Refactored kicking the player into its own function + ran fmt on project

This commit is contained in:
tylerlowrey 2020-08-09 11:14:44 -04:00 committed by Joshua Yanovski
parent d57adc518e
commit 2b0c1e89f3
8 changed files with 62 additions and 58 deletions

View File

@ -1399,7 +1399,7 @@ impl Client {
ServerMsg::Kicked(reason) => {
frontend_events.push(Event::Kicked(reason.clone()));
self.singleton_stream.send(ClientMsg::Terminate)?;
}
},
ServerMsg::CharacterListUpdate(character_list) => {
self.character_list.characters = character_list;
self.character_list.loading = false;

View File

@ -202,7 +202,7 @@ impl ChatCommand {
ChatCommand::Ban => cmd(
vec![Any("username", Required), Message(Optional)],
"Ban a player with a given username",
Admin
Admin,
),
ChatCommand::Build => cmd(vec![], "Toggles build mode on and off", Admin),
ChatCommand::Campfire => cmd(vec![], "Spawns a campfire", Admin),
@ -277,7 +277,7 @@ impl ChatCommand {
ChatCommand::Kick => cmd(
vec![Any("username", Required), Message(Optional)],
"Kick a player with a given username",
Admin
Admin,
),
ChatCommand::Kill => cmd(vec![], "Kill yourself", NoAdmin),
ChatCommand::KillNpcs => cmd(vec![], "Kill the NPCs", Admin),
@ -373,10 +373,10 @@ impl ChatCommand {
"Teleport to another player",
Admin,
),
ChatCommand::Unban => cmd (
ChatCommand::Unban => cmd(
vec![Any("username", Required)],
"Remove the ban for the given username",
Admin
Admin,
),
ChatCommand::Version => cmd(vec![], "Prints server version", NoAdmin),
ChatCommand::Waypoint => {

View File

@ -1814,6 +1814,10 @@ fn handle_whitelist(
}
}
fn kick_player(server: &mut Server, target_player: EcsEntity, reason: &str) {
server.notify_client(target_player, ServerMsg::Kicked(reason.to_string()));
}
fn handle_kick(
server: &mut Server,
client: EcsEntity,
@ -1821,8 +1825,10 @@ fn handle_kick(
args: String,
action: &ChatCommand,
) {
if let (Some(target_alias), reason_opt) = scan_fmt_some!(&args, &action.arg_fmt(), String, String) {
let reason = reason_opt.unwrap_or(String::new());
if let (Some(target_alias), reason_opt) =
scan_fmt_some!(&args, &action.arg_fmt(), String, String)
{
let reason = reason_opt.unwrap_or_default();
let ecs = server.state.ecs();
let target_player_opt = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
.join()
@ -1830,46 +1836,46 @@ fn handle_kick(
.map(|(entity, _)| entity);
if let Some(target_player) = target_player_opt {
server.notify_client(
target_player,
ServerMsg::Kicked(reason.clone())
);
kick_player(server, target_player, &reason);
server.notify_client(
client,
ChatType::CommandInfo.server_msg(
format!("Kicked {} from the server with reason: {}",
target_alias,
reason))
ChatType::CommandInfo.server_msg(format!(
"Kicked {} from the server with reason: {}",
target_alias, reason
)),
);
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(format!("Player with alias {} not found", target_alias))
ChatType::CommandError
.server_msg(format!("Player with alias {} not found", target_alias)),
)
}
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(action.help_string())
client,
ChatType::CommandError.server_msg(action.help_string()),
);
}
}
fn handle_ban(
server: &mut Server,
client: EcsEntity,
target: EcsEntity,
_target: EcsEntity,
args: String,
action: &ChatCommand,
) {
if let (Some(target_alias), reason_opt) = scan_fmt_some!(&args, &action.arg_fmt(), String, String) {
let reason = reason_opt.unwrap_or(String::new());
if let (Some(target_alias), reason_opt) =
scan_fmt_some!(&args, &action.arg_fmt(), String, String)
{
let reason = reason_opt.unwrap_or_default();
if server.settings().banlist.contains_key(&target_alias) {
server.notify_client(
client,
ChatType::CommandError.server_msg(format!("{} is already on the banlist", target_alias))
ChatType::CommandError
.server_msg(format!("{} is already on the banlist", target_alias)),
)
} else {
server.settings_mut().edit(|s| {
@ -1877,27 +1883,26 @@ fn handle_ban(
});
server.notify_client(
client,
ChatType::CommandInfo.server_msg(
format!("Added {} to the banlist with reason: {}",
target_alias,
reason))
ChatType::CommandInfo.server_msg(format!(
"Added {} to the banlist with reason: {}",
target_alias, reason
)),
);
// If the player is online kick them
let ecs = server.state.ecs();
let entity_opt = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
let target_player_opt = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
.join()
.find(|(_, player)| player.alias == target_alias)
.map(|(entity, _)| entity);
if let Some(_) = entity_opt {
get_handler(&ChatCommand::Kick)(server, client, target, format!("{} {}", target_alias, reason), &ChatCommand::Kick)
if let Some(target_player) = target_player_opt {
kick_player(server, target_player, &reason);
}
}
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(action.help_string())
ChatType::CommandError.server_msg(action.help_string()),
);
}
}
@ -1920,7 +1925,7 @@ fn handle_unban(
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(action.help_string())
ChatType::CommandError.server_msg(action.help_string()),
);
}
}

View File

@ -53,7 +53,7 @@ impl LoginProvider {
&mut self,
username_or_token: &str,
whitelist: &[String],
banlist: &HashMap<String, String>
banlist: &HashMap<String, String>,
) -> Result<(String, Uuid), RegisterError> {
self
// resolve user information

View File

@ -1,9 +1,9 @@
use hashbrown::HashMap;
use portpicker::pick_unused_port;
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;

View File

@ -88,14 +88,17 @@ impl Sys {
view_distance,
token_or_username,
} => {
let (username, uuid) =
match login_provider.try_login(&token_or_username, &settings.whitelist, &settings.banlist) {
Err(err) => {
client.error_state(RequestStateError::RegisterDenied(err));
break Ok(());
},
Ok((username, uuid)) => (username, uuid),
};
let (username, uuid) = match login_provider.try_login(
&token_or_username,
&settings.whitelist,
&settings.banlist,
) {
Err(err) => {
client.error_state(RequestStateError::RegisterDenied(err));
break Ok(());
},
Ok((username, uuid)) => (username, uuid),
};
let vd =
view_distance.map(|vd| vd.min(settings.max_view_distance.unwrap_or(vd)));

View File

@ -125,13 +125,11 @@ impl PlayState for MainMenuState {
client::Error::NotOnWhitelist => {
localized_strings.get("main.login.not_on_whitelist").into()
},
client::Error::Banned(reason) => {
format!(
"{}: {}",
localized_strings.get("main.login.banned"),
reason
)
},
client::Error::Banned(reason) => format!(
"{}: {}",
localized_strings.get("main.login.banned"),
reason
),
client::Error::InvalidCharacter => {
localized_strings.get("main.login.invalid_character").into()
},

View File

@ -164,14 +164,12 @@ impl SessionState {
});
},
client::Event::Kicked(reason) => {
global_state.info_message = Some(
format!(
"{}: {}",
self.voxygen_i18n.get("main.login.kicked").to_string(),
reason
)
);
return Ok(TickAction::Disconnect)
global_state.info_message = Some(format!(
"{}: {}",
self.voxygen_i18n.get("main.login.kicked").to_string(),
reason
));
return Ok(TickAction::Disconnect);
},
client::Event::Notification(n) => {
self.hud.new_notification(n);