summaryrefslogtreecommitdiff
path: root/src/components/interactable.rs
blob: 33e3a12911f732744b19a047c5c382f4521d6508 (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

use std::collections::HashMap;
use serde_json::{Value};
use specs::{
	Component,
	HashMapStorage
};
use crate::{
	Template,
	exchange::Exchange,
	ItemId
};

#[derive(Component, Debug, Clone, PartialEq)]
#[storage(HashMapStorage)]
pub enum Interactable {
	Harvest,
	Change(Template),
	Say(String),
	Reply(String),
	Exchange(String, HashMap<String, Exchange>)
}

use Interactable::*;

impl Interactable {
	pub fn from_json(val: &Value) -> Option<Self> {
		let typ = if val.is_string() {val} else {val.get(0)?};
		let arg = if val.is_string() {&Value::Null} else {val.get(1)?};
		Some(match typ.as_str()? {
			"harvest" => Harvest,
			"change" => Change(Template::from_json(arg).ok()?),
			"say" => Say(arg.as_str()?.to_string()),
			"reply" => Reply(arg.as_str()?.to_string()),
			"exchange" => Exchange(
				arg.get(0)?.as_str()?.to_string(),
				arg.get(1)?
					.as_object()?
					.iter()
					.map(|(id, ex)| {
						let exchange = Exchange {
							cost: ex.get(0)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()?,
							offer: ex.get(1)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()?
						};
						Some((id.clone(), exchange)) 
					})
					.collect::<Option<HashMap<String, Exchange>>>()?
			),
			_ => None?
		})
	}
	
	pub fn accepts_arg(&self, arg: &Option<String>) -> bool {
		match self {
			Harvest => arg.is_none(),
			Change(_) => arg.is_none(),
			Say(_) => arg.is_none(),
			Reply(_) => arg.is_some(),
			Exchange(prefix, _exchanges) => {
				if let Some(txt) = arg {
					 txt.starts_with(prefix)
				} else {
					true
				}
			}
		}
	}
}