diff --git a/src/main.rs b/src/main.rs index 91514f5..82fe481 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,19 +46,19 @@ static INDEX_HTML: Lazy = Lazy::new(|| { static STATE: Lazy> = Lazy::new(Mutex::default); struct State { - rem_a: Vec, - rem_b: Vec, + participants: Vec, } fn everyone() -> Vec { - vec!["Alice".into(), "Bob".into(), "Carol".into(), "Dave".into()] + let mut rng = thread_rng(); + let mut e = vec!["Alice".into(), "Bob".into(), "Carol".into(), "Dave".into()]; + return rng.shuffle(&e[..]; } impl Default for State { fn default() -> Self { Self { - rem_a: everyone(), - rem_b: everyone(), + participants: everyone(), } } } @@ -71,28 +71,20 @@ struct Input { async fn input(Json(input): Json) -> String { let mut state = STATE.lock().await; - if state.rem_a.is_empty() { + if state.participants.is_empty() { return "ERROR (everybody drew already)".into(); } - info!("joueur qui doivent piocher : {:?}", state.rem_a.join(", ")); - - match state.rem_a.iter().position(|p| input.person == *p) { + match state.participants.iter().position(|p| input.person == *p) { Some(pos) => { - info!("joueur qui pioche : {:?}", state.rem_a[pos]); - state.rem_a.remove(pos); - } - None => { - return "ERROR (you drew already)".into() - } - } - - loop { - let num = thread_rng().gen_range(0..state.rem_b.len()); - if state.rem_b[num] != input.person { - - info!("joueurs qui peuvent etre pick : {:?}", state.rem_b.join(", ")); - info!("{:?} picked {:?}", input.person, state.rem_b[num]); - return state.rem_b.remove(num); + info!("joueur qui pioche : {:?}", pos); + return match state.participants.iter().nth(pos+1) { + Some(x) => x.to_string(), + None => return match state.participants.first() { + Some(x) => x.to_string(), + None => "ERROR".to_string() + } + } } + None => "ERROR".to_string() } }