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

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)
}
}