diff --git a/src/azul.rs b/src/azul.rs index 98f09c9..7718a41 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -129,10 +129,20 @@ impl GameState { Ok(game) } + + pub fn set_ready(&mut self, player_name: &PlayerName) -> Result<(), &str> { + self.players.get_mut(player_name).ok_or("That player is not part of this game")?.ready = true; + Ok(()) + } + + pub fn all_ready(&self) -> bool { + self.players.values().all(|x| x.ready) + } + /// Fills the factories from the bag /// Will replenish the bag from the lid when empty /// Will return with partially filled factories if out of tiles - fn fill(&mut self) -> Result<(), &'static str> { + pub fn fill(&mut self) -> Result<(), &'static str> { if !self.only_start() { return Err("Cannot fill, there are still tiles left to be picked"); } @@ -162,9 +172,7 @@ impl GameState { let dist = WeightedIndex::new(&weights).unwrap(); let picked = choices[dist.sample(&mut self.rng)]; - - eprintln!("picked {:?}", picked); - + self.bag.add_color(picked, -1)?; factory.add_color(picked, 1)?; } @@ -610,6 +618,7 @@ impl From for TileSetWithStart { #[derive(Debug, Serialize, Deserialize, Clone, Default)] struct Player { + ready: bool, points: usize, pattern_lines: [PatternLine; 5], wall: Wall, diff --git a/src/main.rs b/src/main.rs index d69775d..c021f48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use azul::PlayerName; use rocket::response::status::NotFound; use rocket::serde::{json::Json, Deserialize, Serialize}; use rocket::State; @@ -19,13 +20,26 @@ fn index() -> String { "Hello, world!".to_string() } -#[get("/game/")] +#[get("/game/?")] fn get_game( id: uuid::Uuid, shared: &State, + player: Option ) -> Result, NotFound<&'static str>> { - let games = shared.games.lock().unwrap(); - let game = games.get(&id).ok_or(NotFound("Game not found"))?; + let mut games = shared.games.lock().unwrap(); + let game = games.get_mut(&id).ok_or(NotFound("Game not found"))?; + + let player: Option = player.map(|x| x.into()); + + if !game.all_ready() { + if let Some(player) = player { + game.set_ready(&player); + if game.all_ready() { + game.fill(); + } + } + } + return Ok(Json(game.to_owned())); }