mod weapon; mod tool; mod armor; use async_std::sync::{Arc, Mutex}; use futures::future::OptionFuture; use std::collections::HashMap; use entity::item::ItemDetail; use maps::room::Difficulty; use entity::character::SectionID; pub use weapon::{WeaponShop, WeaponShopItem}; pub use tool::{ToolShop, ToolShopItem}; pub use armor::{ArmorShop, ArmorShopItem}; pub trait ShopItem { fn price(&self) -> usize; fn as_bytes(&self) -> [u8; 12]; fn as_item(&self) -> ItemDetail; } #[async_trait::async_trait] pub trait ItemShops { async fn generate_weapon_list(&self, difficulty: Difficulty, section_id: SectionID, char_level: usize) -> Option>; async fn generate_tool_list(&self, char_level: usize) -> Vec; async fn generate_armor_list(&self, char_level: usize) -> Vec; } #[derive(Clone)] pub struct StandardItemShops { weapon_shop: HashMap<(Difficulty, SectionID), Arc>>>, tool_shop: Arc>>, armor_shop: Arc>>, } impl Default for StandardItemShops { fn default() -> StandardItemShops { let difficulty = [Difficulty::Normal, Difficulty::Hard, Difficulty::VeryHard, Difficulty::Ultimate]; let section_id = [SectionID::Viridia, SectionID::Greenill, SectionID::Skyly, SectionID::Bluefull, SectionID::Purplenum, SectionID::Pinkal, SectionID::Redria, SectionID::Oran, SectionID::Yellowboze, SectionID::Whitill]; let mut weapon_shop = HashMap::new(); for d in difficulty.iter() { for id in section_id.iter() { weapon_shop.insert((*d, *id), Arc::new(Mutex::new(WeaponShop::new(*d, *id)))); } } StandardItemShops { weapon_shop, tool_shop: Arc::new(Mutex::new(ToolShop::default())), armor_shop: Arc::new(Mutex::new(ArmorShop::default())), } } } #[async_trait::async_trait] impl ItemShops for StandardItemShops { async fn generate_weapon_list(&self, difficulty: Difficulty, section_id: SectionID, char_level: usize) -> Option> { OptionFuture::from( self.weapon_shop .get(&(difficulty, section_id)) .map(|shop| async { shop .lock() .await .generate_weapon_list(char_level) })).await } async fn generate_tool_list(&self, char_level: usize) -> Vec { self.tool_shop .lock() .await .generate_tool_list(char_level) } async fn generate_armor_list(&self, char_level: usize) -> Vec { self.armor_shop .lock() .await .generate_armor_list(char_level) } } pub enum ShopType { Weapon, Tool, Armor }