2022-08-20 11:17:20 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
use indexmap::IndexMap;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2022-06-11 18:51:37 +00:00
|
|
|
use crate::static_data::DataLibrary;
|
|
|
|
use crate::static_data::Item;
|
2022-10-08 11:15:04 +00:00
|
|
|
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A library to store all items.
|
2021-01-30 21:29:59 +00:00
|
|
|
#[derive(Debug)]
|
2022-11-27 16:29:29 +00:00
|
|
|
|
2021-01-30 21:29:59 +00:00
|
|
|
pub struct ItemLibrary {
|
2022-10-08 11:15:04 +00:00
|
|
|
/// A unique identifier so we know what value this is.
|
|
|
|
identifier: ValueIdentifier,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The underlying data structure.
|
2022-11-27 16:29:29 +00:00
|
|
|
map: IndexMap<StringKey, Arc<dyn Item>>,
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemLibrary {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Instantiates a new Item Library.
|
2021-01-30 21:29:59 +00:00
|
|
|
pub fn new(capacity: usize) -> ItemLibrary {
|
|
|
|
ItemLibrary {
|
2022-10-08 11:15:04 +00:00
|
|
|
identifier: Default::default(),
|
2022-07-01 15:07:22 +00:00
|
|
|
map: IndexMap::with_capacity(capacity),
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:29:29 +00:00
|
|
|
impl DataLibrary<dyn Item> for ItemLibrary {
|
|
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Item>> {
|
2021-01-30 21:29:59 +00:00
|
|
|
&self.map
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:29:29 +00:00
|
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Item>> {
|
2022-07-01 15:07:22 +00:00
|
|
|
&mut self.map
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-10-08 11:15:04 +00:00
|
|
|
impl ValueIdentifiable for ItemLibrary {
|
|
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
|
|
self.identifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2022-06-19 19:34:08 +00:00
|
|
|
use hashbrown::HashSet;
|
2022-10-08 11:15:04 +00:00
|
|
|
use std::sync::Arc;
|
2022-06-19 19:34:08 +00:00
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::static_data::libraries::data_library::DataLibrary;
|
|
|
|
use crate::static_data::libraries::item_library::ItemLibrary;
|
2022-11-27 16:29:29 +00:00
|
|
|
use crate::static_data::ItemImpl;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::static_data::{BattleItemCategory, ItemCategory};
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-11-27 16:29:29 +00:00
|
|
|
fn build_item() -> ItemImpl {
|
|
|
|
ItemImpl::new(
|
2022-06-11 15:22:46 +00:00
|
|
|
&"foo".into(),
|
2021-01-31 16:31:22 +00:00
|
|
|
ItemCategory::MiscItem,
|
|
|
|
BattleItemCategory::MiscBattleItem,
|
|
|
|
100,
|
|
|
|
HashSet::new(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build() -> ItemLibrary {
|
|
|
|
let mut lib = ItemLibrary::new(1);
|
|
|
|
let m = build_item();
|
|
|
|
// Borrow as mut so we can insert
|
|
|
|
let w = &mut lib;
|
2022-10-08 11:15:04 +00:00
|
|
|
w.add(&"foo".into(), Arc::new(m));
|
2021-01-31 16:31:22 +00:00
|
|
|
// Drops borrow as mut
|
|
|
|
|
|
|
|
lib
|
|
|
|
}
|
|
|
|
}
|