From a387718b579ee64287d91134ed7a0db0fc4ab218 Mon Sep 17 00:00:00 2001 From: AINDUSTRIES Date: Thu, 10 Sep 2026 18:50:08 +0200 Subject: [PATCH] Updated the api --- .../20260831172036_create_table_skins.sql | 5 +- src/skins.rs | 118 +++++++++++------- 2 files changed, 75 insertions(+), 48 deletions(-) diff --git a/migrations/20260831172036_create_table_skins.sql b/migrations/20260831172036_create_table_skins.sql index 431a0d3..b02ed1a 100644 --- a/migrations/20260831172036_create_table_skins.sql +++ b/migrations/20260831172036_create_table_skins.sql @@ -21,7 +21,8 @@ CREATE TABLE IF NOT EXISTS skins ( display varchar NOT NULL, min_float float NOT NULL, max_float float NOT NULL, - legacy boolean NOT NULL + legacy boolean NOT NULL, + "default" boolean DEFAULT false NOT NULL ); CREATE TABLE IF NOT EXISTS agents ( @@ -32,4 +33,4 @@ CREATE TABLE IF NOT EXISTS agents ( def_index integer NOT NULL, team integer NOT NULL, -- 0 = CT, 1 = T, 2 = BOTH model_player varchar NOT NULl -); \ No newline at end of file +); diff --git a/src/skins.rs b/src/skins.rs index 55233f0..10514a3 100644 --- a/src/skins.rs +++ b/src/skins.rs @@ -91,66 +91,92 @@ pub async fn skin_info( Query(info_query): Query, ) -> Result>, AppError> { if let Some(uuid) = session.get::("uuid").await? { - let mut skins = query!("SELECT sk.id, w.team, w.category FROM user_skins sk left join skins s on s.id = sk.skin_id left join weapons w on w.id = s.weapon LEFT JOIN users us ON us.id = sk.user_id WHERE us.id = $1", uuid).fetch_all(&pool).await?; + // Fetch all default skins + let mut default_skins = query!("SELECT w.id, w.name, w.display, w.team, w.category, w.display as wd, s.display as sd, r.color FROM skins s JOIN weapons w ON w.id = s.weapon JOIN rarity r ON r.id = s.rarity WHERE s.default = true").fetch_all(&pool).await?; + // Filter those skins by team if present in query if let Some(team) = info_query.team { - skins = skins + default_skins = default_skins .into_iter() .filter(|x| team.as_i32() == x.team || Teams::BOTH.as_i32() == x.team) .collect(); } + // Same with category if let Some(category) = info_query.category { - skins = skins + default_skins = default_skins .into_iter() .filter(|x| category.as_str() == x.category) .collect(); } - let mut skin_infos = Vec::new(); - let ids: Vec = skins.iter().map(|x| x.id).collect(); - for id in ids { - let info = query!("SELECT w.name, w.display as wdisplay, s.display as sdisplay, s.paint_index, r.color, us.float, us.pattern FROM user_skins us JOIN skins s ON s.id = us.skin_id JOIN rarity r on r.id = s.rarity JOIN weapons w on w.id = s.weapon WHERE us.id = $1", id).fetch_one(&pool).await?; - let display = if info.sdisplay.is_empty() { - info.wdisplay + // For each remaining default weapon, see if the user has set a skin + // Secific cases for Knives and Gloves + let mut skin_infos = vec![]; + for default in default_skins { + struct Info { + id: Uuid, + float: f64, + pattern: i32, + team: i32, + name: String, + wd: String, + sd: String, + paint_index: i32, + color: String, + } + let user_skin = match default.category.as_str() { + "Gloves" | "Knives" => { + query_as!(Info, "SELECT us.id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.category = $2", uuid, default.category).fetch_all(&pool).await? + }, + _ => { + query_as!(Info, "SELECT us.id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.id = $2", uuid, default.id).fetch_all(&pool).await? + } + }; + let user_skin = if let Some(team) = info_query.team { + user_skin + .into_iter() + .filter(|x| x.team == team.as_i32() || x.team == Teams::BOTH.as_i32()) + .min_by(|x, y| x.team.cmp(&y.team)) + .unwrap_or(Info { + id: Uuid::nil(), + float: 0.0, + pattern: 0, + team: 2, + name: default.name, + wd: default.wd, + sd: default.sd, + paint_index: 0, + color: default.color, + }) } else { - format!("{} | {}", info.wdisplay, info.sdisplay) + user_skin + .into_iter() + .max_by(|x, y| x.team.cmp(&y.team)) + .unwrap_or(Info { + id: Uuid::nil(), + float: 0.0, + pattern: 0, + team: 2, + name: default.name, + wd: default.wd, + sd: default.sd, + paint_index: 0, + color: default.color, + }) }; - let skin_info = SkinInfo { - id, + let display = if user_skin.sd.is_empty() { + user_skin.wd + } else { + format!("{} | {}", user_skin.wd, user_skin.sd) + }; + let info = SkinInfo { display, - name: info.name, - paint_index: info.paint_index, - color: info.color, - float: info.float, - pattern: info.pattern, + id: user_skin.id, + name: user_skin.name, + paint_index: user_skin.paint_index, + color: user_skin.color, + float: user_skin.float, + pattern: user_skin.pattern, }; - skin_infos.push(skin_info); - } - if skin_infos.is_empty() { - // fetch default skins - let mut infos = query!("SELECT w.name, w.display, w.team, w.category, r.color FROM skins s JOIN rarity r ON r.id = s.rarity JOIN weapons w ON w.id = s.weapon WHERE s.paint_index = 0").fetch_all(&pool).await?; - if let Some(team) = info_query.team { - infos = infos - .into_iter() - .filter(|x| team.as_i32() == x.team) - .collect(); - } - if let Some(category) = info_query.category { - infos = infos - .into_iter() - .filter(|x| category.as_str() == x.category) - .collect(); - } - skin_infos = infos - .into_iter() - .map(|x| SkinInfo { - id: Uuid::nil(), - name: x.name, - display: x.display, - color: x.color, - paint_index: 0, - float: 0.0, - pattern: 0, - }) - .collect(); + skin_infos.push(info); } Ok(Json(skin_infos)) } else {