summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/components')
-rw-r--r--src/components/interactable.rs26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/components/interactable.rs b/src/components/interactable.rs
index 757e27e..d97b742 100644
--- a/src/components/interactable.rs
+++ b/src/components/interactable.rs
@@ -1,5 +1,6 @@
use std::collections::HashMap;
+use serde_json;
use serde_json::{Value};
use specs::{
Component,
@@ -33,20 +34,17 @@ impl Interactable {
"trigger" => Trigger(Trigger::from_str(arg.as_str()?)?),
"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>>>()?
- ),
+ "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?