diff options
| author | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
| commit | 5ce9b012a7987f4085057f4f0b0af35e76810a7a (patch) | |
| tree | a99418b5aec06d6be28e22150bce85d3c40b8ee7 /src/components/interactable.rs | |
| parent | b6a4c7b2d383755402e5e2c6f60d9a75a899b809 (diff) | |
| parent | 455867294cc849bff2c0829a7464e71e79a0dcae (diff) | |
Merge branch 'interact' into master
Diffstat (limited to 'src/components/interactable.rs')
| -rw-r--r-- | src/components/interactable.rs | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/src/components/interactable.rs b/src/components/interactable.rs index d97b742..2f20966 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -1,59 +1,60 @@ use std::collections::HashMap; -use serde_json; -use serde_json::{Value}; use specs::{ Component, - HashMapStorage + HashMapStorage, }; use crate::{ exchange::Exchange, - ItemId, components::{Trigger, equipment::Stat}, - RoomId + RoomId, + parameter::Parameter, + fromtoparameter::FromToParameter, + ItemId, }; #[derive(Component, Debug, Clone, PartialEq)] #[storage(HashMapStorage)] pub enum Interactable { Trigger(Trigger), + Visit(RoomId), + Mine(Stat), Say(String), Reply(String), 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()?)?), - "say" => Say(arg.as_str()?.to_string()), - "reply" => Reply(arg.as_str()?.to_string()), - "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 parse_from_parameter(typ: &str, arg: &Parameter) -> Option<Self> { + Some(match (typ, arg) { + ("trigger", Parameter::String(s)) => Trigger(Trigger::from_str(s)?), + ("visit", Parameter::String(s)) => Visit(RoomId::from_str(s)), + ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?), + ("say", Parameter::String(s)) => Say(s.clone()), + ("reply", Parameter::String(s)) => Reply(s.clone()), + ("exchange", p) => { + let (prefix, trades) = <(String, Vec<(String, Vec<ItemId>, Vec<ItemId>)>)>::from_parameter(p.clone())?; + let exchanges = trades.into_iter().map(|(k, cost, offer)| (k, Exchange{cost, offer})).collect(); + Exchange(prefix, exchanges) + } + _ => {return None} }) } pub fn accepts_arg(&self, arg: &Option<String>) -> bool { match self { Trigger(_) => arg.is_none(), + 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(), Say(_) => arg.is_none(), Reply(_) => arg.is_some(), Exchange(prefix, _exchanges) => { @@ -63,14 +64,8 @@ impl Interactable { 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() } } } + + |
