diff --git a/Cargo.lock b/Cargo.lock index cfb737c..a9228ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2454,6 +2454,7 @@ dependencies = [ "rocket_cors", "serde_json", "smithe_lib", + "startgg", "thiserror", "tokio", ] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 69a3292..b21c536 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -12,4 +12,5 @@ serde_json = "1" thiserror = "1" smithe_lib = { path = "../lib" } rocket-governor = "0.2.0-rc.1" -tokio = { version = "1", features = ["full"] } \ No newline at end of file +tokio = { version = "1", features = ["full"] } +startgg = { path = "../startgg_api/" } \ No newline at end of file diff --git a/backend/src/main.rs b/backend/src/main.rs index a5d9bd9..db1d2fa 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -10,7 +10,7 @@ use rocket::{ use rocket_cors::{AllowedHeaders, AllowedOrigins}; use rocket_governor::{Quota, RocketGovernable, RocketGovernor}; use smithe_lib::{ - player::{get_all_like, get_player, get_top_two_characters}, + player::{add_new_player_to_pidgtm_db, get_all_like, get_player, get_top_two_characters}, set::{ get_competitor_type, get_head_to_head_record, get_set_losses_by_dq, get_set_losses_without_dqs, get_set_wins_by_dq, get_set_wins_without_dqs, @@ -20,6 +20,10 @@ use smithe_lib::{ }; use thiserror::Error; +use startgg::queries::player_getter::{make_pidgtm_player_getter_query, PIDGTM_PlayerGetterVars}; + +use std::sync::{Arc, Mutex}; + pub const DEV_ADDRESS: &str = "http://localhost:8080/"; pub const DEV_ADDRESS_2: &str = "http://127.0.0.1:8080/"; pub const PROD_ADDRESS: &str = "http://smithe.net"; @@ -58,6 +62,14 @@ async fn view_player( id: i32, _limitguard: RocketGovernor<'_, RateLimitGuard>, ) -> Result { + //Try to update if can but still serve old data otherwise + if let Ok(player_data) = + make_pidgtm_player_getter_query(id, Arc::new(Mutex::new(PIDGTM_PlayerGetterVars::empty()))) + .await + { + add_new_player_to_pidgtm_db(&player_data.player.unwrap()).await?; + } + // insert player page view smithe_lib::player_page_views::insert_player_page_view(id) .await diff --git a/frontend/src/components/player_list.rs b/frontend/src/components/player_list.rs index 2471beb..da5575a 100644 --- a/frontend/src/components/player_list.rs +++ b/frontend/src/components/player_list.rs @@ -42,7 +42,7 @@ pub fn player_list(props: &Props) -> Html { loading.set(true); let endpoint = format!("{}/players/{}", env!("SERVER_ADDRESS"), gamer_tag); - let mut fetched_players: Vec = Request::get(&endpoint) + let fetched_players: Vec = Request::get(&endpoint) .send() .await .unwrap() @@ -50,8 +50,6 @@ pub fn player_list(props: &Props) -> Html { .await .unwrap(); - fetched_players.sort_by_key(|e| e.player_id); - total_pages.set((fetched_players.len() as f32 / PAGE_SIZE as f32).ceil() as usize); search_results.set(fetched_players.clone()); diff --git a/lib/src/player.rs b/lib/src/player.rs index 9816d2b..ec244db 100644 --- a/lib/src/player.rs +++ b/lib/src/player.rs @@ -18,7 +18,9 @@ use diesel::{ insert_into, prelude::*, result::{DatabaseErrorKind, Error as DieselError}, - sql_query, update, + sql_query, + sql_types::Text, + update, }; use smithe_database::{ db_models::empty_player_ids::EmptyPlayerId, schema::empty_player_ids::dsl::*, @@ -62,12 +64,24 @@ pub async fn get_highest_id_with_sets_between(start_id: i32, end_id: i32) -> Res pub async fn get_all_like(tag: &str) -> Result> { let processed_tag = tag.replace(' ', "%"); // ^^^ transform spaces into wildcards to make search more inclusive - let mut db_connection = smithe_database::connect().await?; - let matching_players: Vec = players - .filter(gamer_tag.ilike(format!("%{}%", processed_tag))) // case-insensitive like - .get_results::(&mut db_connection) - .await?; + + let query = diesel::sql_query("SELECT * FROM players WHERE gamer_tag ILIKE $1 ORDER BY LENGTH(gamer_tag) ASC, player_id ASC") + .bind::(format!("%{}%", processed_tag)); + let mut matching_players: Vec = query.get_results::(&mut db_connection).await?; + + match tag.parse::() { + Ok(pid) => { + if let Ok(exact_id_match) = get_player(pid).await { + matching_players.insert(0, exact_id_match); + } + } + Err(_) => { + if let Ok(exact_slug_match) = get_player_from_slug(&processed_tag).await { + matching_players.insert(0, exact_slug_match); + } + } + } Ok(matching_players) } @@ -708,6 +722,25 @@ mod tests { assert_eq!(res.unwrap().len(), 1); } + #[tokio::test] + #[cfg(feature = "skip_db_tests")] + async fn test_get_all_like_id() { + let res = get_all_like("1178271").await; + assert!(res.is_ok()); + assert_eq!( + res.unwrap()[0].user_slug, + format!("user/{}", DANTOTTO_PLAYER_SLUG) + ); + } + + #[tokio::test] + #[cfg(feature = "skip_db_tests")] + async fn test_get_all_like_slug() { + let res = get_all_like(DANTOTTO_PLAYER_SLUG).await; + assert!(res.is_ok()); + assert_eq!(res.unwrap()[0].player_id, DANTOTTO_PLAYER_ID); + } + #[tokio::test] #[cfg(feature = "skip_db_tests")] async fn test_get_player() { diff --git a/src/smithe/player.rs b/src/smithe/player.rs index f0630c6..d3ec5e4 100644 --- a/src/smithe/player.rs +++ b/src/smithe/player.rs @@ -59,8 +59,7 @@ pub async fn handle_id(id: &i32) -> Result<()> { 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 = get_all_like(tag).await?; - matching_players.sort_by_key(|e| e.player_id); + let matching_players: Vec = get_all_like(tag).await?; // cli display let selection = Select::with_theme(&ColorfulTheme::default())