2020-03-25 22:14:26 +01:00
|
|
|
use async_std::prelude::*;
|
|
|
|
use async_std::fs;
|
|
|
|
|
2020-03-27 15:27:19 +01:00
|
|
|
static NAV: [(&'static str, &'static str); 4] = [("/", "Home"), ("/lobby", "Lobby"), ("/mapbuilder", "Map Builder"), ("/frontend/index.html", "Visualizer")];
|
2020-03-27 10:31:56 +01:00
|
|
|
|
2020-03-25 22:14:26 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct Map {
|
|
|
|
name: String,
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:31:56 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Link {
|
|
|
|
name: String,
|
|
|
|
href: String,
|
|
|
|
active: bool,
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:14:26 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct Context {
|
|
|
|
pub name: String,
|
2020-03-27 10:31:56 +01:00
|
|
|
nav: Vec<Link>,
|
2020-03-25 22:14:26 +01:00
|
|
|
pub maps: Option<Vec<Map>>,
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:31:56 +01:00
|
|
|
impl Context {
|
|
|
|
pub fn new(active: &str, maps: Option<Vec<Map>>) -> Self {
|
|
|
|
let nav = NAV.iter().map(|(href, name)| Link { name: name.to_string(), href: href.to_string(), active: *name == active }).collect();
|
|
|
|
|
|
|
|
Context {
|
|
|
|
nav, name: String::from(""), maps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:14:26 +01:00
|
|
|
pub async fn get_maps() -> Result<Vec<Map>, String> {
|
|
|
|
let mut maps = Vec::new();
|
|
|
|
let mut entries = fs::read_dir("maps").await.map_err(|_| "IO error".to_string())?;
|
|
|
|
while let Some(file) = entries.next().await {
|
|
|
|
let file = file.map_err(|_| "IO error".to_string())?.path();
|
|
|
|
if let Some(stem) = file.file_stem().and_then(|x| x.to_str()) {
|
|
|
|
maps.push(Map { name: stem.to_string(), url: file.to_str().unwrap().to_string() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(maps)
|
|
|
|
}
|