fix(main): users can no longer score more than once per day

This commit is contained in:
andreas 2019-05-14 14:26:57 +02:00
parent 4bdfde581d
commit 39a021bfc1

View File

@ -28,6 +28,7 @@ fn main() {
let leet_time = NaiveTime::from_hms(13, 37, 30); let leet_time = NaiveTime::from_hms(13, 37, 30);
let scores : Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new())); let scores : Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new()));
let time_scored : Arc<Mutex<HashMap<String, DateTime<Local>>>> = Arc::new(Mutex::new(HashMap::new()));
{ {
if let Ok(mut score_file) = std::fs::File::open(SCORE_FILE_PATH) { if let Ok(mut score_file) = std::fs::File::open(SCORE_FILE_PATH) {
@ -77,23 +78,37 @@ fn main() {
if data.to_lowercase() == "leet" || data == "1337" { if data.to_lowercase() == "leet" || data == "1337" {
dbg!(data); dbg!(data);
let leet_distance = current_time - leet_time; let leet_distance = (current_time - leet_time).num_seconds().abs();
if leet_distance.num_seconds() > allowed_timegap { if leet_distance > allowed_timegap {
api.spawn(message.text_reply( api.spawn(message.text_reply(
"It is not leet right now".to_string() "It is not leet right now".to_string()
)); ));
} else { } else {
let mut time_scored_map = time_scored.lock().unwrap();
let time_scored = time_scored_map.entry(message.from.first_name.clone()).or_insert(Local.timestamp(0,0));
let time_scored_distance = (Local::now() - *time_scored).num_seconds().abs();
if time_scored_distance < allowed_timegap * 4 {
api.spawn(message.text_reply(
"You already hit leet today you dumbass, don't try to cheat!".to_string()
));
} else {
let mut map = scores.lock().unwrap(); let mut map = scores.lock().unwrap();
let entry = map.entry(message.from.first_name.clone()).or_insert(0); let entry = map.entry(message.from.first_name.clone()).or_insert(0);
*entry += 1; *entry += 1;
*time_scored = Local::now();
api.spawn(message.text_reply( api.spawn(message.text_reply(
format!("{} just hit leet! New score: {}", message.from.first_name, *entry) format!("{} just hit leet! New score: {}", message.from.first_name, *entry)
)); ));
} }
}
} else if data == "-score" { } else if data == "-score" {
let map = scores.lock().unwrap(); let map = scores.lock().unwrap();