command to set room description

master
rasul 5 years ago
parent 540d4fb0fb
commit 651a3a4d7f

@ -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)
}
}

@ -1,5 +1,7 @@
mod description;
mod name;
mod room;
pub use description::*;
pub use name::*;
pub use room::*;

@ -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,
}

Loading…
Cancel
Save