use galactic_war::{Coords, Details, Galaxy, Resources, SystemInfo, SystemProduction};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
lazy_static::lazy_static! {
pub static ref GALAXIES: Arc<Mutex<HashMap<String, Galaxy>>> = Arc::new(Mutex::new(HashMap::new()));
}
pub fn tick() -> usize {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as usize
}
pub fn system_info(galaxy: &str, coords: Coords) -> Result<SystemInfo, String> {
let mut galaxies = GALAXIES.lock().unwrap();
if let Some(galaxy) = galaxies.get_mut(galaxy) {
let dets = galaxy.get_details(tick(), coords, None);
if let Ok(dets) = dets {
match dets {
Details::System(info) => Ok(info),
_ => Err("Unexpected Details type".to_string()),
}
} else {
Err(dets.unwrap_err())
}
} else {
Err("Galaxy not found".to_string())
}
}
pub fn resource_table(resources: &Resources, production: &SystemProduction) -> String {
format!("<table width=600 border=1 cellspacing=0 cellpadding=3><tr><td>💰 {}</td><td>🧑 {}</td><td>💧 {}</td><td>🏃 {}/{}/{}</td></tr></table>",
resources.metal, resources.crew, resources.water, production.metal, production.crew, production.water)
}