Subcommand for working with room descriptions

Create the CommandSetRoomDescription sub command, move the former description
command into it and rename it set, and the delete command.
master
rasul 5 years ago
parent 651a3a4d7f
commit 87367644cc

@ -0,0 +1,42 @@
use std::str::FromStr;
use mio::Token;
use crate::command::CommandSetRoomDescription;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send, try_send_error};
impl CommandSetRoomDescription {
/// Set the specified line in the description to the specified text.
pub fn dispatch_delete(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
// make sure something was provided
if args.is_empty() {
return SendQueue(vec![(token, "Delete which line?".into(), true)].into());
}
// try and get the line number
let line_num: usize = try_send!(
token,
u8::from_str(&args),
"Can't figure out line number from '{}'.",
&args
)
.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() || line_num <= 0 {
return SendQueue(vec![(token, "Line doeesn't exist".into(), true)].into());
}
// remove the line and save
room.description.remove(line_num-1);
let _ = try_send_error!(token, db.save_room(&room));
SendQueue::ok(token)
}
}

@ -0,0 +1,39 @@
use std::default::Default;
use mio::Token;
use strum_macros::{Display, EnumIter};
use crate::command::Parse;
use crate::database::Db;
use crate::queue::SendQueue;
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetRoomDescription {
Delete,
Set,
Default,
}
impl Default for CommandSetRoomDescription {
fn default() -> Self {
Self::Default
}
}
impl Parse for CommandSetRoomDescription {
fn help(&self) -> &str {
match self {
Self::Delete => "set room description delete LINE :: Delete line LINE from room description",
Self::Set => "set room description set LINE DESC :: Set room description line LINE to DESC",
Self::Default => "",
}
}
fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue {
match self {
Self::Delete => Self::dispatch_delete,
Self::Set => Self::dispatch_set,
Self::Default => Self::dispatch_default,
}
}
}

@ -0,0 +1,7 @@
mod delete;
mod description;
mod set;
pub use delete::*;
pub use description::*;
pub use set::*;

@ -2,23 +2,23 @@ use std::str::FromStr;
use mio::Token;
use crate::command::CommandSetRoom;
use crate::command::CommandSetRoomDescription;
use crate::database::Db;
use crate::queue::SendQueue;
use crate::{option_send, try_option_send_error, try_send, try_send_error};
impl CommandSetRoom {
impl CommandSetRoomDescription {
/// 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
pub fn dispatch_set(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
// make sure something was provided
if args.is_empty() {
return SendQueue(
vec![(token, "Set what line to what description?".into(), true)].into(),
);
}
// parse the arguments
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);

@ -3,13 +3,13 @@ use std::default::Default;
use mio::Token;
use strum_macros::{Display, EnumIter};
use crate::command::Parse;
use crate::command::{CommandSetRoomDescription, Parse, ParserError};
use crate::database::Db;
use crate::queue::SendQueue;
#[derive(Clone, Debug, Display, EnumIter, Eq, Ord, PartialEq, PartialOrd)]
pub enum CommandSetRoom {
Description,
Description(CommandSetRoomDescription),
Name,
Default,
}
@ -23,15 +23,33 @@ 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::Description(_) => "",
Self::Name => "set room name NEW_NAME :: Set room name to NEW_NAME.",
Self::Default => "",
}
}
fn parse_subcommand(&self, s: String) -> Result<(Self, String), ParserError> {
match self {
Self::Description(_) => {
let (command, args) = CommandSetRoomDescription::parse(s)?;
Ok((Self::Description(command), args))
},
Self::Default => Err(ParserError::Default),
_ => Ok((self.clone(), s)),
}
}
fn dispatch_map_subcommand(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
match self {
Self::Description(command) => command.dispatch(command, args, token, db),
_ => SendQueue::new(),
}
}
fn dispatch_map(&self) -> fn(&Self, String, Token, &mut Db) -> SendQueue {
match self {
Self::Description => Self::dispatch_description,
Self::Description(_) => Self::dispatch_map_subcommand,
Self::Name => Self::dispatch_name,
Self::Default => Self::dispatch_default,
}

Loading…
Cancel
Save