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 crate::util::*;
|
||||||
use sqlite::*;
|
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) {
|
pub fn list(connection: &Connection, dataset: Option<String>, all: bool) {
|
||||||
if all {
|
if all {
|
||||||
} else {
|
} 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) {
|
pub fn info(connection: &Connection, dataset: Option<String>, player: String) {
|
||||||
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
||||||
|
|
||||||
let PlayerData {
|
let player = get_player_from_input(connection, player)
|
||||||
id,
|
|
||||||
name,
|
|
||||||
prefix,
|
|
||||||
discrim,
|
|
||||||
} = get_player_from_input(connection, player)
|
|
||||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
.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));
|
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||||
|
|
||||||
if let Some(pre) = prefix {
|
player_display(player, true, true, true);
|
||||||
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);
|
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"\n\x1b[1mSet Count:\x1b[0m {} - {} ({:.3}%)",
|
"\n\x1b[1mSet Count:\x1b[0m {} - {} ({:.3}%)",
|
||||||
|
|
@ -45,66 +62,44 @@ pub fn info(connection: &Connection, dataset: Option<String>, player: String) {
|
||||||
pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String, player2: String) {
|
pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String, player2: String) {
|
||||||
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
let dataset = dataset.unwrap_or_else(|| String::from("default"));
|
||||||
|
|
||||||
let PlayerData {
|
let player1 = get_player_from_input(connection, player1)
|
||||||
id: player1,
|
|
||||||
name: name1,
|
|
||||||
prefix: prefix1,
|
|
||||||
discrim: discrim1,
|
|
||||||
} = get_player_from_input(connection, player1)
|
|
||||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||||
|
let id1 = player1.id;
|
||||||
|
|
||||||
let PlayerData {
|
let player2 = get_player_from_input(connection, player2)
|
||||||
id: player2,
|
|
||||||
name: name2,
|
|
||||||
prefix: prefix2,
|
|
||||||
discrim: discrim2,
|
|
||||||
} = get_player_from_input(connection, player2)
|
|
||||||
.unwrap_or_else(|_| error("Could not find player", 1));
|
.unwrap_or_else(|_| error("Could not find player", 1));
|
||||||
|
let id2 = player2.id;
|
||||||
|
|
||||||
let (hypothetical, advantage, variance) =
|
let (hypothetical, advantage, variance) = get_network_data(connection, &dataset, id1, id2)
|
||||||
get_network_data(connection, &dataset, player1, player2)
|
.expect("Error communicating with SQLite")
|
||||||
.expect("Error communicating with SQLite")
|
.map(|(adv, var)| (false, adv, var))
|
||||||
.map(|(adv, var)| (false, adv, var))
|
.unwrap_or_else(|| {
|
||||||
.unwrap_or_else(|| {
|
let metadata = get_metadata(connection, &dataset)
|
||||||
let metadata = get_metadata(connection, &dataset)
|
.expect("Error communicating with SQLite")
|
||||||
.expect("Error communicating with SQLite")
|
.unwrap_or_else(|| error("Dataset not found", 1));
|
||||||
.unwrap_or_else(|| error("Dataset not found", 1));
|
let (adv, var) = hypothetical_advantage(
|
||||||
let (adv, var) = hypothetical_advantage(
|
connection,
|
||||||
connection,
|
&dataset,
|
||||||
&dataset,
|
player1.id,
|
||||||
player1,
|
player2.id,
|
||||||
player2,
|
metadata.decay_const,
|
||||||
metadata.decay_const,
|
)
|
||||||
)
|
.expect("Error communicating with SQLite");
|
||||||
.expect("Error communicating with SQLite");
|
(true, adv, var)
|
||||||
(true, adv, var)
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let probability = 1.0 / (1.0 + f64::exp(-advantage));
|
let probability = 1.0 / (1.0 + f64::exp(-advantage));
|
||||||
|
|
||||||
let (color, other_color) = ansi_num_color(advantage, 0.2, 2.0);
|
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 len1 = player1.prefix.as_deref().map(|s| s.len() + 1).unwrap_or(0) + player1.name.len();
|
||||||
let len2 = prefix2.as_deref().map(|s| s.len() + 1).unwrap_or(0) + name2.len();
|
let len2 = player2.prefix.as_deref().map(|s| s.len() + 1).unwrap_or(0) + player2.name.len();
|
||||||
|
|
||||||
// Prefix + name for each player
|
// Prefix + name for each player
|
||||||
|
|
||||||
if let Some(pre) = prefix1 {
|
player_display(player1, false, false, false);
|
||||||
print!("\x1b[2m{}\x1b[22m ", pre);
|
print!(" - ");
|
||||||
}
|
player_display(player2, false, false, true);
|
||||||
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
|
|
||||||
);
|
|
||||||
|
|
||||||
// Probability breakdown
|
// Probability breakdown
|
||||||
|
|
||||||
|
|
@ -131,7 +126,7 @@ pub fn matchup(connection: &Connection, dataset: Option<String>, player1: String
|
||||||
if !hypothetical {
|
if !hypothetical {
|
||||||
// Set count
|
// 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");
|
.expect("Error communicating with SQLite");
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue