diff --git a/backend/.gitignore b/backend/.gitignore index 7700136..af9d0de 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -4,3 +4,4 @@ games/ maps/ trace.log games.ini +static/bot diff --git a/backend/src/planetwars/mod.rs b/backend/src/planetwars/mod.rs index 45de3e6..67ed7a7 100644 --- a/backend/src/planetwars/mod.rs +++ b/backend/src/planetwars/mod.rs @@ -5,7 +5,7 @@ use serde_json; use std::collections::HashMap; use std::convert::TryInto; -use std::fs::{File, create_dir}; +use std::fs::{create_dir, File}; use std::io::Write; mod pw_config; @@ -60,8 +60,6 @@ impl PlanetWarsGame { ) .unwrap(); - // println!("{}", serde_json::to_string(&state).unwrap()); - for player in self .state .players @@ -83,7 +81,6 @@ impl PlanetWarsGame { )); if !player.alive || self.state.is_finished() { - println!("Kicking player {}", player.id); updates.push(HostMsg::Kick(player.id as u64)); } } @@ -172,8 +169,8 @@ impl PlanetWarsGame { } } -use std::fs::OpenOptions; use ini::Ini; +use std::fs::OpenOptions; impl game::Controller for PlanetWarsGame { fn start(&mut self) -> Vec { let mut updates = Vec::new(); @@ -200,14 +197,19 @@ impl game::Controller for PlanetWarsGame { fn is_done(&mut self) -> bool { if self.state.is_finished() { if !self.logged { - - - let mut f = match OpenOptions::new().create(true).append(true).open("games.ini") { Err(_) => return true, Ok(f) => f }; + let mut f = match OpenOptions::new() + .create(true) + .append(true) + .open("games.ini") + { + Err(_) => return true, + Ok(f) => f, + }; let mut conf = Ini::new(); conf.with_section(Some(self.log_file_loc.clone())) - .set("name", &self.name) - .set("turns", format!("{}", self.turns)); + .set("name", &self.name) + .set("turns", format!("{}", self.turns)); conf.write_to(&mut f).unwrap(); diff --git a/backend/src/planetwars/pw_rules.rs b/backend/src/planetwars/pw_rules.rs index 703daf8..5848db5 100644 --- a/backend/src/planetwars/pw_rules.rs +++ b/backend/src/planetwars/pw_rules.rs @@ -1,4 +1,5 @@ /// The planet wars game rules. +#[derive(Debug)] pub struct PlanetWars { pub players: Vec, pub planets: Vec, diff --git a/backend/src/planetwars/pw_serializer.rs b/backend/src/planetwars/pw_serializer.rs index c6f1223..0d17b14 100644 --- a/backend/src/planetwars/pw_serializer.rs +++ b/backend/src/planetwars/pw_serializer.rs @@ -47,7 +47,7 @@ impl<'a> Serializer<'a> { /// rotated based on the number offset for this serializer. fn player_num(&self, player_id: usize) -> usize { let num_players = self.state.players.len(); - let rotated_id = (player_id + self.player_num_offset) % num_players; + let rotated_id = (player_id + num_players - self.player_num_offset) % num_players; // protocol player ids start at 1 return rotated_id + 1; } diff --git a/backend/src/routes.rs b/backend/src/routes.rs index bf76d48..b6b270e 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -104,12 +104,28 @@ struct GameReq { name: String, } +#[derive(Serialize)] +struct GameRes { + players: Vec, +} + +use mozaic::util::request::Connect; #[post("/lobby", data="")] -async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result { +async fn game_post(game_req: Json, tp: State<'_, ThreadPool>, gm: State<'_, game::Manager>, state: State<'_, Games>) -> Result, String> { let game = build_builder(tp.clone(), game_req.nop, game_req.max_turns, &game_req.map, &game_req.name); let game_id = gm.start_game(game).await.unwrap(); state.add_game(game_req.name.clone(), game_id); - Ok(format!("{:?}", gm.get_state(game_id).await)) + + if let Some(conns) = gm.get_state(game_id).await { + let players: Vec = conns.iter().map(|conn| match conn { + Connect::Waiting(_, key) => *key, + _ => 0, + }).collect(); + + Ok(Json(GameRes { players })) + } else { + Err(String::from("Fuck the world")) + } } pub fn fuel(routes: &mut Vec) { diff --git a/backend/static/style/style.css b/backend/static/style/style.css index c4382fc..42d782f 100644 --- a/backend/static/style/style.css +++ b/backend/static/style/style.css @@ -19,6 +19,8 @@ body { display: flex; flex-direction: column; align-self: flex-start; + overflow-y: scroll; + height: 100%;; } .input_container {