From 74449fd78b5f6d5422805457106cd3763e047bfa Mon Sep 17 00:00:00 2001 From: AINDUSTRIES Date: Fri, 11 Sep 2026 00:04:07 +0200 Subject: [PATCH] Implemented agent api --- src/main.rs | 3 ++- src/skins.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 379f59a..fc8a868 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use tower_sessions_sqlx_store::sqlx::PgPool as SessionPgPool; mod error; mod skins; mod users; -use skins::{skin_info, weapons}; +use skins::{agent_info, skin_info, weapons}; use users::{login, logout, me, reload, update}; #[tokio::main] @@ -40,6 +40,7 @@ async fn main() -> Result<(), Box> { .route("/reload", get(reload)) .route("/weapons", get(weapons)) .route("/skin", get(skin_info)) + .route("/agent", get(agent_info)) .layer( CorsLayer::new() .allow_origin("http://localhost:5173".parse::().unwrap()) diff --git a/src/skins.rs b/src/skins.rs index 10514a3..88c4f97 100644 --- a/src/skins.rs +++ b/src/skins.rs @@ -1,5 +1,5 @@ use axum::{ - Json, + Json, debug_handler, extract::{Query, State}, }; use serde::{Deserialize, Serialize}; @@ -183,3 +183,44 @@ pub async fn skin_info( Err(AppError::Unauthorised) } } + +#[derive(Deserialize, Serialize)] +pub struct AgentInfoQuery { + team: Teams, +} + +#[debug_handler] +pub async fn agent_info( + State(pool): State, + session: Session, + Query(info_query): Query, +) -> Result, AppError> { + if let Some(uuid) = session.get::("uuid").await? { + let agent = query!("SELECT us.id, a.name, a.display, r.color FROM user_skins us JOIN agents a ON a.id = us.agent_id JOIN rarity r ON r.id = a.rarity WHERE us.user_id=$1 AND us.team=$2", uuid, info_query.team.as_i32()).fetch_optional(&pool).await?; + match agent { + Some(agent) => Ok(Json(SkinInfo { + id: agent.id, + name: agent.name, + display: agent.display, + paint_index: 0, + color: agent.color, + float: 0.0, + pattern: 0, + })), + None => { + let default = query!("SELECT a.name, a.display, r.color FROM agents a JOIN rarity r ON r.id = a.rarity WHERE a.team = $1 AND a.default = true", info_query.team.as_i32()).fetch_one(&pool).await?; + Ok(Json(SkinInfo { + id: Uuid::nil(), + name: default.name, + display: default.display, + paint_index: 0, + color: default.color, + float: 0.0, + pattern: 0, + })) + } + } + } else { + Err(AppError::Unauthorised) + } +}