From ed2b1808b64a462652b2734ce9465e9ec289cf53 Mon Sep 17 00:00:00 2001 From: Andreas Larsen Date: Tue, 7 May 2019 20:54:42 +0200 Subject: [PATCH] feature(score): read score file on startup --- src/main.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2f4d08..5e0be3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate serde; extern crate serde_json; extern crate chrono; +use std::io::Read; use std::io::Write; use crate::futures::Stream; use tokio_core::reactor::Core; @@ -29,7 +30,19 @@ fn main() { let scores : Arc>> = 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(); std::thread::spawn(move || { @@ -49,7 +62,6 @@ fn main() { 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 json = serde_json::to_string(&*map).unwrap(); @@ -91,7 +103,7 @@ fn main() { api.spawn(message.text_reply( format!("Score json: {}", json) )); - } else if data.contains("-time") { + } else if data == "-time" { api.spawn(message.text_reply( format!("local time: {}, leet time: {}, time distance to leet: {:#?}", current_time, leet_time, (current_time - leet_time).num_seconds())