Added ability to fetch all paint indexes of skins (or ids for agents)
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::{agent_info, skin_info, weapons};
|
use skins::{agent_info, skin_info, weapons, list_paint_index};
|
||||||
use users::{login, logout, me, reload, update};
|
use users::{login, logout, me, reload, update};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -41,6 +41,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.route("/weapons", get(weapons))
|
.route("/weapons", get(weapons))
|
||||||
.route("/skin", get(skin_info))
|
.route("/skin", get(skin_info))
|
||||||
.route("/agent", get(agent_info))
|
.route("/agent", get(agent_info))
|
||||||
|
.route("/index", get(list_paint_index))
|
||||||
.layer(
|
.layer(
|
||||||
CorsLayer::new()
|
CorsLayer::new()
|
||||||
.allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap())
|
.allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap())
|
||||||
|
|||||||
56
src/skins.rs
56
src/skins.rs
@@ -21,6 +21,7 @@ pub struct Weapon {
|
|||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct SkinInfo {
|
pub struct SkinInfo {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
|
weapon_id: Uuid,
|
||||||
name: String,
|
name: String,
|
||||||
display: String,
|
display: String,
|
||||||
paint_index: i32,
|
paint_index: i32,
|
||||||
@@ -115,6 +116,7 @@ pub async fn skin_info(
|
|||||||
for default in default_skins {
|
for default in default_skins {
|
||||||
struct Info {
|
struct Info {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
|
weapon_id: Uuid,
|
||||||
float: f64,
|
float: f64,
|
||||||
min_float: f64,
|
min_float: f64,
|
||||||
max_float: f64,
|
max_float: f64,
|
||||||
@@ -128,10 +130,10 @@ pub async fn skin_info(
|
|||||||
}
|
}
|
||||||
let user_skin = match default.category.as_str() {
|
let user_skin = match default.category.as_str() {
|
||||||
"Gloves" | "Knives" => {
|
"Gloves" | "Knives" => {
|
||||||
query_as!(Info, "SELECT us.id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, s.min_float, s.max_float, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.category = $2", uuid, default.category).fetch_all(&pool).await?
|
query_as!(Info, "SELECT us.id, w.id as weapon_id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, s.min_float, s.max_float, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.category = $2", uuid, default.category).fetch_all(&pool).await?
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
query_as!(Info, "SELECT us.id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, s.min_float, s.max_float, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.id = $2", uuid, default.id).fetch_all(&pool).await?
|
query_as!(Info, "SELECT us.id, w.id as weapon_id, us.float, us.pattern, us.team, w.name, w.display as wd, s.display as sd, s.paint_index, s.min_float, s.max_float, r.color FROM user_skins us JOIN skins s ON us.skin_id = s.id JOIN weapons w ON s.weapon = w.id JOIN rarity r ON r.id = s.rarity WHERE us.user_id = $1 AND w.id = $2", uuid, default.id).fetch_all(&pool).await?
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let user_skin = if let Some(team) = info_query.team {
|
let user_skin = if let Some(team) = info_query.team {
|
||||||
@@ -141,6 +143,7 @@ pub async fn skin_info(
|
|||||||
.min_by(|x, y| x.team.cmp(&y.team))
|
.min_by(|x, y| x.team.cmp(&y.team))
|
||||||
.unwrap_or(Info {
|
.unwrap_or(Info {
|
||||||
id: Uuid::nil(),
|
id: Uuid::nil(),
|
||||||
|
weapon_id: default.id,
|
||||||
float: 0.0,
|
float: 0.0,
|
||||||
min_float: 0.0,
|
min_float: 0.0,
|
||||||
max_float: 1.0,
|
max_float: 1.0,
|
||||||
@@ -158,6 +161,7 @@ pub async fn skin_info(
|
|||||||
.max_by(|x, y| x.team.cmp(&y.team))
|
.max_by(|x, y| x.team.cmp(&y.team))
|
||||||
.unwrap_or(Info {
|
.unwrap_or(Info {
|
||||||
id: Uuid::nil(),
|
id: Uuid::nil(),
|
||||||
|
weapon_id: default.id,
|
||||||
float: 0.0,
|
float: 0.0,
|
||||||
min_float: 0.0,
|
min_float: 0.0,
|
||||||
max_float: 1.0,
|
max_float: 1.0,
|
||||||
@@ -178,6 +182,7 @@ pub async fn skin_info(
|
|||||||
let info = SkinInfo {
|
let info = SkinInfo {
|
||||||
display,
|
display,
|
||||||
id: user_skin.id,
|
id: user_skin.id,
|
||||||
|
weapon_id: user_skin.weapon_id,
|
||||||
name: user_skin.name,
|
name: user_skin.name,
|
||||||
paint_index: user_skin.paint_index,
|
paint_index: user_skin.paint_index,
|
||||||
color: user_skin.color,
|
color: user_skin.color,
|
||||||
@@ -206,10 +211,11 @@ pub async fn agent_info(
|
|||||||
Query(info_query): Query<AgentInfoQuery>,
|
Query(info_query): Query<AgentInfoQuery>,
|
||||||
) -> Result<Json<SkinInfo>, AppError> {
|
) -> Result<Json<SkinInfo>, AppError> {
|
||||||
if let Some(uuid) = session.get::<Uuid>("uuid").await? {
|
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?;
|
let agent = query!("SELECT us.id, a.id as weapon_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 {
|
match agent {
|
||||||
Some(agent) => Ok(Json(SkinInfo {
|
Some(agent) => Ok(Json(SkinInfo {
|
||||||
id: agent.id,
|
id: agent.id,
|
||||||
|
weapon_id: agent.weapon_id,
|
||||||
name: agent.name,
|
name: agent.name,
|
||||||
display: agent.display,
|
display: agent.display,
|
||||||
paint_index: 0,
|
paint_index: 0,
|
||||||
@@ -220,9 +226,10 @@ pub async fn agent_info(
|
|||||||
pattern: 0,
|
pattern: 0,
|
||||||
})),
|
})),
|
||||||
None => {
|
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?;
|
let default = query!("SELECT a.name, a.id as weapon_id, 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 {
|
Ok(Json(SkinInfo {
|
||||||
id: Uuid::nil(),
|
id: Uuid::nil(),
|
||||||
|
weapon_id: default.weapon_id,
|
||||||
name: default.name,
|
name: default.name,
|
||||||
display: default.display,
|
display: default.display,
|
||||||
paint_index: 0,
|
paint_index: 0,
|
||||||
@@ -238,3 +245,44 @@ pub async fn agent_info(
|
|||||||
Err(AppError::Unauthorised)
|
Err(AppError::Unauthorised)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Default, Debug)]
|
||||||
|
pub struct PaintIndexInfo {
|
||||||
|
name: String,
|
||||||
|
paint_index: i32,
|
||||||
|
display: String,
|
||||||
|
color: String,
|
||||||
|
min_float: f64,
|
||||||
|
max_float: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct PaintQuery {
|
||||||
|
weapon: Uuid,
|
||||||
|
team: Teams,
|
||||||
|
agent: Option<bool>
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_paint_index(
|
||||||
|
State(pool): State<PgPool>,
|
||||||
|
Query(paint_query): Query<PaintQuery>,
|
||||||
|
) -> Result<Json<Vec<PaintIndexInfo>>, AppError> {
|
||||||
|
let agent = match paint_query.agent {
|
||||||
|
Some(agent) => agent,
|
||||||
|
None => false
|
||||||
|
};
|
||||||
|
let paints = if agent {
|
||||||
|
let values = query!("SELECT a.name, a.def_index, a.display, r.color FROM agents a JOIN rarity r ON r.id = a.rarity WHERE a.team=$1", paint_query.team.as_i32()).fetch_all(&pool).await?;
|
||||||
|
values.into_iter().map(|x| PaintIndexInfo {
|
||||||
|
name: x.name,
|
||||||
|
paint_index: x.def_index,
|
||||||
|
display: x.display,
|
||||||
|
color: x.color,
|
||||||
|
min_float: 0.0,
|
||||||
|
max_float: 0.0
|
||||||
|
}).collect()
|
||||||
|
} else {
|
||||||
|
query_as!(PaintIndexInfo, "SELECT w.name, s.paint_index, s.display, r.color, s.min_float, s.max_float FROM skins s JOIN rarity r ON r.id = s.rarity JOIN weapons w ON w.id = s.weapon WHERE s.weapon=$1", paint_query.weapon).fetch_all(&pool).await?
|
||||||
|
};
|
||||||
|
Ok(Json(paints))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user