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

4 years ago
4 years ago
4 years ago
4 years ago
  1. mod weapon;
  2. mod tool;
  3. mod armor;
  4. use async_std::sync::{Arc, Mutex};
  5. use futures::future::OptionFuture;
  6. use std::collections::HashMap;
  7. use entity::item::ItemDetail;
  8. use maps::room::Difficulty;
  9. use entity::character::SectionID;
  10. pub use weapon::{WeaponShop, WeaponShopItem};
  11. pub use tool::{ToolShop, ToolShopItem};
  12. pub use armor::{ArmorShop, ArmorShopItem};
  13. pub trait ShopItem {
  14. fn price(&self) -> usize;
  15. fn as_bytes(&self) -> [u8; 12];
  16. fn as_item(&self) -> ItemDetail;
  17. }
  18. #[async_trait::async_trait]
  19. pub trait ItemShops {
  20. async fn generate_weapon_list(&self, difficulty: Difficulty, section_id: SectionID, char_level: usize) -> Option<Vec<WeaponShopItem>>;
  21. async fn generate_tool_list(&self, char_level: usize) -> Vec<ToolShopItem>;
  22. async fn generate_armor_list(&self, char_level: usize) -> Vec<ArmorShopItem>;
  23. }
  24. #[derive(Clone)]
  25. pub struct StandardItemShops {
  26. weapon_shop: HashMap<(Difficulty, SectionID), Arc<Mutex<WeaponShop<rand_chacha::ChaCha20Rng>>>>,
  27. tool_shop: Arc<Mutex<ToolShop<rand_chacha::ChaCha20Rng>>>,
  28. armor_shop: Arc<Mutex<ArmorShop<rand_chacha::ChaCha20Rng>>>,
  29. }
  30. impl Default for StandardItemShops {
  31. fn default() -> StandardItemShops {
  32. let difficulty = [Difficulty::Normal, Difficulty::Hard, Difficulty::VeryHard, Difficulty::Ultimate];
  33. let section_id = [SectionID::Viridia, SectionID::Greenill, SectionID::Skyly, SectionID::Bluefull, SectionID::Purplenum,
  34. SectionID::Pinkal, SectionID::Redria, SectionID::Oran, SectionID::Yellowboze, SectionID::Whitill];
  35. let mut weapon_shop = HashMap::new();
  36. for d in difficulty.iter() {
  37. for id in section_id.iter() {
  38. weapon_shop.insert((*d, *id), Arc::new(Mutex::new(WeaponShop::new(*d, *id))));
  39. }
  40. }
  41. StandardItemShops {
  42. weapon_shop,
  43. tool_shop: Arc::new(Mutex::new(ToolShop::default())),
  44. armor_shop: Arc::new(Mutex::new(ArmorShop::default())),
  45. }
  46. }
  47. }
  48. #[async_trait::async_trait]
  49. impl ItemShops for StandardItemShops {
  50. async fn generate_weapon_list(&self, difficulty: Difficulty, section_id: SectionID, char_level: usize) -> Option<Vec<WeaponShopItem>> {
  51. OptionFuture::from(
  52. self.weapon_shop
  53. .get(&(difficulty, section_id))
  54. .map(|shop| async {
  55. shop
  56. .lock()
  57. .await
  58. .generate_weapon_list(char_level)
  59. })).await
  60. }
  61. async fn generate_tool_list(&self, char_level: usize) -> Vec<ToolShopItem> {
  62. self.tool_shop
  63. .lock()
  64. .await
  65. .generate_tool_list(char_level)
  66. }
  67. async fn generate_armor_list(&self, char_level: usize) -> Vec<ArmorShopItem> {
  68. self.armor_shop
  69. .lock()
  70. .await
  71. .generate_armor_list(char_level)
  72. }
  73. }
  74. pub enum ShopType {
  75. Weapon,
  76. Tool,
  77. Armor
  78. }