diff --git a/src/command/set/room/mod.rs b/src/command/set/room/mod.rs index 13b37d6..86aaa93 100644 --- a/src/command/set/room/mod.rs +++ b/src/command/set/room/mod.rs @@ -1,6 +1,7 @@ mod description; mod name; mod room; +mod zone; pub use description::*; pub use room::*; diff --git a/src/command/set/room/room.rs b/src/command/set/room/room.rs index c2f269b..70a29e6 100644 --- a/src/command/set/room/room.rs +++ b/src/command/set/room/room.rs @@ -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, } } diff --git a/src/command/set/room/zone.rs b/src/command/set/room/zone.rs new file mode 100644 index 0000000..dac8367 --- /dev/null +++ b/src/command/set/room/zone.rs @@ -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) + } +}