use std::{hash::Hasher, collections::hash_map::DefaultHasher, time::{SystemTime, UNIX_EPOCH}}; // this is trash but i only need a unique string each minute fn formatted_timestamp() -> String { let t = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("UNIX time overflowed again") .as_secs(); format!( "D{} H{} M{}", (t / (60 * 60 * 24)), (t / (60 * 60)) % 60, (t / 60) % 60, ) } fn main() { let now_str = formatted_timestamp(); let mut hasher = DefaultHasher::new(); hasher.write(now_str.as_bytes()); let index = hasher.finish() as usize; let file = std::env::var("MOOD_FILE").expect("missing MOOD_FILE env variable"); let content = std::fs::read_to_string(file).expect("could not read MOOD_FILE contents"); let lines : Vec<(u64, &str)> = content.lines() .map(|l| l.split_once(' ').unwrap()) .map(|(n, l)| (n.parse::().unwrap(), l)) .collect(); let mut pool = Vec::new(); let sum = lines.iter().map(|(x,_)| x).sum::(); pool.reserve(sum as usize); for (count, line) in lines { for _ in 0..count { pool.push((line, count as f32 / sum as f32)); } } let (choice, chance) = pool[index % pool.len()]; println!("Content-type: application/json"); println!(); println!("{{\"id\":{},\"mood\":\"{}\",\"chance\":{:.6}}}", index, choice, chance); }