2019-05-07 18:00:23 +00:00
|
|
|
extern crate telegram_bot;
|
|
|
|
|
extern crate futures;
|
|
|
|
|
extern crate tokio_core;
|
|
|
|
|
extern crate serde;
|
|
|
|
|
extern crate serde_json;
|
|
|
|
|
extern crate chrono;
|
|
|
|
|
|
2019-05-07 18:54:42 +00:00
|
|
|
use std::io::Read;
|
2019-05-07 18:00:23 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
use crate::futures::Stream;
|
|
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
|
use std::env;
|
|
|
|
|
use telegram_bot::*;
|
|
|
|
|
|
|
|
|
|
use chrono::prelude::*;
|
|
|
|
|
|
|
|
|
|
use std::sync::*;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
const SCORE_FILE_PATH : &str = "score.json";
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let mut core = Core::new().unwrap();
|
|
|
|
|
|
|
|
|
|
let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
|
|
|
|
|
let api = Api::configure(token).build(core.handle()).unwrap();
|
|
|
|
|
|
|
|
|
|
let leet_time = NaiveTime::from_hms(13, 37, 30);
|
|
|
|
|
|
|
|
|
|
let scores : Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new()));
|
2019-05-14 12:26:57 +00:00
|
|
|
let time_scored : Arc<Mutex<HashMap<String, DateTime<Local>>>> = Arc::new(Mutex::new(HashMap::new()));
|
2019-05-07 18:00:23 +00:00
|
|
|
|
|
|
|
|
{
|
2019-05-07 18:54:42 +00:00
|
|
|
if let Ok(mut score_file) = std::fs::File::open(SCORE_FILE_PATH) {
|
2019-05-07 18:00:23 +00:00
|
|
|
|
2019-05-07 18:54:42 +00:00
|
|
|
let mut json_str = String::new();
|
|
|
|
|
|
|
|
|
|
score_file.read_to_string(&mut json_str).expect("Failed to read score file");
|
|
|
|
|
|
|
|
|
|
let mut map = scores.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
*map = serde_json::from_str(&json_str).expect("Failed to deserialize scores");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2019-05-07 18:00:23 +00:00
|
|
|
let scores = scores.clone();
|
|
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
loop {
|
|
|
|
|
{
|
|
|
|
|
let map = scores.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let json_str = serde_json::to_string(&*map).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut score_file = std::fs::File::create(SCORE_FILE_PATH).expect("Unable to open or create file");
|
|
|
|
|
|
|
|
|
|
score_file.write_all(json_str.as_bytes()).expect("unable to write to score file!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("written score file");
|
|
|
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(5*60));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let future = api.stream().for_each(|update| {
|
|
|
|
|
if let UpdateKind::Message(message) = update.kind {
|
|
|
|
|
if let MessageKind::Text {ref data, ..} = message.kind {
|
|
|
|
|
println!("<{}>: {}", message.from.first_name, data);
|
|
|
|
|
|
|
|
|
|
let current_time = Local::now().time();
|
|
|
|
|
|
|
|
|
|
let allowed_timegap = 30;
|
|
|
|
|
|
|
|
|
|
if data.to_lowercase() == "leet" || data == "1337" {
|
|
|
|
|
dbg!(data);
|
|
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
let leet_distance = (current_time - leet_time).num_seconds().abs();
|
2019-05-07 18:00:23 +00:00
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
if leet_distance > allowed_timegap {
|
2019-05-07 18:00:23 +00:00
|
|
|
api.spawn(message.text_reply(
|
|
|
|
|
"It is not leet right now".to_string()
|
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
let mut time_scored_map = time_scored.lock().unwrap();
|
2019-05-07 18:00:23 +00:00
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
let time_scored = time_scored_map.entry(message.from.first_name.clone()).or_insert(Local.timestamp(0,0));
|
2019-05-07 18:00:23 +00:00
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
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 entry = map.entry(message.from.first_name.clone()).or_insert(0);
|
|
|
|
|
|
|
|
|
|
*entry += 1;
|
|
|
|
|
*time_scored = Local::now();
|
|
|
|
|
|
|
|
|
|
api.spawn(message.text_reply(
|
|
|
|
|
format!("{} just hit leet! New score: {}", message.from.first_name, *entry)
|
|
|
|
|
));
|
2019-07-02 19:09:10 +00:00
|
|
|
|
2019-07-02 19:26:27 +00:00
|
|
|
let current_leet_count = time_scored_map.iter().filter(|(_,t)| (**t - Local::now()).num_seconds().abs() < allowed_timegap * 2).count();
|
2019-07-02 19:09:10 +00:00
|
|
|
|
|
|
|
|
if current_leet_count == 3 {
|
|
|
|
|
api.spawn(message.chat.text("OH BABY A TRIPPLE!!!"));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 12:26:57 +00:00
|
|
|
}
|
2019-05-07 18:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-07 18:54:42 +00:00
|
|
|
} else if data == "-score" {
|
2019-05-07 18:00:23 +00:00
|
|
|
let map = scores.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let json = serde_json::to_string(&*map).unwrap();
|
|
|
|
|
|
|
|
|
|
api.spawn(message.text_reply(
|
|
|
|
|
format!("Score json: {}", json)
|
|
|
|
|
));
|
2019-05-07 18:54:42 +00:00
|
|
|
} else if data == "-time" {
|
2019-05-07 18:00:23 +00:00
|
|
|
|
|
|
|
|
api.spawn(message.text_reply(
|
|
|
|
|
format!("local time: {}, leet time: {}, time distance to leet: {:#?}", current_time, leet_time, (current_time - leet_time).num_seconds())
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
core.run(future).unwrap();
|
|
|
|
|
}
|