use crate::astar::AStar;
use crate::errors::my_errors::{MyError, RetResult};
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use chrono::Utc;
use crate::map::map::Map;
use crate::map::map::MapType;
use crate::map::IndexType;
pub struct MapManager {
map_collections: HashMap<i64, MapType>,
}
impl MapManager {
pub fn get_instance() -> Arc<RwLock<MapManager>> {
Arc::clone(&MAP_MANAGER)
}
fn new() -> Arc<RwLock<MapManager>> {
Arc::new(RwLock::new(MapManager {
map_collections: HashMap::new(),
}))
}
pub async fn new_astar(&mut self) -> i64 {
let map_id = Utc::now().timestamp_millis();
self.map_collections.insert(map_id, AStar::new());
map_id
}
pub async fn remove_map(&mut self, map_id: &i64) {
self.map_collections.remove(map_id);
}
pub fn load(&self, map_id: &i64, points: Vec<Vec<i32>>) -> RetResult<()> {
let res = self.map_collections.get(map_id);
match res {
None => Err(MyError::MapNotExist(map_id.clone()).into()),
Some(m) => m.clone().write().map_or_else(
|e| Err(MyError::UnknownErr(e.to_string()).into()),
|mut v| v.load(points),
),
}
}
pub async fn load_from_file(&self, map_id: &i64, file: String) -> RetResult<()> {
if let Some(m) = self.map_collections.get(&map_id) {
return match m.clone().write() {
Ok(mut x) => Ok(x.load_from_file(file).await?),
Err(e) => Err(MyError::UnknownErr(e.to_string()).into()),
};
}
Err(MyError::MapNotExist(map_id.clone()).into())
}
pub async fn load_from_string(&self, map_id: &i64, file: String) -> RetResult<()> {
if let Some(m) = self.map_collections.get(&map_id) {
return match m.clone().write() {
Ok(mut x) => Ok(x.load_from_string(file).await?),
Err(e) => Err(MyError::UnknownErr(e.to_string()).into()),
};
}
Err(MyError::MapNotExist(map_id.clone()).into())
}
pub fn set_walkable(
&self,
map_id: &i64,
point: (IndexType, IndexType),
walkable: i32,
) -> RetResult<()> {
let res = self.map_collections.get(map_id);
match res {
None => Err(MyError::MapNotExist(map_id.clone()).into()),
Some(m) => match m.clone().write() {
Ok(mut v) => Ok(v.set_walkable(point, walkable)),
Err(e) => Err(MyError::UnknownErr(e.to_string()).into()),
},
}
}
pub fn find_path(
&self,
map_id: &i64,
start: (IndexType, IndexType),
end: (IndexType, IndexType),
) -> RetResult<Vec<(IndexType, IndexType)>> {
let res = self.map_collections.get(map_id);
match res {
None => Err(MyError::MapNotExist(map_id.clone()).into()),
Some(m) => match m.clone().read() {
Ok(v) => Ok(v.find_path(start, end)),
Err(e) => Err(MyError::UnknownErr(e.to_string()).into()),
},
}
}
}
lazy_static! {
static ref MAP_MANAGER: Arc<RwLock<MapManager>> = MapManager::new();
}