summaryrefslogtreecommitdiff
path: root/src/worldloader.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-20 23:18:44 +0100
committertroido <troido@protonmail.com>2020-02-20 23:18:44 +0100
commitd225dc6349670926a4adea932f0ea77b7af5acbc (patch)
treec70d5c5b2f9d791cf3b9855733e171092497a2e0 /src/worldloader.rs
parent933e831a82725e8bf22788d6a9f1fba2c596a975 (diff)
load room template from file
Diffstat (limited to 'src/worldloader.rs')
-rw-r--r--src/worldloader.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/worldloader.rs b/src/worldloader.rs
new file mode 100644
index 0000000..6660f9d
--- /dev/null
+++ b/src/worldloader.rs
@@ -0,0 +1,36 @@
+
+use std::path::PathBuf;
+use std::fs;
+use serde_json;
+use serde_json::Value;
+use crate::{
+ RoomId,
+ roomtemplate::RoomTemplate,
+ util::Result
+};
+
+
+pub struct WorldLoader {
+ pub directory: PathBuf,
+ pub default_room: RoomId
+}
+
+impl WorldLoader {
+ pub fn new(path: PathBuf, default_room: RoomId) -> Self {
+ Self {
+ directory: path,
+ default_room
+ }
+ }
+
+ pub fn load_room(&self, id: RoomId) -> Result<RoomTemplate> {
+ let mut path = self.directory.clone();
+ let fname = id.to_string() + ".json";
+ path.push(fname);
+ println!("PATH: {:?}", path);
+ let text = fs::read_to_string(path)?;
+ let json: Value = serde_json::from_str(&text)?;
+ let template = RoomTemplate::from_json(&json)?;
+ Ok(template)
+ }
+}