Skip to content

Commit

Permalink
Adding id and slug search to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
CERLgit committed Feb 22, 2024
1 parent 7f1864a commit b1efa80
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
11 changes: 11 additions & 0 deletions lib/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ pub async fn get_all_like(tag: &str) -> Result<Vec<Player>> {
Ok(matching_players)
}

pub async fn get_player_from_slug(slug: &str) -> Result<Player> {
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?;

Ok(matched_player)
}

pub async fn get_player(pid: i32) -> Result<Player> {
let mut db_connection = smithe_database::connect().await?;
let matched_player = players
Expand Down
16 changes: 15 additions & 1 deletion src/bin/smithe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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_player, player::handle_id, 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 +22,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 +50,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

0 comments on commit b1efa80

Please sign in to comment.