Skip to content

Commit

Permalink
feat: add search for tech in claims
Browse files Browse the repository at this point in the history
  • Loading branch information
ResuBaka committed Nov 24, 2024
1 parent 7904596 commit ef4856b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 20 deletions.
25 changes: 16 additions & 9 deletions rust/api-server/api/src/claims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ pub(crate) async fn get_claim(
Some(tier) => Some(tier.clone()),
None => None,
};
let learned: Vec<i32> =
serde_json::from_value(claim_tech_state.learned.clone()).unwrap();
let learned: Vec<i64> = claim_tech_state.learned.clone();
claim.upgrades = learned
.iter()
.map(|id| {
Expand All @@ -202,7 +201,7 @@ pub(crate) async fn get_claim(
.iter()
.filter(|id| tier_upgrades_ids.contains(&(**id as i64)))
.map(|id| id.clone())
.collect::<Vec<i32>>();
.collect::<Vec<i64>>();

if found_tiers.len() > 0 {
claim.tier = tier_upgrades
Expand Down Expand Up @@ -352,16 +351,25 @@ pub(crate) async fn get_claim(
Ok(Json(claim))
}

#[derive(Deserialize)]
struct ListClaimsParams {
page: Option<u64>,
per_page: Option<u64>,
search: Option<String>,
research: Option<i32>,
running_upgrade: Option<bool>,
}

pub(crate) async fn list_claims(
state: State<AppState>,
Query(params): Query<Params>,
Query(params): Query<ListClaimsParams>,
) -> Result<Json<ClaimResponse>, (StatusCode, &'static str)> {
let page = params.page.unwrap_or(1);
let posts_per_page = params.per_page.unwrap_or(5);
let posts_per_page = params.per_page.unwrap_or(25);
let search = params.search;

let (claims, num_pages) =
QueryCore::find_claim_descriptions(&state.conn, page, posts_per_page, search)
QueryCore::find_claim_descriptions(&state.conn, page, posts_per_page, search, params.research, params.running_upgrade)
.await
.expect("Cannot find posts in page");

Expand Down Expand Up @@ -401,8 +409,7 @@ pub(crate) async fn list_claims(
Some(tier) => Some(tier.clone()),
None => None,
};
let learned: Vec<i32> =
serde_json::from_value(claim_tech_state.learned.clone()).unwrap();
let learned: Vec<i64> = claim_tech_state.learned.clone();
claim_description.upgrades = learned
.iter()
.map(|id| {
Expand All @@ -417,7 +424,7 @@ pub(crate) async fn list_claims(
.iter()
.filter(|id| tier_upgrades_ids.contains(&(**id as i64)))
.map(|id| id.clone())
.collect::<Vec<i32>>();
.collect::<Vec<i64>>();

if found_tiers.len() > 0 {
claim_description.tier = tier_upgrades
Expand Down
6 changes: 3 additions & 3 deletions rust/api-server/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ fn import_data(config: Config) {

let temp_config = config.clone();
tasks.push(tokio::spawn(skill_descriptions::import_job_skill_desc(temp_config)));

let temp_config = config.clone();
tasks.push(tokio::spawn(player_state::import_job_player_state(temp_config)));

let temp_config = config.clone();
tasks.push(tokio::spawn(player_state::import_job_player_username_state(temp_config)));

let temp_config = config.clone();
tasks.push(tokio::spawn(leaderboard::import_job_experience_state(temp_config)));

Expand Down
2 changes: 1 addition & 1 deletion rust/api-server/entity/src/claim_tech_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub entity_id: i64,
pub learned: Json,
pub learned: Vec<i64>,
pub researching: i32,
pub start_timestamp: i64,
pub scheduled_id: Json,
Expand Down
8 changes: 4 additions & 4 deletions rust/api-server/migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use sea_orm_migration::prelude::*;

mod m20220101_000001_create_table;
mod m20240727_170250_claim_tech;
mod m20220101_000001_player_state;
mod m20240727_170250_skill_desc;
mod m20240728_160123_vehicle_state;
mod m20240801_163734_changes_experience_state;

Expand All @@ -11,8 +11,8 @@ pub struct Migrator;
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20240727_170250_claim_tech::Migration),
Box::new(m20220101_000001_player_state::Migration),
Box::new(m20240727_170250_skill_desc::Migration),
Box::new(m20240728_160123_vehicle_state::Migration),
Box::new(m20240801_163734_changes_experience_state::Migration),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl MigrationTrait for Migration {
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(ClaimTechState::Learned).json().not_null())
.col(ColumnDef::new(ClaimTechState::Learned).array(ColumnType::BigInteger).not_null())
.col(
ColumnDef::new(ClaimTechState::Researching)
.integer()
Expand Down Expand Up @@ -220,7 +220,11 @@ impl MigrationTrait for Migration {
.drop_table(Table::drop().table(ClaimTechDesc::Table).to_owned())
.await
.expect("Dropping ClaimTechDesc table");
// Replace the sample below with your own migration scripts

manager
.drop_table(Table::drop().table(ClaimDescriptionState::Table).to_owned())
.await
.expect("Dropping ClaimDescriptionState table");

manager
.drop_table(Table::drop().table(ClaimTechState::Table).to_owned())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ impl MigrationTrait for Migration {
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table(ItemDesc::Table).to_owned()).await.expect("Dropping ItemDesc table");
manager.drop_table(Table::drop().table(CargoDesc::Table).to_owned()).await.expect("Dropping CargoDesc table");
manager.drop_table(Table::drop().table(Inventory::Table).to_owned()).await.expect("Dropping Inventory table");
manager.drop_table(Table::drop().table(DeployableState::Table).to_owned()).await.expect("Dropping DeployableState table");
manager.drop_table(Table::drop().table(BuildingState::Table).to_owned()).await.expect("Dropping BuildingState table");
manager.drop_table(Table::drop().table(BuildingDesc::Table).to_owned()).await.expect("Dropping BuildingDesc table");

manager
.drop_table(
Table::drop()
Expand Down
49 changes: 48 additions & 1 deletion rust/api-server/service/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ::entity::{
player_username_state, player_username_state::Entity as PlayerUsernameState,
};
use sea_orm::prelude::Decimal;
use sea_orm::sea_query::{Alias, Expr, MysqlQueryBuilder, PostgresQueryBuilder, Quote, SimpleExpr, SqliteQueryBuilder};
use sea_orm::sea_query::{Alias, Expr, ExprTrait, MysqlQueryBuilder, PgFunc, PostgresQueryBuilder, Quote, SimpleExpr, SqliteQueryBuilder};
use sea_orm::*;
use sea_orm::sea_query::extension::postgres::PgExpr;
use sea_orm::sqlx::RawSql;
Expand Down Expand Up @@ -263,15 +263,62 @@ impl Query {
page: u64,
per_page: u64,
search: Option<String>,
has_research: Option<i32>,
is_running_upgrade: Option<bool>,
) -> Result<(Vec<claim_description_state::Model>, ItemsAndPagesNumber), DbErr> {
// Setup paginator
let paginator = ClaimDescription::find()
.order_by_asc(claim_description_state::Column::EntityId)
.filter(claim_description_state::Column::Name.ne("Watchtower"))
.filter(claim_description_state::Column::OwnerPlayerEntityId.ne(0))
.apply_if(search, |query, value| match db.get_database_backend() {
DbBackend::Postgres => query.filter(Expr::col(claim_description_state::Column::Name).ilike(format!("%{}%", value))),
_ => unreachable!()
})
// Look at how to write this query so it works and seo-orm does not to make things I would not like it do to here.
// The query needs to look like this at the end: SELECT "entity_id" FROM "claim_tech_state" WHERE learned::jsonb @> '[500]';
.apply_if(has_research, |query, value| match db.get_database_backend() {
DbBackend::Postgres => {
query.filter(
Condition::any().add(claim_description_state::Column::EntityId.in_subquery(
sea_query::Query::select()
.column(claim_tech_state::Column::EntityId)
// .and_where(SimpleExpr::from(PgFunc::any(Expr::col(claim_tech_state::Column::Learned)).eq(
// value
// )))
.and_where(
Expr::eq(
Expr::val(value),
Expr::expr(PgFunc::any(Expr::col(claim_tech_state::Column::Learned))),
)
)
.from(claim_tech_state::Entity)
.to_owned()
)
)
)
},
_ => unreachable!()
})
.apply_if(is_running_upgrade, |query, value| match db.get_database_backend() {
DbBackend::Postgres => {
let where_query = if value {
claim_tech_state::Column::Researching.ne(0)
} else {
claim_tech_state::Column::Researching.eq(0)
};

query.filter(
Condition::any().add(claim_description_state::Column::EntityId.in_subquery(
sea_query::Query::select()
.column(claim_tech_state::Column::EntityId)
.and_where(where_query)
.from(claim_tech_state::Entity)
.to_owned()
)))
},
_ => unreachable!()
})
.paginate(db, per_page);
let num_pages = paginator.num_items_and_pages().await?;

Expand Down

0 comments on commit ef4856b

Please sign in to comment.