planet-wars/backend/src/routes/mod.rs

94 lines
2.4 KiB
Rust
Raw Normal View History

2020-04-09 22:57:12 +02:00
use crate::planetwars::FinishedState;
2020-04-09 23:03:36 +02:00
use crate::util::*;
2020-04-09 22:57:12 +02:00
use rocket::response::NamedFile;
2020-04-09 23:03:36 +02:00
use rocket::Route;
2020-04-09 22:57:12 +02:00
use rocket_contrib::templates::Template;
use async_std::fs;
2020-04-09 23:03:36 +02:00
use async_std::io::BufReader;
use async_std::prelude::*;
2020-04-09 22:57:12 +02:00
use futures::stream::StreamExt;
use std::path::{Path, PathBuf};
2020-04-09 23:03:36 +02:00
mod info;
2020-04-09 22:57:12 +02:00
mod lobby;
mod maps;
/// Handles all files located in the static folder
#[get("/<file..>", rank = 6)]
async fn files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(file)).ok()
}
/// Routes the index page, rendering the index Template.
#[get("/")]
async fn index() -> Template {
let context = Context::new("Home");
Template::render("index", &context)
}
/// Routes the mapbuilder page, rendering the mapbuilder Template.
#[get("/mapbuilder")]
async fn builder_get() -> Result<Template, String> {
let context = Context::new("Map Builder");
Ok(Template::render("mapbuilder", &context))
}
/// Routes the debug page, rendering the debug Template.
#[get("/debug")]
async fn debug_get() -> Result<Template, String> {
let context = Context::new("Debug Station");
Ok(Template::render("debug", &context))
}
/// Routes the visualizer page, rendering the visualizer Template.
#[get("/visualizer")]
async fn visualizer_get() -> Template {
let mut game_options: Vec<GameState> = get_played_games().await;
game_options.sort();
2020-04-09 23:03:36 +02:00
let context = Context::new_with(
"Visualizer",
json!({"games": game_options, "colours": COLOURS}),
);
2020-04-09 22:57:12 +02:00
Template::render("visualizer", &context)
}
/// Fuels all routes
pub fn fuel(routes: &mut Vec<Route>) {
2020-04-09 23:03:36 +02:00
routes.extend(routes![
files,
index,
builder_get,
visualizer_get,
debug_get
]);
2020-04-09 22:57:12 +02:00
lobby::fuel(routes);
maps::fuel(routes);
info::fuel(routes);
}
/// Reads games.ini
/// File that represents all played games
/// Ready to be visualized
async fn get_played_games() -> Vec<GameState> {
match fs::File::open("games.ini").await {
Ok(file) => {
let file = BufReader::new(file);
file.lines()
.filter_map(move |maybe| async {
maybe
.ok()
.and_then(|line| serde_json::from_str::<FinishedState>(&line).ok())
})
.map(|state| state.into())
.collect()
.await
}
Err(_) => Vec::new(),
}
}