summaryrefslogtreecommitdiff
path: root/src/worldloader.rs
blob: 8934fc80f0f18788948814ba504f68f1bb6f8aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

use std::path::{PathBuf};
use std::fs;
use serde_json;
use serde_json::Value;
use crate::{
	RoomId,
	roomtemplate::RoomTemplate,
	Result,
	aerr,
	Encyclopedia
};


pub struct WorldLoader {
	pub directory: PathBuf
}

impl WorldLoader {
	pub fn new(path: PathBuf) -> Self {
		Self {
			directory: path
		}
	}
	
	pub fn load_world_meta(&self) -> Result<WorldMeta> {
		let path = self.directory.join("world.json");
		let text = fs::read_to_string(path)?;
		let json: Value = serde_json::from_str(&text)?;
		let default_room = RoomId::from_str(
			json
				.get("default_room")
				.ok_or(aerr!("world meta does not have default_room"))?
				.as_str()
				.ok_or(aerr!("world meta default_room is not a string"))?
		);
		let encyclopediae = 
			json
				.get("encyclopediae")
				.ok_or(aerr!("world meta does not have encyclopediae"))?
				.as_array()
				.ok_or(aerr!("world meta encyclopediae is not a list"))?
				.iter()
				.map(|v| Ok(v
					.as_str()
					.ok_or(aerr!("world meta encyclopediae item {:?} is not a string", v))?
					.to_string()
				))
				.collect::<Result<Vec<String>>>()?;
		Ok(WorldMeta{
			default_room,
			encyclopediae
		})
	}
	
	pub fn load_room(&self, id: RoomId) -> Result<RoomTemplate> {
		let fname = id.name.splitn(2, '+').next().unwrap().to_string() + ".json";
		let path = self.directory.join("maps").join(fname);
		let text = fs::read_to_string(path)?;
		let template = serde_json::from_str(&text)?;
		Ok(template)
	}
	
	pub fn load_encyclopedia(&self, name: &str) -> Result<Encyclopedia> {
		let fname: String = name.to_string() + ".json";
		let encyclopedia = Encyclopedia::from_json(
			serde_json::from_str(
				&fs::read_to_string(
					self.directory
						.join("encyclopediae")
						.join(&fname)
				)?
			)?
		)?;
		encyclopedia.validate()?;
		Ok(encyclopedia)
	}
}

pub struct WorldMeta {
	pub encyclopediae: Vec<String>,
	pub default_room: RoomId
}