diff --git a/backend/src/planetwars/mod.rs b/backend/src/planetwars/mod.rs index b1de23b..0f6b298 100644 --- a/backend/src/planetwars/mod.rs +++ b/backend/src/planetwars/mod.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use std::convert::TryInto; use std::fs::{create_dir, File}; use std::io::Write; +use std::path::PathBuf; mod pw_config; mod pw_protocol; @@ -47,7 +48,7 @@ impl PlanetWarsGame { log_file: file, turns: 0, name: name.to_string(), - map: map.to_string(), + map: PathBuf::from(map).file_stem().and_then(|x| x.to_str()).unwrap().to_string(), } } @@ -194,6 +195,12 @@ impl game::Controller for PlanetWarsGame { updates } + fn state(&mut self) -> Value { + json!({ + "map": self.map, + }) + } + fn is_done(&mut self) -> Option { if self.state.is_finished() { Some(json!({ diff --git a/backend/src/routes.rs b/backend/src/routes.rs index e7cb87a..5a9e96e 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -1,5 +1,6 @@ use serde::{Deserialize}; +use serde_json::Value; use rocket::{Route, State}; use rocket::response::NamedFile; @@ -107,6 +108,7 @@ struct GameReq { #[derive(Serialize)] struct GameRes { players: Vec, + state: Value, } use mozaic::util::request::Connect; @@ -117,13 +119,13 @@ async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State state.add_game(game_req.name.clone(), game_id); match gm.get_state(game_id).await { - Some(Ok(conns)) => { + Some(Ok((state, conns))) => { let players: Vec = conns.iter().map(|conn| match conn { Connect::Waiting(_, key) => *key, _ => 0, }).collect(); - Ok(Json(GameRes { players })) + Ok(Json(GameRes { players, state })) }, Some(Err(v)) => { Err(serde_json::to_string(&v).unwrap()) diff --git a/backend/src/util.rs b/backend/src/util.rs index 95b5425..f5de360 100644 --- a/backend/src/util.rs +++ b/backend/src/util.rs @@ -52,6 +52,8 @@ impl From for PlayerStatus { } } +use serde_json::Value; + #[derive(Serialize, Eq, PartialEq)] #[serde(tag = "type")] pub enum GameState { @@ -68,6 +70,7 @@ pub enum GameState { players: Vec, connected: usize, total: usize, + state: Value, }, } @@ -94,7 +97,6 @@ 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); @@ -105,7 +107,7 @@ impl From for GameState { .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(), + map: state.map, name: state.name, turns: state.turns, file: state.file, @@ -238,7 +240,7 @@ pub async fn get_states( for (gs, name) in gss { if let Some(state) = gs { match state { - Ok(conns) => { + Ok((state, conns)) => { let players: Vec = conns.iter().cloned().map(|x| x.into()).collect(); let connected = players.iter().filter(|x| x.connected).count(); @@ -248,6 +250,7 @@ pub async fn get_states( players, connected, map: String::new(), + state, }); } Err(value) => { diff --git a/backend/templates/state_partial.html.tera b/backend/templates/state_partial.html.tera index 16e783d..fafdafc 100644 --- a/backend/templates/state_partial.html.tera +++ b/backend/templates/state_partial.html.tera @@ -3,7 +3,7 @@