-
Notifications
You must be signed in to change notification settings - Fork 0
/
player-dump.rs
65 lines (56 loc) · 1.9 KB
/
player-dump.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use chesscom::{ChessApi, api::ChessStats};
use chrono::{Duration, Utc};
use std::{env, process};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
fn exit(msg: &str) -> ! {
eprintln!("{}", msg);
process::exit(1);
}
#[tokio::main]
async fn main() -> Result<()> {
let mut args = env::args();
args.next(); // discard args[0]
let username = match args.next() {
Some(arg) => arg,
None => exit("USAGE: profile <username>"),
};
let chess_api = ChessApi::new();
let profile = chess_api.get_profile(&username).await?;
println!("Username: {}", profile.username);
println!("Status: {}", profile.status);
println!("Name: {}", profile.name.as_deref().unwrap_or("<Unknown>"));
println!("Joined: {}", profile.joined.date().format("%Y-%m-%d"));
let time_since_online = Utc::today() - profile.last_online.date();
if time_since_online < Duration::hours(1) {
println!("Last Online: {} mins ago", time_since_online.num_minutes());
} else if time_since_online < Duration::days(1) {
println!("Last Online: {} hours ago", time_since_online.num_hours());
} else {
println!("Last Online: {} days ago", time_since_online.num_days());
}
let stats = chess_api.get_user_stats(&username).await?;
if let Some(stats) = stats.chess_daily {
print_stats("Daily", &stats);
}
if let Some(stats) = stats.chess_rapid {
print_stats("Rapid", &stats);
}
if let Some(stats) = stats.chess_blitz {
print_stats("Blitz", &stats);
}
if let Some(stats) = stats.chess_bullet {
print_stats("Bullet", &stats);
}
Ok(())
}
fn print_stats(label: &str, stats: &ChessStats) {
println!(
"{}: {} rating (rd={}) with {}-{}-{} record",
label,
stats.last.rating,
stats.last.rd,
stats.record.win,
stats.record.loss,
stats.record.draw
);
}