Display set counts in player info

This commit is contained in:
Kiana Sheibani 2023-11-27 01:05:58 -05:00
parent db85674601
commit 14b5b2cafd
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
5 changed files with 44 additions and 5 deletions

View file

@ -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::<i64, _>("sets_count_won")? as u64,
statement.read::<i64, _>("sets_count_lost")? as u64,
))
}
pub fn set_player_data(
connection: &Connection,
dataset: &str,
@ -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::<i64, _>("id")?, 1);
assert_eq!(statement.read::<String, _>("name")?, "1");

View file

@ -470,6 +470,9 @@ fn player_info(dataset: Option<String>, 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<String>, 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);
}

View file

@ -14,6 +14,15 @@ pub enum StringOrInt {
Int(u64),
}
impl StringOrInt {
pub fn from_string(s: &str) -> Self {
match s.parse::<u64>() {
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 {

View file

@ -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<()> {

View file

@ -30,6 +30,8 @@ pub fn read_string() -> String {
line.trim().to_owned()
}
// Player Input
pub enum PlayerInput {
Id(PlayerId),
Discrim(String),