Updated the api
This commit is contained in:
@@ -21,7 +21,8 @@ CREATE TABLE IF NOT EXISTS skins (
|
|||||||
display varchar NOT NULL,
|
display varchar NOT NULL,
|
||||||
min_float float NOT NULL,
|
min_float float NOT NULL,
|
||||||
max_float float NOT NULL,
|
max_float float NOT NULL,
|
||||||
legacy boolean NOT NULL
|
legacy boolean NOT NULL,
|
||||||
|
"default" boolean DEFAULT false NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS agents (
|
CREATE TABLE IF NOT EXISTS agents (
|
||||||
|
|||||||
114
src/skins.rs
114
src/skins.rs
@@ -91,66 +91,92 @@ pub async fn skin_info(
|
|||||||
Query(info_query): Query<SkinInfoQuery>,
|
Query(info_query): Query<SkinInfoQuery>,
|
||||||
) -> Result<Json<Vec<SkinInfo>>, AppError> {
|
) -> Result<Json<Vec<SkinInfo>>, AppError> {
|
||||||
if let Some(uuid) = session.get::<Uuid>("uuid").await? {
|
if let Some(uuid) = session.get::<Uuid>("uuid").await? {
|
||||||
let mut skins = query!("SELECT sk.id, w.team, w.category FROM user_skins sk left join skins s on s.id = sk.skin_id left join weapons w on w.id = s.weapon LEFT JOIN users us ON us.id = sk.user_id WHERE us.id = $1", uuid).fetch_all(&pool).await?;
|
// Fetch all default skins
|
||||||
|
let mut default_skins = query!("SELECT w.id, w.name, w.display, w.team, w.category, w.display as wd, s.display as sd, r.color FROM skins s JOIN weapons w ON w.id = s.weapon JOIN rarity r ON r.id = s.rarity WHERE s.default = true").fetch_all(&pool).await?;
|
||||||
|
// Filter those skins by team if present in query
|
||||||
if let Some(team) = info_query.team {
|
if let Some(team) = info_query.team {
|
||||||
skins = skins
|
default_skins = default_skins
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| team.as_i32() == x.team || Teams::BOTH.as_i32() == x.team)
|
.filter(|x| team.as_i32() == x.team || Teams::BOTH.as_i32() == x.team)
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
// Same with category
|
||||||
if let Some(category) = info_query.category {
|
if let Some(category) = info_query.category {
|
||||||
skins = skins
|
default_skins = default_skins
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| category.as_str() == x.category)
|
.filter(|x| category.as_str() == x.category)
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
let mut skin_infos = Vec::new();
|
// For each remaining default weapon, see if the user has set a skin
|
||||||
let ids: Vec<Uuid> = skins.iter().map(|x| x.id).collect();
|
// Secific cases for Knives and Gloves
|
||||||
for id in ids {
|
let mut skin_infos = vec![];
|
||||||
let info = query!("SELECT w.name, w.display as wdisplay, s.display as sdisplay, s.paint_index, r.color, us.float, us.pattern FROM user_skins us JOIN skins s ON s.id = us.skin_id JOIN rarity r on r.id = s.rarity JOIN weapons w on w.id = s.weapon WHERE us.id = $1", id).fetch_one(&pool).await?;
|
for default in default_skins {
|
||||||
let display = if info.sdisplay.is_empty() {
|
struct Info {
|
||||||
info.wdisplay
|
id: Uuid,
|
||||||
} else {
|
float: f64,
|
||||||
format!("{} | {}", info.wdisplay, info.sdisplay)
|
pattern: i32,
|
||||||
|
team: i32,
|
||||||
|
name: String,
|
||||||
|
wd: String,
|
||||||
|
sd: String,
|
||||||
|
paint_index: i32,
|
||||||
|
color: String,
|
||||||
|
}
|
||||||
|
let user_skin = match default.category.as_str() {
|
||||||
|
"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, 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, 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 skin_info = SkinInfo {
|
let user_skin = if let Some(team) = info_query.team {
|
||||||
id,
|
user_skin
|
||||||
display,
|
|
||||||
name: info.name,
|
|
||||||
paint_index: info.paint_index,
|
|
||||||
color: info.color,
|
|
||||||
float: info.float,
|
|
||||||
pattern: info.pattern,
|
|
||||||
};
|
|
||||||
skin_infos.push(skin_info);
|
|
||||||
}
|
|
||||||
if skin_infos.is_empty() {
|
|
||||||
// fetch default skins
|
|
||||||
let mut infos = query!("SELECT w.name, w.display, w.team, w.category, r.color FROM skins s JOIN rarity r ON r.id = s.rarity JOIN weapons w ON w.id = s.weapon WHERE s.paint_index = 0").fetch_all(&pool).await?;
|
|
||||||
if let Some(team) = info_query.team {
|
|
||||||
infos = infos
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| team.as_i32() == x.team)
|
.filter(|x| x.team == team.as_i32() || x.team == Teams::BOTH.as_i32())
|
||||||
.collect();
|
.min_by(|x, y| x.team.cmp(&y.team))
|
||||||
}
|
.unwrap_or(Info {
|
||||||
if let Some(category) = info_query.category {
|
|
||||||
infos = infos
|
|
||||||
.into_iter()
|
|
||||||
.filter(|x| category.as_str() == x.category)
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
skin_infos = infos
|
|
||||||
.into_iter()
|
|
||||||
.map(|x| SkinInfo {
|
|
||||||
id: Uuid::nil(),
|
id: Uuid::nil(),
|
||||||
name: x.name,
|
|
||||||
display: x.display,
|
|
||||||
color: x.color,
|
|
||||||
paint_index: 0,
|
|
||||||
float: 0.0,
|
float: 0.0,
|
||||||
pattern: 0,
|
pattern: 0,
|
||||||
|
team: 2,
|
||||||
|
name: default.name,
|
||||||
|
wd: default.wd,
|
||||||
|
sd: default.sd,
|
||||||
|
paint_index: 0,
|
||||||
|
color: default.color,
|
||||||
})
|
})
|
||||||
.collect();
|
} else {
|
||||||
|
user_skin
|
||||||
|
.into_iter()
|
||||||
|
.max_by(|x, y| x.team.cmp(&y.team))
|
||||||
|
.unwrap_or(Info {
|
||||||
|
id: Uuid::nil(),
|
||||||
|
float: 0.0,
|
||||||
|
pattern: 0,
|
||||||
|
team: 2,
|
||||||
|
name: default.name,
|
||||||
|
wd: default.wd,
|
||||||
|
sd: default.sd,
|
||||||
|
paint_index: 0,
|
||||||
|
color: default.color,
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let display = if user_skin.sd.is_empty() {
|
||||||
|
user_skin.wd
|
||||||
|
} else {
|
||||||
|
format!("{} | {}", user_skin.wd, user_skin.sd)
|
||||||
|
};
|
||||||
|
let info = SkinInfo {
|
||||||
|
display,
|
||||||
|
id: user_skin.id,
|
||||||
|
name: user_skin.name,
|
||||||
|
paint_index: user_skin.paint_index,
|
||||||
|
color: user_skin.color,
|
||||||
|
float: user_skin.float,
|
||||||
|
pattern: user_skin.pattern,
|
||||||
|
};
|
||||||
|
skin_infos.push(info);
|
||||||
}
|
}
|
||||||
Ok(Json(skin_infos))
|
Ok(Json(skin_infos))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user