From 651a3a4d7f97de7198b9d4b7ec409e64dad8284c Mon Sep 17 00:00:00 2001 From: rasul Date: Sat, 4 Apr 2020 08:57:08 -0500 Subject: [PATCH] command to set room description --- src/command/set/room/description.rs | 50 +++++++++++++++++++++++++++++ src/command/set/room/mod.rs | 2 ++ src/command/set/room/room.rs | 3 ++ 3 files changed, 55 insertions(+) create mode 100644 src/command/set/room/description.rs diff --git a/src/command/set/room/description.rs b/src/command/set/room/description.rs new file mode 100644 index 0000000..59827b2 --- /dev/null +++ b/src/command/set/room/description.rs @@ -0,0 +1,50 @@ +use std::str::FromStr; + +use mio::Token; + +use crate::command::CommandSetRoom; +use crate::database::Db; +use crate::queue::SendQueue; +use crate::{option_send, try_option_send_error, try_send, try_send_error}; + +impl CommandSetRoom { + /// Set the specified line in the description to the specified text. + pub fn dispatch_description(&self, args: String, token: Token, db: &mut Db) -> SendQueue { + // parse the arguments + if args.is_empty() { + return SendQueue( + vec![(token, "Set what line to what description?".into(), true)].into(), + ); + } + + let space = option_send!(token, args.find(' '), "Set what line to what description?"); + + let mut line_string = args.clone(); + let mut description = line_string.split_off(space); + description.remove(0); + let line_num: usize = try_send!( + token, + u8::from_str(&line_string), + "Can't figure out line number from '{}'.", + line_string + ) + .into(); + + // load the player and room + let player = try_option_send_error!(token, db.get_connected_player(token)); + let mut room = try_option_send_error!(token, db.load_room(player.location)); + + // make sure description has enough lines + if line_num > room.description.len().into() { + for _ in room.description.len()..line_num { + room.description.push("".into()); + } + } + + // set description and save + room.description[line_num - 1] = description; + let _ = try_send_error!(token, db.save_room(&room)); + + SendQueue::ok(token) + } +} diff --git a/src/command/set/room/mod.rs b/src/command/set/room/mod.rs index 3c78711..6fb0b0f 100644 --- a/src/command/set/room/mod.rs +++ b/src/command/set/room/mod.rs @@ -1,5 +1,7 @@ +mod description; mod name; mod room; +pub use description::*; pub use name::*; pub use room::*; diff --git a/src/command/set/room/room.rs b/src/command/set/room/room.rs index 0fd1152..eb547cb 100644 --- a/src/command/set/room/room.rs +++ b/src/command/set/room/room.rs @@ -9,6 +9,7 @@ use crate::queue::SendQueue; #[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)] pub enum CommandSetRoom { + Description, Name, Default, } @@ -22,6 +23,7 @@ impl Default for CommandSetRoom { impl Parse for CommandSetRoom { fn help(&self) -> &str { match self { + Self::Description => "set room description LINE DESCRIPTION :: Set line LINE of description to DESCRIPTION.", Self::Name => "set room name NEW_NAME :: Set room name to NEW_NAME.", Self::Default => "", } @@ -29,6 +31,7 @@ impl Parse for CommandSetRoom { fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue { match self { + Self::Description => Self::dispatch_description, Self::Name => Self::dispatch_name, Self::Default => Self::dispatch_default, }