refactor: factor out player info display code
This commit is contained in:
parent
9cd8d77753
commit
9b21944624
1 changed files with 60 additions and 65 deletions
|
|
@ -3,6 +3,36 @@ use crate::queries::*;
|
|||
use crate::util::*;
|
||||
use sqlite::*;
|
||||
|
||||
fn player_display(
|
||||
PlayerData {
|
||||
id,
|
||||
prefix,
|
||||
name,
|
||||
discrim,
|
||||
}: PlayerData,
|
||||
with_discrim: bool,
|
||||
with_id: bool,
|
||||
newline: bool,
|
||||
) {
|
||||
if let Some(prefix) = prefix {
|
||||
print!("\x1b[2m{}\x1b[22m ", prefix);
|
||||
}
|
||||
print!(
|
||||
"\x1b[4m\x1b]8;;https://www.start.gg/user/{1}\x1b\\\
|
||||
\x1b[1m{0}\x1b[22m\x1b]8;;\x1b\\\x1b[0m",
|
||||
name, discrim
|
||||
);
|
||||
if with_discrim {
|
||||
print!(" ({})", discrim);
|
||||
}
|
||||
if with_id {
|
||||
print!("\n\x1b[1mID:\x1b[0m {}", id.0);
|
||||
}
|
||||
if newline {
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list(connection: &Connection, dataset: Option<String>, all: bool) {
|
||||
if all {
|
||||
} else {
|
||||
|
|
@ -13,26 +43,13 @@ pub fn list(connection: &Connection, dataset: Option<String>, all: bool) {
|
|||
pub fn info(connection: &Connection, dataset: Option<String>, player: String) {
|
||||
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
||||
|
||||
let PlayerData {
|
||||
id,
|
||||
name,
|
||||
prefix,
|
||||
discrim,
|
||||
} = get_player_from_input(connection, player)
|
||||
let player = get_player_from_input(connection, player)
|
||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||
|
||||
let (won, lost) = get_player_set_counts(connection, &dataset, id)
|
||||
let (won, lost) = get_player_set_counts(connection, &dataset, player.id)
|
||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||
|
||||
if let Some(pre) = prefix {
|
||||
print!("\x1b[2m{}\x1b[22m ", pre);
|
||||
}
|
||||
println!(
|
||||
"\x1b[4m\x1b]8;;https://www.start.gg/user/{1}\x1b\\\
|
||||
\x1b[1m{0}\x1b[22m\x1b]8;;\x1b\\\x1b[0m ({1})",
|
||||
name, discrim
|
||||
);
|
||||
println!("\x1b[1mID:\x1b[0m {}", id.0);
|
||||
player_display(player, true, true, true);
|
||||
|
||||
println!(
|
||||
"\n\x1b[1mSet Count:\x1b[0m {} - {} ({:.3}%)",
|
||||
|
|
@ -45,24 +62,15 @@ pub fn info(connection: &Connection, dataset: Option<String>, player: String) {
|
|||
pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String, player2: String) {
|
||||
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
||||
|
||||
let PlayerData {
|
||||
id: player1,
|
||||
name: name1,
|
||||
prefix: prefix1,
|
||||
discrim: discrim1,
|
||||
} = get_player_from_input(connection, player1)
|
||||
let player1 = get_player_from_input(connection, player1)
|
||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||
let id1 = player1.id;
|
||||
|
||||
let PlayerData {
|
||||
id: player2,
|
||||
name: name2,
|
||||
prefix: prefix2,
|
||||
discrim: discrim2,
|
||||
} = get_player_from_input(connection, player2)
|
||||
let player2 = get_player_from_input(connection, player2)
|
||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||
let id2 = player2.id;
|
||||
|
||||
let (hypothetical, advantage, variance) =
|
||||
get_network_data(connection, &dataset, player1, player2)
|
||||
let (hypothetical, advantage, variance) = get_network_data(connection, &dataset, id1, id2)
|
||||
.expect("Error communicating with SQLite")
|
||||
.map(|(adv, var)| (false, adv, var))
|
||||
.unwrap_or_else(|| {
|
||||
|
|
@ -72,8 +80,8 @@ pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String
|
|||
let (adv, var) = hypothetical_advantage(
|
||||
connection,
|
||||
&dataset,
|
||||
player1,
|
||||
player2,
|
||||
player1.id,
|
||||
player2.id,
|
||||
metadata.decay_const,
|
||||
)
|
||||
.expect("Error communicating with SQLite");
|
||||
|
|
@ -84,27 +92,14 @@ pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String
|
|||
|
||||
let (color, other_color) = ansi_num_color(advantage, 0.2, 2.0);
|
||||
|
||||
let len1 = prefix1.as_deref().map(|s| s.len() + 1).unwrap_or(0) + name1.len();
|
||||
let len2 = prefix2.as_deref().map(|s| s.len() + 1).unwrap_or(0) + name2.len();
|
||||
let len1 = player1.prefix.as_deref().map(|s| s.len() + 1).unwrap_or(0) + player1.name.len();
|
||||
let len2 = player2.prefix.as_deref().map(|s| s.len() + 1).unwrap_or(0) + player2.name.len();
|
||||
|
||||
// Prefix + name for each player
|
||||
|
||||
if let Some(pre) = prefix1 {
|
||||
print!("\x1b[2m{}\x1b[22m ", pre);
|
||||
}
|
||||
print!(
|
||||
"\x1b[4m\x1b]8;;https://www.start.gg/user/{}\x1b\\\
|
||||
\x1b[1m{}\x1b[22m\x1b]8;;\x1b\\\x1b[0m - ",
|
||||
discrim1, name1
|
||||
);
|
||||
if let Some(pre) = prefix2 {
|
||||
print!("\x1b[2m{}\x1b[22m ", pre);
|
||||
}
|
||||
println!(
|
||||
"\x1b[4m\x1b]8;;https://www.start.gg/user/{}\x1b\\\
|
||||
\x1b[1m{}\x1b[22m\x1b]8;;\x1b\\\x1b[0m",
|
||||
discrim2, name2
|
||||
);
|
||||
player_display(player1, false, false, false);
|
||||
print!(" - ");
|
||||
player_display(player2, false, false, true);
|
||||
|
||||
// Probability breakdown
|
||||
|
||||
|
|
@ -131,7 +126,7 @@ pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String
|
|||
if !hypothetical {
|
||||
// Set count
|
||||
|
||||
let (a, b) = get_matchup_set_counts(connection, &dataset, player1, player2)
|
||||
let (a, b) = get_matchup_set_counts(connection, &dataset, id1, id2)
|
||||
.expect("Error communicating with SQLite");
|
||||
|
||||
println!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue