2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{PkmnResult, StringKey};
|
2022-06-12 15:57:39 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::sync::Arc;
|
2022-06-06 11:54:59 +00:00
|
|
|
|
|
|
|
pub trait VolatileScripts<'a> {
|
|
|
|
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>>;
|
2022-06-18 16:41:23 +00:00
|
|
|
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Arc<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-12 15:57:39 +00:00
|
|
|
self.volatile_scripts().read().has(key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
fn get_volatile_script(&self, key: &StringKey) -> Option<ScriptContainer> {
|
|
|
|
let scripts = self.volatile_scripts().read();
|
2022-06-06 11:54:59 +00:00
|
|
|
let s = scripts.get(key);
|
|
|
|
s.cloned()
|
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
fn add_volatile_script(&mut self, key: &StringKey) -> PkmnResult<Option<ScriptContainer>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.volatile_scripts()
|
|
|
|
.write()
|
|
|
|
.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-12 15:57:39 +00:00
|
|
|
self.volatile_scripts().write().remove(key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
}
|