2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::Script;
|
|
|
|
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{PkmnResult, StringKey};
|
2022-06-06 11:54:59 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
pub trait VolatileScripts<'a> {
|
|
|
|
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>>;
|
2022-06-11 15:22:46 +00:00
|
|
|
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>>;
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
fn has_volatile_script(&self, key: &StringKey) -> bool {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.volatile_scripts().read().unwrap().has(key)
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
fn get_volatile_script(&self, key: &StringKey) -> Option<Arc<Box<dyn Script>>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
let scripts = self.volatile_scripts().read().unwrap();
|
|
|
|
let s = scripts.get(key);
|
|
|
|
s.cloned()
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<Arc<Box<dyn Script>>>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.volatile_scripts()
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.stack_or_add(key, &|| self.load_volatile_script(key))
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
fn remove_volatile_script(&mut self, key: &StringKey) {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.volatile_scripts().write().unwrap().remove(key)
|
|
|
|
}
|
|
|
|
}
|