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

use std::collections::HashMap;
use serde_json;
use serde_json::{Value};
use specs::{
	Component,
	HashMapStorage
};
use crate::{
	exchange::Exchange,
	ItemId,
	components::{Trigger, equipment::Stat},
	RoomId
};

#[derive(Component, Debug, Clone, PartialEq)]
#[storage(HashMapStorage)]
pub enum Interactable {
	Trigger(Trigger),
	Exchange(String, HashMap<String, Exchange>),
	Visit(RoomId),
	Mine(Stat)
}

use Interactable::*;

impl Interactable {
	pub fn from_json(val: &Value) -> Option<Self> {
		let typ = val.get(0)?;
		let arg = val.get(1)?;
		Some(match typ.as_str()? {
			"trigger" => Trigger(Trigger::from_str(arg.as_str()?)?),
			"exchange" => {
				let (prefix, change) = serde_json::value::from_value::<
						(String, HashMap<String, (Vec<ItemId>, Vec<ItemId>)>)
					>(arg.clone()).ok()?;
				Exchange(
					prefix,
					change.into_iter().map(
						|(id, (cost, offer))| (id, Exchange{cost, offer})
					).collect::<HashMap<String, Exchange>>()
				)
			},
			"visit" => Visit(RoomId::from_str(arg.as_str()?)),
			"mine" => Mine(Stat::from_str(arg.as_str()?)?),
			_ => None?
		})
	}
	
	pub fn accepts_arg(&self, arg: &Option<String>) -> bool {
		match self {
			Trigger(_) => arg.is_none(),
			Exchange(prefix, _exchanges) => {
				if let Some(txt) = arg {
					 txt.starts_with(prefix)
				} else {
					true
				}
			},
			Visit(_) => {
				if let Some(txt) = arg {
					 txt.starts_with("visit ") || txt.starts_with("disallow ") || txt.starts_with("allow ") || txt.starts_with("whitelist")
				} else {
					true
				}
			}
			Mine(_) => arg.is_none()
		}
	}
}

#[derive(Component, Debug, Clone, PartialEq)]
#[storage(HashMapStorage)]
pub struct Talkable {
	pub text: String
}