Further massive amounts of work

This commit is contained in:
2022-06-06 13:54:59 +02:00
parent df662ce6b5
commit ce33ec0649
33 changed files with 848 additions and 80 deletions

View File

@@ -1,14 +1,69 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::PkmnResult;
use indexmap::IndexMap;
use std::sync::Arc;
#[derive(Debug)]
pub struct ScriptSet {}
#[derive(Debug, Default)]
pub struct ScriptSet {
scripts: IndexMap<String, Arc<Box<dyn Script>>>,
}
impl ScriptSet {
pub fn count(&self) -> usize {
todo!()
pub fn add(&mut self, script: Box<dyn Script>) -> Arc<Box<dyn Script>> {
if let Some(existing) = self.scripts.get(script.name()) {
existing.stack();
return existing.clone();
}
let arc = Arc::new(script);
self.scripts.insert(arc.name().to_string(), arc.clone());
self.scripts.last().unwrap().1.clone()
}
pub fn at(&self, _index: usize) -> &Box<dyn Script> {
todo!()
pub fn stack_or_add<'b, F>(
&mut self,
key: &str,
instantiation: &'b F,
) -> PkmnResult<Arc<Box<dyn Script>>>
where
F: Fn() -> PkmnResult<Box<dyn Script>>,
{
if let Some(existing) = self.scripts.get(key) {
existing.stack();
return Ok(existing.clone());
}
let script = instantiation()?;
let arc = Arc::new(script);
self.scripts.insert(arc.name().to_string(), arc.clone());
Ok(self.scripts.last().unwrap().1.clone())
}
pub fn get(&self, key: &str) -> Option<&Arc<Box<dyn Script>>> {
self.scripts.get(key)
}
pub fn remove(&mut self, key: &str) {
let value = self.scripts.shift_remove(key);
if let Some(script) = value {
script.on_remove();
}
}
pub fn clear(&mut self) {
for script in &self.scripts {
script.1.on_remove();
}
self.scripts.clear();
}
pub fn has(&self, key: &str) -> bool {
self.scripts.contains_key(key)
}
pub fn at(&self, index: usize) -> &Arc<Box<dyn Script>> {
&self.scripts[index]
}
pub fn count(&self) -> usize {
self.scripts.len()
}
}