From ff457701ff56072914acb8a7160cd02c2a07095a Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 5 Apr 2020 23:22:36 +0200 Subject: trading now works --- src/components/interactable.rs | 29 +++++++++++++++++++++++++++-- src/components/inventory.rs | 9 +++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src/components') diff --git a/src/components/interactable.rs b/src/components/interactable.rs index a59cc90..33e3a12 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -1,11 +1,14 @@ +use std::collections::HashMap; use serde_json::{Value}; use specs::{ Component, HashMapStorage }; use crate::{ - Template + Template, + exchange::Exchange, + ItemId }; #[derive(Component, Debug, Clone, PartialEq)] @@ -14,7 +17,8 @@ pub enum Interactable { Harvest, Change(Template), Say(String), - Reply(String) + Reply(String), + Exchange(String, HashMap) } use Interactable::*; @@ -28,6 +32,20 @@ impl Interactable { "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::>>()?, + offer: ex.get(1)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::>>()? + }; + Some((id.clone(), exchange)) + }) + .collect::>>()? + ), _ => None? }) } @@ -38,6 +56,13 @@ impl Interactable { 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 + } + } } } } diff --git a/src/components/inventory.rs b/src/components/inventory.rs index c3282e9..fa65b03 100644 --- a/src/components/inventory.rs +++ b/src/components/inventory.rs @@ -5,6 +5,7 @@ use crate::{ ItemId, item::{Item, ItemAction}, components::equipment::{Stat, Equippable}, + Encyclopedia }; #[derive(Debug, Clone)] @@ -25,6 +26,14 @@ impl Component for Inventory { impl Inventory { + pub fn add_item(&mut self, itemid: ItemId, enc: &Encyclopedia) { + self.items.insert(0, InventoryEntry{ + itemid: itemid.clone(), + item: enc.get_item(&itemid).unwrap(), + is_equipped: false + }); + } + fn equipped(&self) -> Vec { let mut equippables = Vec::new(); for entry in self.items.iter() { -- cgit