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

Trigger a profile (e.g., tag, photo, etc.) update on visit. #114

Closed
wants to merge 9 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
tokio = { version = "1", features = ["full"] }
startgg = { path = "../startgg_api/" }
14 changes: 13 additions & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -58,6 +62,14 @@ async fn view_player(
id: i32,
_limitguard: RocketGovernor<'_, RateLimitGuard>,
) -> Result<String, Error> {
//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?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the name of this function is a bit confusing at this point 😅

}

// insert player page view
smithe_lib::player_page_views::insert_player_page_view(id)
.await
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/player_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ pub fn player_list(props: &Props) -> Html {
loading.set(true);

let endpoint = format!("{}/players/{}", env!("SERVER_ADDRESS"), gamer_tag);
let mut fetched_players: Vec<Player> = Request::get(&endpoint)
let fetched_players: Vec<Player> = Request::get(&endpoint)
.send()
.await
.unwrap()
.json()
.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());

Expand Down
45 changes: 39 additions & 6 deletions lib/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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::*,
Expand Down Expand Up @@ -62,12 +64,24 @@
pub async fn get_all_like(tag: &str) -> Result<Vec<Player>> {
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<Player> = players
.filter(gamer_tag.ilike(format!("%{}%", processed_tag))) // case-insensitive like
.get_results::<Player>(&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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk why GitHub is showing this as a change considering you only changed this in your old PR and main looks like:

let query = diesel::sql_query("SELECT * FROM players WHERE gamer_tag ILIKE $1 ORDER BY LENGTH(gamer_tag) ASC, player_id ASC")
🤔

.bind::<Text, _>(format!("%{}%", processed_tag));
let mut matching_players: Vec<Player> = query.get_results::<Player>(&mut db_connection).await?;

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

View check run for this annotation

Codecov / codecov/patch

lib/src/player.rs#L69-L71

Added lines #L69 - L71 were not covered by tests

match tag.parse::<i32>() {
Ok(pid) => {
if let Ok(exact_id_match) = get_player(pid).await {
matching_players.insert(0, exact_id_match);
}

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

View check run for this annotation

Codecov / codecov/patch

lib/src/player.rs#L73-L77

Added lines #L73 - L77 were not covered by tests
}
Err(_) => {
if let Ok(exact_slug_match) = get_player_from_slug(&processed_tag).await {
matching_players.insert(0, exact_slug_match);
}

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

View check run for this annotation

Codecov / codecov/patch

lib/src/player.rs#L80-L82

Added lines #L80 - L82 were not covered by tests
}
}

Ok(matching_players)
}
Expand Down Expand Up @@ -708,6 +722,25 @@
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() {
Expand Down
3 changes: 1 addition & 2 deletions src/smithe/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player> = get_all_like(tag).await?;
matching_players.sort_by_key(|e| e.player_id);
let matching_players: Vec<Player> = get_all_like(tag).await?;

// cli display
let selection = Select::with_theme(&ColorfulTheme::default())
Expand Down
Loading