subcommand: set room zone : to set zone for the current room

master
rasul 5 years ago
parent 222aa967a1
commit 24c6c01da7

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

@ -16,6 +16,9 @@ pub enum CommandSetRoom {
/// Set the room name
Name,
/// Set the parent zone
Zone,
Default,
}
@ -30,6 +33,7 @@ impl Parse for CommandSetRoom {
match self {
Self::Description(_) => "",
Self::Name => "set room name NEW_NAME :: Set room name to NEW_NAME.",
Self::Zone => "set room zone ZONE_ID :: Set room zone to ZONE_ID.",
Self::Default => "",
}
}
@ -56,6 +60,7 @@ impl Parse for CommandSetRoom {
match self {
Self::Description(_) => Self::dispatch_map_subcommand,
Self::Name => Self::dispatch_name,
Self::Zone => Self::dispatch_zone,
Self::Default => Self::dispatch_default,
}
}

@ -0,0 +1,34 @@
use mio::Token;
use crate::command::CommandSetRoom;
use crate::database::Db;
use crate::id::Id;
use crate::queue::SendQueue;
use crate::{try_option_send_error, try_send_error};
impl CommandSetRoom {
/// Change the room's containing zone
pub fn dispatch_zone(&self, args: String, token: Token, db: &mut Db) -> SendQueue {
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));
let mut start_zone = try_option_send_error!(token, db.load_zone(room.id));
let mut end_zone = try_option_send_error!(
token,
db.load_zone(try_send_error!(token, Id::parse_str(&args)))
);
if start_zone == end_zone {
return (token, "Room is already in that zone").into();
}
room.zone = end_zone.id;
start_zone.areas.remove(&room.id);
end_zone.areas.insert(room.id);
let _ = try_send_error!(token, db.save_room(&room));
let _ = try_send_error!(token, db.save_zone(&start_zone));
let _ = try_send_error!(token, db.save_zone(&end_zone));
SendQueue::ok(token)
}
}
Loading…
Cancel
Save