From 14b5b2cafdbe1e268a2417b4eb043cde6b472688 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Mon, 27 Nov 2023 01:05:58 -0500 Subject: [PATCH] Display set counts in player info --- src/database.rs | 24 +++++++++++++++++++++--- src/main.rs | 12 +++++++++++- src/queries/scalars.rs | 9 +++++++++ src/sync.rs | 2 +- src/util.rs | 2 ++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/database.rs b/src/database.rs index a3054f1..5e5533b 100644 --- a/src/database.rs +++ b/src/database.rs @@ -383,6 +383,25 @@ pub fn get_player_rating_data( )) } +pub fn get_player_set_counts( + connection: &Connection, + dataset: &str, + player: PlayerId, +) -> sqlite::Result<(u64, u64)> { + let query = format!( + r#"SELECT sets_count_won, sets_count_lost FROM "{}_players" WHERE id = ?"#, + dataset + ); + + let mut statement = connection.prepare(&query)?; + statement.bind((1, player.0 as i64))?; + statement.next()?; + Ok(( + statement.read::("sets_count_won")? as u64, + statement.read::("sets_count_lost")? as u64, + )) +} + pub fn set_player_data( connection: &Connection, dataset: &str, @@ -656,7 +675,7 @@ pub mod tests { // Mock a database file in transient memory pub fn mock_datasets() -> sqlite::Result { - let query = "PRAGMA foreign_keys = ON; + let query = "PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS datasets ( name TEXT UNIQUE NOT NULL, @@ -751,8 +770,7 @@ CREATE TABLE IF NOT EXISTS sets ( add_players(&connection, "test", &vec![players(2)], Timestamp(0))?; - let mut statement = - connection.prepare("SELECT * FROM dataset_test_players WHERE id = 1")?; + let mut statement = connection.prepare("SELECT * FROM players WHERE id = 1")?; statement.next()?; assert_eq!(statement.read::("id")?, 1); assert_eq!(statement.read::("name")?, "1"); diff --git a/src/main.rs b/src/main.rs index 15c79bb..783a206 100644 --- a/src/main.rs +++ b/src/main.rs @@ -470,6 +470,9 @@ fn player_info(dataset: Option, player: String) { let (deviation, volatility, _) = get_player_rating_data(&connection, &dataset, id) .unwrap_or_else(|_| error("Could not find player", 1)); + let (won, lost) = get_player_set_counts(&connection, &dataset, id) + .unwrap_or_else(|_| error("Could not find player", 1)); + println!(); if let Some(pre) = prefix { print!("\x1b[2m{}\x1b[22m ", pre); @@ -479,8 +482,15 @@ fn player_info(dataset: Option, player: String) { \x1b[1m{0}\x1b[22m\x1b]8;;\x1b\\\x1b[0m ({1})", name, discrim ); - println!("\x1b[1mID:\x1b[0m {}", id.0); + + println!( + "\n\x1b[1mSet Count:\x1b[0m {} - {} ({:.3}%)", + won, + lost, + (won as f64 / (won + lost) as f64) * 100.0 + ); + println!("\n\x1b[1mDeviation:\x1b[0m {}", deviation); println!("\x1b[1mVolatility:\x1b[0m {}", volatility); } diff --git a/src/queries/scalars.rs b/src/queries/scalars.rs index 32c6722..c7b1486 100644 --- a/src/queries/scalars.rs +++ b/src/queries/scalars.rs @@ -14,6 +14,15 @@ pub enum StringOrInt { Int(u64), } +impl StringOrInt { + pub fn from_string(s: &str) -> Self { + match s.parse::() { + Ok(x) => StringOrInt::Int(x), + Err(_) => StringOrInt::String(s.to_owned()), + } + } +} + impl Display for StringOrInt { fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { match self { diff --git a/src/sync.rs b/src/sync.rs index 70d32fc..7639abc 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -323,7 +323,7 @@ pub fn sync_dataset( #[cfg(test)] mod tests { use super::*; - use crate::database::tests::*; + use crate::datasets::tests::*; #[test] fn glicko_single() -> sqlite::Result<()> { diff --git a/src/util.rs b/src/util.rs index 9980b16..15d06e0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -30,6 +30,8 @@ pub fn read_string() -> String { line.trim().to_owned() } +// Player Input + pub enum PlayerInput { Id(PlayerId), Discrim(String),