Compare commits
10 Commits
8aac11e370
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e7f5f393a5 | |||
| 746b01000c | |||
| 84d44f9535 | |||
| 6df96285a2 | |||
| 464fbde1b9 | |||
| 74449fd78b | |||
| a8434a5c9b | |||
| a387718b57 | |||
| 3b63fe94a7 | |||
| 404031f771 |
@@ -21,7 +21,8 @@ CREATE TABLE IF NOT EXISTS skins (
|
||||
display varchar NOT NULL,
|
||||
min_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 (
|
||||
@@ -31,5 +32,6 @@ CREATE TABLE IF NOT EXISTS agents (
|
||||
display varchar NOT NULL,
|
||||
def_index integer NOT NULL,
|
||||
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
|
||||
);
|
||||
|
||||
@@ -3,5 +3,7 @@ CREATE TABLE IF NOT EXISTS user_skins (
|
||||
user_id uuid REFERENCES users(id) NOT NULL,
|
||||
skin_id uuid REFERENCES skins(id),
|
||||
agent_id uuid REFERENCES agents(id),
|
||||
float float DEFAULT 0 NOT NULL,
|
||||
pattern integer DEFAULT 0 NOT NULL,
|
||||
team integer NOT NULL -- 0 = CT, 1 = T, 2 = BOTH
|
||||
);
|
||||
11
src/main.rs
11
src/main.rs
@@ -17,7 +17,7 @@ use tower_sessions_sqlx_store::sqlx::PgPool as SessionPgPool;
|
||||
mod error;
|
||||
mod skins;
|
||||
mod users;
|
||||
use skins::{gloves, knife, weapons};
|
||||
use skins::{agent_info, list_paint_index, skin_info, weapons};
|
||||
use users::{login, logout, me, reload, update};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -39,8 +39,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
.route("/update", post(update))
|
||||
.route("/reload", get(reload))
|
||||
.route("/weapons", get(weapons))
|
||||
.route("/knife", get(knife))
|
||||
.route("/gloves", get(gloves))
|
||||
.route("/skin", get(skin_info))
|
||||
.route("/agent", get(agent_info))
|
||||
.route("/index", get(list_paint_index))
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin("http://localhost:5173".parse::<HeaderValue>().unwrap())
|
||||
@@ -50,10 +51,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
)
|
||||
.layer(session)
|
||||
.with_state(pool);
|
||||
let listener = TcpListener::bind("0.0.0.0:8080").await?;
|
||||
let listener = TcpListener::bind(var("BIND_ADDRESS")?).await?;
|
||||
Ok(serve(listener, app).await?)
|
||||
}
|
||||
|
||||
async fn status() -> &'static str {
|
||||
"api online"
|
||||
"API online"
|
||||
}
|
||||
|
||||
312
src/skins.rs
312
src/skins.rs
@@ -1,11 +1,8 @@
|
||||
use std::error::Error;
|
||||
|
||||
use axum::{
|
||||
Json,
|
||||
Json, debug_handler,
|
||||
extract::{Query, State},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use sqlx::{PgPool, query, query_as, types::Uuid};
|
||||
use tower_sessions::Session;
|
||||
|
||||
@@ -22,11 +19,17 @@ pub struct Weapon {
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct WeaponInfo {
|
||||
pub struct SkinInfo {
|
||||
id: Uuid,
|
||||
weapon_id: Uuid,
|
||||
name: String,
|
||||
display: String,
|
||||
paint_index: i32,
|
||||
color: String,
|
||||
float: f64,
|
||||
min_float: f64,
|
||||
max_float: f64,
|
||||
pattern: i32,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -37,71 +40,272 @@ pub async fn weapons(State(pool): State<PgPool>) -> Result<Json<Vec<Weapon>>, Ap
|
||||
Ok(Json(weapons))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
|
||||
enum Teams {
|
||||
CT,
|
||||
T,
|
||||
BOTH,
|
||||
}
|
||||
|
||||
impl Teams {
|
||||
fn as_i32(self) -> i32 {
|
||||
match self {
|
||||
Teams::CT => 0,
|
||||
Teams::T => 1,
|
||||
Teams::BOTH => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
|
||||
enum Categories {
|
||||
Knives,
|
||||
Gloves,
|
||||
Rifles,
|
||||
SMGs,
|
||||
Pistols,
|
||||
Heavy,
|
||||
Equipment,
|
||||
Weapons, // Special category to get all weapons except gloves and knives
|
||||
}
|
||||
|
||||
impl Categories {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Categories::Knives => "Knives",
|
||||
Categories::Gloves => "Gloves",
|
||||
Categories::Rifles => "Rifles",
|
||||
Categories::SMGs => "SMGs",
|
||||
Categories::Pistols => "Pistols",
|
||||
Categories::Heavy => "Heavy",
|
||||
Categories::Equipment => "Equipment",
|
||||
Categories::Weapons => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct TeamQuery {
|
||||
team: String,
|
||||
pub struct SkinInfoQuery {
|
||||
category: Option<Categories>,
|
||||
team: Option<Teams>,
|
||||
}
|
||||
|
||||
pub async fn knife(
|
||||
pub async fn skin_info(
|
||||
State(pool): State<PgPool>,
|
||||
session: Session,
|
||||
Query(team): Query<TeamQuery>,
|
||||
) -> Result<Json<WeaponInfo>, AppError> {
|
||||
Query(info_query): Query<SkinInfoQuery>,
|
||||
) -> Result<Json<Vec<SkinInfo>>, AppError> {
|
||||
if let Some(uuid) = session.get::<Uuid>("uuid").await? {
|
||||
let team = match team.team.as_str() {
|
||||
"CT" => 0,
|
||||
"T" => 1,
|
||||
_ => 2,
|
||||
};
|
||||
let knife = match query_as!(WeaponInfo, "SELECT us.id, wp.name, sk.paint_index, r.color FROM user_skins us LEFT JOIN skins sk ON sk.id = us.skin_id LEFT JOIN rarity r ON r.id = sk.rarity LEFT JOIN weapons wp ON wp.id = sk.weapon WHERE us.user_id = $1 AND us.team = $2 AND wp.category='Knives'", uuid, team).fetch_optional(&pool).await? {
|
||||
Some(knife) => knife,
|
||||
None => {
|
||||
let name = match team {
|
||||
0 => "weapon_knife",
|
||||
_ => "weapon_knife_t"
|
||||
};
|
||||
let info = query!("SELECT wp.name, r.color FROM weapons wp LEFT JOIN skins sk ON sk.weapon = wp.id LEFT JOIN rarity r ON r.id = sk.rarity WHERE wp.name = $1", name).fetch_one(&pool).await?;
|
||||
WeaponInfo {
|
||||
id: Uuid::nil(),
|
||||
name: info.name,
|
||||
paint_index: 0,
|
||||
color: info.color
|
||||
}}
|
||||
};
|
||||
Ok(Json(knife))
|
||||
// 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 {
|
||||
default_skins = default_skins
|
||||
.into_iter()
|
||||
.filter(|x| team.as_i32() == x.team || Teams::BOTH.as_i32() == x.team)
|
||||
.collect();
|
||||
}
|
||||
// Same with category
|
||||
if let Some(category) = info_query.category {
|
||||
default_skins = match category {
|
||||
Categories::Weapons => default_skins
|
||||
.into_iter()
|
||||
.filter(|x| {
|
||||
![Categories::Gloves.as_str(), Categories::Knives.as_str()]
|
||||
.contains(&x.category.as_str())
|
||||
})
|
||||
.collect(),
|
||||
_ => default_skins
|
||||
.into_iter()
|
||||
.filter(|x| category.as_str() == x.category)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
// For each remaining default weapon, see if the user has set a skin
|
||||
// Secific cases for Knives and Gloves
|
||||
let mut skin_infos = vec![];
|
||||
for default in default_skins {
|
||||
struct Info {
|
||||
id: Uuid,
|
||||
weapon_id: Uuid,
|
||||
float: f64,
|
||||
min_float: f64,
|
||||
max_float: f64,
|
||||
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, 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, 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 {
|
||||
user_skin
|
||||
.into_iter()
|
||||
.filter(|x| x.team == team.as_i32() || x.team == Teams::BOTH.as_i32())
|
||||
.min_by(|x, y| x.team.cmp(&y.team))
|
||||
.unwrap_or(Info {
|
||||
id: Uuid::nil(),
|
||||
weapon_id: default.id,
|
||||
float: 0.0,
|
||||
min_float: 0.0,
|
||||
max_float: 1.0,
|
||||
pattern: 0,
|
||||
team: 2,
|
||||
name: default.name,
|
||||
wd: default.wd,
|
||||
sd: default.sd,
|
||||
paint_index: 0,
|
||||
color: default.color,
|
||||
})
|
||||
} else {
|
||||
user_skin
|
||||
.into_iter()
|
||||
.max_by(|x, y| x.team.cmp(&y.team))
|
||||
.unwrap_or(Info {
|
||||
id: Uuid::nil(),
|
||||
weapon_id: default.id,
|
||||
float: 0.0,
|
||||
min_float: 0.0,
|
||||
max_float: 1.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,
|
||||
weapon_id: user_skin.weapon_id,
|
||||
name: user_skin.name,
|
||||
paint_index: user_skin.paint_index,
|
||||
color: user_skin.color,
|
||||
float: user_skin.float,
|
||||
min_float: user_skin.min_float,
|
||||
max_float: user_skin.max_float,
|
||||
pattern: user_skin.pattern,
|
||||
};
|
||||
skin_infos.push(info);
|
||||
}
|
||||
Ok(Json(skin_infos))
|
||||
} else {
|
||||
Err(AppError::Unauthorised)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn gloves(
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct AgentInfoQuery {
|
||||
team: Teams,
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn agent_info(
|
||||
State(pool): State<PgPool>,
|
||||
session: Session,
|
||||
Query(team): Query<TeamQuery>,
|
||||
) -> Result<Json<WeaponInfo>, AppError> {
|
||||
Query(info_query): Query<AgentInfoQuery>,
|
||||
) -> Result<Json<SkinInfo>, AppError> {
|
||||
if let Some(uuid) = session.get::<Uuid>("uuid").await? {
|
||||
let team = match team.team.as_str() {
|
||||
"CT" => 0,
|
||||
"T" => 1,
|
||||
_ => 2,
|
||||
};
|
||||
let gloves = match query_as!(WeaponInfo, "SELECT us.id, wp.name, sk.paint_index, r.color FROM user_skins us LEFT JOIN skins sk ON sk.id = us.skin_id LEFT JOIN rarity r ON r.id = sk.rarity LEFT JOIN weapons wp ON wp.id = sk.weapon WHERE us.user_id = $1 AND us.team = $2 AND wp.category = 'Gloves'", uuid, team).fetch_optional(&pool).await? {
|
||||
Some(gloves) => gloves,
|
||||
None => {
|
||||
let name = match team {
|
||||
0 => "ct_gloves",
|
||||
_ => "t_gloves"
|
||||
};
|
||||
let info = query!("SELECT wp.name, r.color FROM weapons wp LEFT JOIN skins sk ON sk.weapon = wp.id LEFT JOIN rarity r ON r.id = sk.rarity WHERE wp.name = $1", name).fetch_one(&pool).await?;
|
||||
WeaponInfo {
|
||||
id: Uuid::nil(),
|
||||
name: info.name,
|
||||
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 {
|
||||
Some(agent) => Ok(Json(SkinInfo {
|
||||
id: agent.id,
|
||||
weapon_id: agent.weapon_id,
|
||||
name: agent.name,
|
||||
display: agent.display,
|
||||
paint_index: 0,
|
||||
color: info.color
|
||||
}}
|
||||
};
|
||||
Ok(Json(gloves))
|
||||
color: agent.color,
|
||||
float: 0.0,
|
||||
min_float: 0.0,
|
||||
max_float: 0.0,
|
||||
pattern: 0,
|
||||
})),
|
||||
None => {
|
||||
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 {
|
||||
id: Uuid::nil(),
|
||||
weapon_id: default.weapon_id,
|
||||
name: default.name,
|
||||
display: default.display,
|
||||
paint_index: 0,
|
||||
color: default.color,
|
||||
float: 0.0,
|
||||
min_float: 0.0,
|
||||
max_float: 0.0,
|
||||
pattern: 0,
|
||||
}))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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 {
|
||||
let category = query!(
|
||||
"SELECT w.category FROM weapons w WHERE w.id=$1",
|
||||
paint_query.weapon
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
match category.category.as_str() {
|
||||
"Knives" | "Gloves" => 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 w.category=$1", category.category).fetch_all(&pool).await?,
|
||||
_ => 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 w.id=$1", paint_query.weapon).fetch_all(&pool).await?
|
||||
}
|
||||
};
|
||||
Ok(Json(paints))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user