From faadf904e075f8d7ba485549486f213cce1a2ad4 Mon Sep 17 00:00:00 2001 From: rasul Date: Mon, 6 Apr 2020 14:13:06 -0500 Subject: [PATCH] subcommand to change player password --- src/command/set/player/mod.rs | 1 + src/command/set/player/password.rs | 24 ++++++++++++++++++++++++ src/command/set/player/player.rs | 5 +++++ 3 files changed, 30 insertions(+) create mode 100644 src/command/set/player/password.rs diff --git a/src/command/set/player/mod.rs b/src/command/set/player/mod.rs index 6ffd43b..5e49e46 100644 --- a/src/command/set/player/mod.rs +++ b/src/command/set/player/mod.rs @@ -1,4 +1,5 @@ mod name; +mod password; mod player; pub use name::*; diff --git a/src/command/set/player/password.rs b/src/command/set/player/password.rs new file mode 100644 index 0000000..6f3e47f --- /dev/null +++ b/src/command/set/player/password.rs @@ -0,0 +1,24 @@ +use mio::Token; + +use crate::command::CommandSetPlayer; +use crate::database::Db; +use crate::queue::SendQueue; +use crate::{try_option_send_error, try_send_error}; + +impl CommandSetPlayer { + /// Set the player's password + pub fn dispatch_password(&self, args: String, token: Token, db: &mut Db) -> SendQueue { + let mut player = try_option_send_error!(token, db.get_connected_player(token)); + + if args.is_empty() { + return SendQueue(vec![(token, "Password can't be empty".into(), true, None)].into()); + } + + player.password = args; + + let _ = try_send_error!(token, db.save_player(&player)); + let _ = try_send_error!(token, db.save_connected_player(token, &player)); + + SendQueue::ok(token) + } +} diff --git a/src/command/set/player/player.rs b/src/command/set/player/player.rs index 6797d55..a7c4aa7 100644 --- a/src/command/set/player/player.rs +++ b/src/command/set/player/player.rs @@ -13,6 +13,9 @@ pub enum CommandSetPlayer { /// Set the player's name Name, + /// Set the password + Password, + Default, } @@ -26,6 +29,7 @@ impl Parse for CommandSetPlayer { fn help(&self) -> &str { match self { Self::Name => "set player name NEW_NAME :: Set player name to NEW_NAME.", + Self::Password => "set player password NEW_PASS :: Set password to NEW_PASS", Self::Default => "", } } @@ -33,6 +37,7 @@ impl Parse for CommandSetPlayer { fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue { match self { Self::Name => Self::dispatch_name, + Self::Password => Self::dispatch_password, Self::Default => Self::dispatch_default, } }