Compare commits

..

2 Commits

Author SHA1 Message Date
74449fd78b Implemented agent api 2026-09-11 00:04:07 +02:00
a8434a5c9b Added default attribute for agents 2026-09-11 00:03:53 +02:00
3 changed files with 46 additions and 3 deletions

View File

@@ -32,5 +32,6 @@ CREATE TABLE IF NOT EXISTS agents (
display varchar NOT NULL, display varchar NOT NULL,
def_index integer NOT NULL, def_index integer NOT NULL,
team integer NOT NULL, -- 0 = CT, 1 = T, 2 = BOTH team integer NOT NULL, -- 0 = CT, 1 = T, 2 = BOTH
model_player varchar NOT NULl model_player varchar NOT NULL,
"default" boolean DEFAULT false NOT NULL
); );

View File

@@ -17,7 +17,7 @@ use tower_sessions_sqlx_store::sqlx::PgPool as SessionPgPool;
mod error; mod error;
mod skins; mod skins;
mod users; mod users;
use skins::{skin_info, weapons}; use skins::{agent_info, skin_info, weapons};
use users::{login, logout, me, reload, update}; use users::{login, logout, me, reload, update};
#[tokio::main] #[tokio::main]
@@ -40,6 +40,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.route("/reload", get(reload)) .route("/reload", get(reload))
.route("/weapons", get(weapons)) .route("/weapons", get(weapons))
.route("/skin", get(skin_info)) .route("/skin", get(skin_info))
.route("/agent", get(agent_info))
.layer( .layer(
CorsLayer::new() CorsLayer::new()
.allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap()) .allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap())

View File

@@ -1,5 +1,5 @@
use axum::{ use axum::{
Json, Json, debug_handler,
extract::{Query, State}, extract::{Query, State},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -183,3 +183,44 @@ pub async fn skin_info(
Err(AppError::Unauthorised) Err(AppError::Unauthorised)
} }
} }
#[derive(Deserialize, Serialize)]
pub struct AgentInfoQuery {
team: Teams,
}
#[debug_handler]
pub async fn agent_info(
State(pool): State<PgPool>,
session: Session,
Query(info_query): Query<AgentInfoQuery>,
) -> Result<Json<SkinInfo>, AppError> {
if let Some(uuid) = session.get::<Uuid>("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)
}
}