wip : shuffle at the init phase

This commit is contained in:
gvcgael
2022-11-30 18:38:22 +01:00
parent 1e7dbc61e9
commit 487b6312f6

View File

@@ -46,19 +46,19 @@ static INDEX_HTML: Lazy<String> = Lazy::new(|| {
static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default);
struct State {
rem_a: Vec<String>,
rem_b: Vec<String>,
participants: 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 {
fn default() -> Self {
Self {
rem_a: everyone(),
rem_b: everyone(),
participants: everyone(),
}
}
}
@@ -71,28 +71,20 @@ struct Input {
async fn input(Json(input): Json<Input>) -> 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()
}
}