use crate::{Object, ObjectLocation, Plane};
use std::{collections::BTreeMap, sync::Arc};
#[derive(Default)]
pub(crate) struct Claims(BTreeMap<String, Arc<str>>);
impl Claims {
pub fn claim(&mut self, uid: &str, path: &Arc<str>) -> Option<Arc<str>> {
if let Some(first) = self.0.get(uid) {
return Some(Arc::clone(first));
}
self.0.insert(uid.to_owned(), Arc::clone(path));
None
}
}
pub(crate) fn index(planes: &BTreeMap<String, Plane>) -> BTreeMap<String, ObjectLocation> {
let mut index = BTreeMap::new();
let mut path = Vec::new();
for (id, plane) in planes {
for (key, chunk) in &plane.chunks {
for (position, object) in chunk.objects.iter().enumerate() {
path.push(position);
add(&mut index, id, *key, object, &mut path);
path.pop();
}
}
}
index
}
fn add(
index: &mut BTreeMap<String, ObjectLocation>,
plane: &str,
chunk: (i32, i64, i64),
object: &Object,
path: &mut Vec<usize>,
) {
if let Some(uid) = &object.uid {
index.entry(uid.clone()).or_insert_with(|| ObjectLocation {
plane: plane.to_owned(),
chunk,
path: path.clone(),
});
}
for (position, item) in object.inventory.iter().enumerate() {
path.push(position);
add(index, plane, chunk, item, path);
path.pop();
}
}