From eb8f4d514f90a6c52ee66cde1de3a5d8bc962f2d Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Wed, 1 Apr 2020 23:03:18 +0200 Subject: [PATCH] Display map, most of the time --- backend/src/main.rs | 5 +--- backend/src/planetwars/mod.rs | 10 +++++--- backend/src/routes.rs | 2 +- backend/src/util.rs | 30 ++++++----------------- backend/templates/state_partial.html.tera | 2 +- 5 files changed, 17 insertions(+), 32 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 4937b4c..2c874ba 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -37,14 +37,13 @@ mod planetwars; mod routes; mod util; use util::Games; +use util::COLOURS; use rocket_contrib::templates::{Template, Engines}; use rocket_contrib::templates::tera::{self, Value}; use std::collections::HashMap; -const COLOURS: [&'static str; 9] = ["grey", "blue", "cyan", "green", "yellow", "orange", "red", "pink", "purple"]; - fn calc_viewbox(value: Value, _: HashMap) -> tera::Result { let mut min_x = std::f64::MAX; let mut min_y = std::f64::MAX; @@ -79,7 +78,6 @@ async fn main() { pool.spawn_ok(fut.map(|_| ())); let gm = create_game_manager("0.0.0.0:9142", pool.clone()).await; - let mut routes = Vec::new(); routes::fuel(&mut routes); @@ -88,7 +86,6 @@ async fn main() { engines.tera.register_filter("get_colour", get_colour); }); - rocket::ignite() .manage(gm) .manage(pool) diff --git a/backend/src/planetwars/mod.rs b/backend/src/planetwars/mod.rs index 859d253..b1de23b 100644 --- a/backend/src/planetwars/mod.rs +++ b/backend/src/planetwars/mod.rs @@ -23,10 +23,11 @@ pub struct PlanetWarsGame { log_file: File, turns: u64, name: String, + map: String, } impl PlanetWarsGame { - pub fn new(state: pw_rules::PlanetWars, location: &str, name: &str) -> Self { + pub fn new(state: pw_rules::PlanetWars, location: &str, name: &str, map: &str) -> Self { let planet_map = state .planets .iter() @@ -46,6 +47,7 @@ impl PlanetWarsGame { log_file: file, turns: 0, name: name.to_string(), + map: map.to_string(), } } @@ -197,8 +199,9 @@ impl game::Controller for PlanetWarsGame { Some(json!({ "winners": self.state.living_players(), "turns": self.state.turn_num, - "name": self.name.clone(), - "file": self.log_file_loc.clone(), + "name": self.name, + "map": self.map, + "file": self.log_file_loc, })) } else { None @@ -212,5 +215,6 @@ pub struct FinishedState { pub turns: u64, pub name: String, pub file: String, + pub map: String, pub players: Vec<(u64, String)>, } diff --git a/backend/src/routes.rs b/backend/src/routes.rs index 159428d..e7cb87a 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -165,7 +165,7 @@ fn build_builder( }; let game = - planetwars::PlanetWarsGame::new(config.create_game(number_of_clients as usize), &generate_string_id(), name); + planetwars::PlanetWarsGame::new(config.create_game(number_of_clients as usize), &generate_string_id(), name, map); let players: Vec = (0..number_of_clients).collect(); diff --git a/backend/src/util.rs b/backend/src/util.rs index d954b98..95b5425 100644 --- a/backend/src/util.rs +++ b/backend/src/util.rs @@ -94,17 +94,18 @@ impl Ord for GameState { } } +use std::path::PathBuf; impl From for GameState { fn from(mut state: FinishedState) -> Self { state.players.sort_by_key(|x| x.0); GameState::Finished { - map: String::new(), players: state - .players - .iter() - .map(|(id, name)| (name.clone(), state.winners.contains(&id))) - .collect(), + .players + .iter() + .map(|(id, name)| (name.clone(), state.winners.contains(&id))) + .collect(), + map: PathBuf::from(state.map).file_stem().and_then(|x| x.to_str()).unwrap().to_string(), name: state.name, turns: state.turns, file: state.file, @@ -112,23 +113,6 @@ impl From for GameState { } } -/// Visualiser game option -#[derive(Serialize)] -pub struct GameOption { - name: String, - location: String, - turns: u64, -} -impl GameOption { - pub fn new(name: &str, location: &str, turns: u64) -> Self { - Self { - name: name.to_string(), - location: location.to_string(), - turns, - } - } -} - #[derive(Serialize)] struct Link { name: String, @@ -218,7 +202,7 @@ use futures::stream::StreamExt; pub async fn get_games() -> Vec { match fs::File::open("games.ini").await { Ok(file) => { - let mut file = BufReader::new(file); + let file = BufReader::new(file); file.lines() .filter_map(move |maybe| async { maybe diff --git a/backend/templates/state_partial.html.tera b/backend/templates/state_partial.html.tera index 3e6c5b8..16e783d 100644 --- a/backend/templates/state_partial.html.tera +++ b/backend/templates/state_partial.html.tera @@ -4,7 +4,7 @@