Added prototype function for skin retrieval

This commit is contained in:
2026-09-03 02:02:55 +02:00
parent 7ef7983ee8
commit 8aac11e370
2 changed files with 112 additions and 0 deletions

View File

@@ -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<dyn Error>> {
.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::<HeaderValue>().unwrap())

107
src/skins.rs Normal file
View File

@@ -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<PgPool>) -> Result<Json<Vec<Weapon>>, 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<PgPool>,
session: Session,
Query(team): Query<TeamQuery>,
) -> Result<Json<WeaponInfo>, AppError> {
if let Some(uuid) = session.get::<Uuid>("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<PgPool>,
session: Session,
Query(team): Query<TeamQuery>,
) -> Result<Json<WeaponInfo>, AppError> {
if let Some(uuid) = session.get::<Uuid>("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)
}
}