From eb9ba21144fa3b6504b64333122a2bef20b7be5e Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 12:05:37 -0500 Subject: [PATCH] method to get the root parent zone --- src/database/zones.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/database/zones.rs b/src/database/zones.rs index f288a34..9299d9d 100644 --- a/src/database/zones.rs +++ b/src/database/zones.rs @@ -10,6 +10,38 @@ use crate::try_log; use crate::world::Zone; impl Db { + /// Get root zone + pub fn root_zone(&self, zone: &Zone) -> RudeResult { + // get rid of the & + let mut zone = Zone { + id: zone.id, + parent: zone.parent, + name: zone.name.clone(), + users_visible: zone.users_visible, + areas: zone.areas.clone(), + }; + + // loop until find the root + loop { + zone = match self.load_zone(zone.id) { + Ok(Some(z)) => z, + Ok(None) => { + log::error!( + "{}({}) :: Can't find parent zone :: None value", + file!(), + line!() + ); + return Err(Box::from("None value")); + } + Err(e) => return Err(e), + }; + + if zone.id == zone.parent { + return Ok(zone); + } + } + } + /// Save a zone to the database. pub fn save_zone(&self, zone: &Zone) -> RudeResult<()> { let mut statement = try_log!(