fill game when all players are ready

Co-authored-by: Eirik Witterso <eirikwit@pvv.ntnu.no>
This commit is contained in:
Daniel Lovbrotte Olsen 2024-02-11 01:09:31 +01:00
parent 20c3108389
commit bc8572caaa
2 changed files with 30 additions and 7 deletions

View File

@ -129,10 +129,20 @@ impl GameState {
Ok(game) 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 /// Fills the factories from the bag
/// Will replenish the bag from the lid when empty /// Will replenish the bag from the lid when empty
/// Will return with partially filled factories if out of tiles /// 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() { if !self.only_start() {
return Err("Cannot fill, there are still tiles left to be picked"); return Err("Cannot fill, there are still tiles left to be picked");
} }
@ -163,8 +173,6 @@ impl GameState {
let dist = WeightedIndex::new(&weights).unwrap(); let dist = WeightedIndex::new(&weights).unwrap();
let picked = choices[dist.sample(&mut self.rng)]; let picked = choices[dist.sample(&mut self.rng)];
eprintln!("picked {:?}", picked);
self.bag.add_color(picked, -1)?; self.bag.add_color(picked, -1)?;
factory.add_color(picked, 1)?; factory.add_color(picked, 1)?;
} }
@ -610,6 +618,7 @@ impl From<TileSet> for TileSetWithStart {
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
struct Player { struct Player {
ready: bool,
points: usize, points: usize,
pattern_lines: [PatternLine; 5], pattern_lines: [PatternLine; 5],
wall: Wall, wall: Wall,

View File

@ -1,3 +1,4 @@
use azul::PlayerName;
use rocket::response::status::NotFound; use rocket::response::status::NotFound;
use rocket::serde::{json::Json, Deserialize, Serialize}; use rocket::serde::{json::Json, Deserialize, Serialize};
use rocket::State; use rocket::State;
@ -19,13 +20,26 @@ fn index() -> String {
"Hello, world!".to_string() "Hello, world!".to_string()
} }
#[get("/game/<id>")] #[get("/game/<id>?<player>")]
fn get_game( fn get_game(
id: uuid::Uuid, id: uuid::Uuid,
shared: &State<SharedState>, shared: &State<SharedState>,
player: Option<String>
) -> Result<Json<azul::GameState>, NotFound<&'static str>> { ) -> Result<Json<azul::GameState>, NotFound<&'static str>> {
let games = shared.games.lock().unwrap(); let mut games = shared.games.lock().unwrap();
let game = games.get(&id).ok_or(NotFound("Game not found"))?; let game = games.get_mut(&id).ok_or(NotFound("Game not found"))?;
let player: Option<PlayerName> = 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())); return Ok(Json(game.to_owned()));
} }