wip : shuffle at the init phase

This commit is contained in:
2024-11-23 16:22:28 +01:00
parent a9e83d6742
commit 925cdab931

View File

@@ -46,19 +46,19 @@ static INDEX_HTML: Lazy<String> = Lazy::new(|| {
static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default); static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default);
struct State { struct State {
rem_a: Vec<String>, participants: Vec<String>,
rem_b: Vec<String>,
} }
fn everyone() -> Vec<String> { fn everyone() -> Vec<String> {
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 { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { Self {
rem_a: everyone(), participants: everyone(),
rem_b: everyone(),
} }
} }
} }
@@ -71,28 +71,20 @@ struct Input {
async fn input(Json(input): Json<Input>) -> String { async fn input(Json(input): Json<Input>) -> String {
let mut state = STATE.lock().await; let mut state = STATE.lock().await;
if state.rem_a.is_empty() { if state.participants.is_empty() {
return "ERROR (everybody drew already)".into(); return "ERROR (everybody drew already)".into();
} }
info!("joueur qui doivent piocher : {:?}", state.rem_a.join(", ")); match state.participants.iter().position(|p| input.person == *p) {
match state.rem_a.iter().position(|p| input.person == *p) {
Some(pos) => { Some(pos) => {
info!("joueur qui pioche : {:?}", state.rem_a[pos]); info!("joueur qui pioche : {:?}", pos);
state.rem_a.remove(pos); return match state.participants.iter().nth(pos+1) {
} Some(x) => x.to_string(),
None => { None => return match state.participants.first() {
return "ERROR (you drew already)".into() Some(x) => x.to_string(),
} None => "ERROR".to_string()
}
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);
} }
} }
} }
None => "ERROR".to_string()
}
}