Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding id and slug search to CLI #106

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
Ok(matching_players)
}

pub async fn get_player_from_slug(slug: &str) -> Result<Player> {
CERLgit marked this conversation as resolved.
Show resolved Hide resolved
let mut db_connection = smithe_database::connect().await?;
let db_user_slug = format!("user/{}", slug);
let matched_player = players
.filter(smithe_database::schema::players::user_slug.eq(db_user_slug))
.get_result::<Player>(&mut db_connection)
.await?;

Check warning on line 81 in lib/src/player.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/player.rs#L75-L81

Added lines #L75 - L81 were not covered by tests

Ok(matched_player)
}

Check warning on line 84 in lib/src/player.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/player.rs#L83-L84

Added lines #L83 - L84 were not covered by tests

pub async fn get_player(pid: i32) -> Result<Player> {
let mut db_connection = smithe_database::connect().await?;
let matched_player = players
Expand Down Expand Up @@ -533,6 +544,7 @@
use diesel_async::AsyncConnection;

const DANTOTTO_PLAYER_ID: i32 = 1178271;
const DANTOTTO_PLAYER_SLUG: &str = "566b1fb5";

#[tokio::test]
#[cfg(feature = "skip_db_tests")]
Expand Down Expand Up @@ -704,6 +716,14 @@
assert_eq!(res.unwrap().player_id, DANTOTTO_PLAYER_ID);
}

#[tokio::test]
#[cfg(feature = "skip_db_tests")]
async fn test_get_player_from_slug() {
let res = get_player_from_slug(DANTOTTO_PLAYER_SLUG).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().player_id, DANTOTTO_PLAYER_ID);
}

#[tokio::test]
#[cfg(feature = "skip_db_tests")]
async fn test_get_highest_id_with_sets_between() {
Expand Down
18 changes: 17 additions & 1 deletion src/bin/smithe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use smithe_lib::common::init_logger;
use std::str::FromStr;

use clap::{Parser, Subcommand};
use smithereens::smithe::{event::handle_event, player::handle_player};
use smithereens::smithe::{
event::handle_event, player::handle_id, player::handle_player, player::handle_slug,
};
use url::Url;

/// Smithereens, or Smithe, is a digested open-source data visualizer tool for your Smash results.
Expand All @@ -22,6 +24,18 @@ enum Commands {
tag: String,
},

/// Gets player related info (using id)
Id {
#[clap(value_parser)]
id: i32,
},

/// Gets player related info (using slug)
Slug {
#[clap(value_parser)]
slug: String,
},

/// Gets tournament related info
Event {
#[clap(value_parser)]
Expand All @@ -38,5 +52,7 @@ async fn main() -> Result<()> {
match &cli.command {
Commands::Player { tag } => handle_player(tag).await,
Commands::Event { url } => handle_event(Url::from_str(url)?).await,
Commands::Id { id } => handle_id(id).await,
Commands::Slug { slug } => handle_slug(slug).await,
}
}
50 changes: 36 additions & 14 deletions src/smithe/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use smithe_database::db_models::player::Player;

use smithe_lib::{
common::start_read_all_by_increment_execute_finish_maybe_cancel,
player::{execute, get_all_like},
player::{execute, get_all_like, get_player, get_player_from_slug},
set::{
get_all_from_player_id, get_competitor_type, get_last_completed_at, get_set_losses_by_dq,
get_set_losses_without_dqs, get_set_wins_by_dq, get_set_wins_without_dqs, get_winrate,
Expand All @@ -16,19 +16,7 @@ use startgg::queries::set_getter::{make_set_getter_query, SetGetterVars};

use dialoguer::{theme::ColorfulTheme, Select};

pub async fn handle_player(tag: &str) -> Result<()> {
tracing::info!("🔍 looking for players with tags similar to the provided one...");
let mut matching_players: Vec<Player> = get_all_like(tag).await?;
matching_players.sort_by_key(|e| e.player_id);

// cli display
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("❗ These players matched your search:")
.default(0)
.items(&matching_players[..])
.interact()?;
let selected_player = &matching_players[selection];

pub async fn handle_search(selected_player: &Player) -> Result<()> {
tracing::info!("🤔 checking if player is cached...");
let cache = get_all_from_player_id(selected_player.player_id).await?;
let updated_after = get_last_completed_at(cache);
Expand All @@ -53,6 +41,40 @@ pub async fn handle_player(tag: &str) -> Result<()> {
.await
}

pub async fn handle_slug(slug: &str) -> Result<()> {
tracing::info!("🔍 looking for player with slug provided...");
let selected_player: Player = get_player_from_slug(slug).await?;
handle_search(&selected_player).await?;

Ok(())
}

pub async fn handle_id(id: &i32) -> Result<()> {
tracing::info!("🔍 looking for player with id provided...");
let selected_player: Player = get_player(*id).await?;
handle_search(&selected_player).await?;

Ok(())
}

pub async fn handle_player(tag: &str) -> Result<()> {
tracing::info!("🔍 looking for players with tags similar to the provided one...");
let mut matching_players: Vec<Player> = get_all_like(tag).await?;
matching_players.sort_by_key(|e| e.player_id);

// cli display
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("❗ These players matched your search:")
.default(0)
.items(&matching_players[..])
.interact()?;

let selected_player = &matching_players[selection];
handle_search(selected_player).await?;

Ok(())
}

async fn finish(usgv: Arc<Mutex<SetGetterVars>>) -> Result<()> {
let pid = usgv.lock().unwrap().playerId;
println!(
Expand Down
Loading