From 8aac11e3704edc62b29fbea999bff8a32b3e8b8a Mon Sep 17 00:00:00 2001 From: AINDUSTRIES Date: Thu, 3 Sep 2026 02:02:55 +0200 Subject: [PATCH] Added prototype function for skin retrieval --- src/main.rs | 5 +++ src/skins.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/skins.rs diff --git a/src/main.rs b/src/main.rs index 1fa6442..1b11db0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,9 @@ use tower_sessions_sqlx_store::PostgresStore; use tower_sessions_sqlx_store::sqlx::PgPool as SessionPgPool; mod error; +mod skins; mod users; +use skins::{gloves, knife, weapons}; use users::{login, logout, me, reload, update}; #[tokio::main] @@ -36,6 +38,9 @@ async fn main() -> Result<(), Box> { .route("/logout", get(logout)) .route("/update", post(update)) .route("/reload", get(reload)) + .route("/weapons", get(weapons)) + .route("/knife", get(knife)) + .route("/gloves", get(gloves)) .layer( CorsLayer::new() .allow_origin("http://localhost:5173".parse::().unwrap()) diff --git a/src/skins.rs b/src/skins.rs new file mode 100644 index 0000000..eb8ab64 --- /dev/null +++ b/src/skins.rs @@ -0,0 +1,107 @@ +use std::error::Error; + +use axum::{ + Json, + extract::{Query, State}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use sqlx::{PgPool, query, query_as, types::Uuid}; +use tower_sessions::Session; + +use crate::error::AppError; + +#[derive(Deserialize, Serialize)] +pub struct Weapon { + id: Uuid, + name: String, + display: String, + def_index: i32, + category: String, + team: i32, +} + +#[derive(Deserialize, Serialize)] +pub struct WeaponInfo { + id: Uuid, + name: String, + paint_index: i32, + color: String, +} + +#[axum::debug_handler] +pub async fn weapons(State(pool): State) -> Result>, AppError> { + let weapons = query_as!(Weapon, "SELECT * FROM weapons") + .fetch_all(&pool) + .await?; + Ok(Json(weapons)) +} + +#[derive(Deserialize, Serialize)] +pub struct TeamQuery { + team: String, +} + +pub async fn knife( + State(pool): State, + session: Session, + Query(team): Query, +) -> Result, AppError> { + if let Some(uuid) = session.get::("uuid").await? { + let team = match team.team.as_str() { + "CT" => 0, + "T" => 1, + _ => 2, + }; + let knife = match query_as!(WeaponInfo, "SELECT us.id, wp.name, sk.paint_index, r.color FROM user_skins us LEFT JOIN skins sk ON sk.id = us.skin_id LEFT JOIN rarity r ON r.id = sk.rarity LEFT JOIN weapons wp ON wp.id = sk.weapon WHERE us.user_id = $1 AND us.team = $2 AND wp.category='Knives'", uuid, team).fetch_optional(&pool).await? { + Some(knife) => knife, + None => { + let name = match team { + 0 => "weapon_knife", + _ => "weapon_knife_t" + }; + let info = query!("SELECT wp.name, r.color FROM weapons wp LEFT JOIN skins sk ON sk.weapon = wp.id LEFT JOIN rarity r ON r.id = sk.rarity WHERE wp.name = $1", name).fetch_one(&pool).await?; + WeaponInfo { + id: Uuid::nil(), + name: info.name, + paint_index: 0, + color: info.color + }} + }; + Ok(Json(knife)) + } else { + Err(AppError::Unauthorised) + } +} + +pub async fn gloves( + State(pool): State, + session: Session, + Query(team): Query, +) -> Result, AppError> { + if let Some(uuid) = session.get::("uuid").await? { + let team = match team.team.as_str() { + "CT" => 0, + "T" => 1, + _ => 2, + }; + let gloves = match query_as!(WeaponInfo, "SELECT us.id, wp.name, sk.paint_index, r.color FROM user_skins us LEFT JOIN skins sk ON sk.id = us.skin_id LEFT JOIN rarity r ON r.id = sk.rarity LEFT JOIN weapons wp ON wp.id = sk.weapon WHERE us.user_id = $1 AND us.team = $2 AND wp.category = 'Gloves'", uuid, team).fetch_optional(&pool).await? { + Some(gloves) => gloves, + None => { + let name = match team { + 0 => "ct_gloves", + _ => "t_gloves" + }; + let info = query!("SELECT wp.name, r.color FROM weapons wp LEFT JOIN skins sk ON sk.weapon = wp.id LEFT JOIN rarity r ON r.id = sk.rarity WHERE wp.name = $1", name).fetch_one(&pool).await?; + WeaponInfo { + id: Uuid::nil(), + name: info.name, + paint_index: 0, + color: info.color + }} + }; + Ok(Json(gloves)) + } else { + Err(AppError::Unauthorised) + } +}