feature(score): read score file on startup
This commit is contained in:
parent
6b65045795
commit
ed2b1808b6
18
src/main.rs
18
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<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();
|
||||
|
||||
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())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user