feature(score): read score file on startup

This commit is contained in:
andreas 2019-05-07 20:54:42 +02:00
parent 6b65045795
commit ed2b1808b6

View File

@ -5,6 +5,7 @@ extern crate serde;
extern crate serde_json; extern crate serde_json;
extern crate chrono; extern crate chrono;
use std::io::Read;
use std::io::Write; use std::io::Write;
use crate::futures::Stream; use crate::futures::Stream;
use tokio_core::reactor::Core; use tokio_core::reactor::Core;
@ -29,7 +30,19 @@ fn main() {
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()));
{ {
if let Ok(mut score_file) = std::fs::File::open(SCORE_FILE_PATH) {
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");
}
}
{
let scores = scores.clone(); let scores = scores.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
@ -49,7 +62,6 @@ fn main() {
std::thread::sleep(std::time::Duration::from_secs(5*60)); std::thread::sleep(std::time::Duration::from_secs(5*60));
} }
}); });
} }
@ -83,7 +95,7 @@ fn main() {
)); ));
} }
} else if data.contains("-score") { } else if data == "-score" {
let map = scores.lock().unwrap(); let map = scores.lock().unwrap();
let json = serde_json::to_string(&*map).unwrap(); let json = serde_json::to_string(&*map).unwrap();
@ -91,7 +103,7 @@ fn main() {
api.spawn(message.text_reply( api.spawn(message.text_reply(
format!("Score json: {}", json) format!("Score json: {}", json)
)); ));
} else if data.contains("-time") { } else if data == "-time" {
api.spawn(message.text_reply( api.spawn(message.text_reply(
format!("local time: {}, leet time: {}, time distance to leet: {:#?}", current_time, leet_time, (current_time - leet_time).num_seconds()) format!("local time: {}, leet time: {}, time distance to leet: {:#?}", current_time, leet_time, (current_time - leet_time).num_seconds())