You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.9 KiB
95 lines
2.9 KiB
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<Vec<WeaponShopItem>>;
|
|
async fn generate_tool_list(&self, char_level: usize) -> Vec<ToolShopItem>;
|
|
async fn generate_armor_list(&self, char_level: usize) -> Vec<ArmorShopItem>;
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
pub struct StandardItemShops {
|
|
weapon_shop: HashMap<(Difficulty, SectionID), Arc<Mutex<WeaponShop<rand_chacha::ChaCha20Rng>>>>,
|
|
tool_shop: Arc<Mutex<ToolShop<rand_chacha::ChaCha20Rng>>>,
|
|
armor_shop: Arc<Mutex<ArmorShop<rand_chacha::ChaCha20Rng>>>,
|
|
}
|
|
|
|
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<Vec<WeaponShopItem>> {
|
|
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<ToolShopItem> {
|
|
self.tool_shop
|
|
.lock()
|
|
.await
|
|
.generate_tool_list(char_level)
|
|
}
|
|
|
|
async fn generate_armor_list(&self, char_level: usize) -> Vec<ArmorShopItem> {
|
|
self.armor_shop
|
|
.lock()
|
|
.await
|
|
.generate_armor_list(char_level)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum ShopType {
|
|
Weapon,
|
|
Tool,
|
|
Armor
|
|
}
|
|
|