diff options
author | Santo Cariotti <santo@dcariotti.me> | 2020-12-04 19:24:02 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2020-12-04 19:24:02 +0100 |
commit | 21817cfad161ecbb9c391ab05b4486835ea6785a (patch) | |
tree | cd518ff486e98f8400ecd810004b715db487ae21 | |
parent | 2985beeb2754944b7dcc97762638e07a47822818 (diff) |
feat: associate player to one another
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 73 |
2 files changed, 74 insertions, 1 deletions
@@ -7,3 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = "2.33.3" +rand = "0.7.3" diff --git a/src/main.rs b/src/main.rs index e7a11a9..f80069f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,74 @@ +extern crate clap; +use clap::{App, Arg}; +use rand::prelude::*; +use std::collections::HashMap; +use std::fs; + +fn my_gift_to(players: &mut Vec<String>) -> Option<&String> { + let mut rng = rand::thread_rng(); + players.shuffle(&mut rng); + + Some(&players[0]) +} + fn main() { - println!("Hello, world!"); + let matches = App::new("rsanta") + .version("0.1.0") + .author("Santo Cariotti <santo@dcariotti.me>") + .about("Make secret santa with your friends") + .arg( + Arg::with_name("file") + .short("f") + .long("file") + .value_name("FILE") + .help("Input file for players") + .takes_value(true), + ) + .get_matches(); + + let config_file = matches + .value_of("file") + .expect("You must specify a config file"); + let contents = fs::read_to_string(config_file) + .expect("Error occurs reading this file") + .to_string(); + let mut members_data: Vec<&str> = contents.split("\n").collect(); + + // ignore the last element, cause it's empty + members_data.pop(); + + let mut rng = rand::thread_rng(); + members_data.shuffle(&mut rng); + + let mut members = HashMap::new(); + for member in members_data { + let details: Vec<&str> = member.split(" ").collect(); + + // <email> : <name> + members.insert(details[1], details[0]); + } + + let mut done: Vec<String> = Vec::new(); + let mut emails: Vec<String> = Vec::new(); + for (email, _) in &members { + emails.push(email.to_string()); + } + println!("{:?}", emails); + + for (email, name) in members.into_iter() { + loop { + match my_gift_to(&mut emails) { + Some(gift_to) => { + if gift_to.to_string() != email.to_string() + && !done.iter().any(|v| v == gift_to) + { + println!("{} {}", email, gift_to); + done.push(gift_to.to_string()); + break; + } + } + None => break, + } + } + } } |