Implemented agent api
This commit is contained in:
@@ -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())
|
||||||
|
|||||||
43
src/skins.rs
43
src/skins.rs
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user