Structuring

This commit is contained in:
2026-02-23 02:25:38 +01:00
parent 7b532981a1
commit cdb40f64f4
6 changed files with 1373 additions and 20 deletions
+74 -4
View File
@@ -1,3 +1,7 @@
use sqlx::SqlitePool;
use crate::user::get_put;
pub const CLOAKS: &[&str] = &[
"mercedes_flow",
"glitch",
@@ -16,10 +20,76 @@ enum CosmeticKind {
Cloak,
}
async fn buy(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item bought successfully, congrats!"))
// Buy a cosmetic
pub async fn buy(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
match kind {
CosmeticKind::Hat => {
if !HATS.contains(&id) {
return Err("Hat does not exist".into());
}
if !user.hats.contains(&id.to_string()) {
user.hats.push(id.to_string());
}
}
CosmeticKind::Cloak => {
if !CLOAKS.contains(&id) {
return Err("Cloak does not exist".into());
}
if !user.cloaks.contains(&id.to_string()) {
user.cloaks.push(id.to_string());
}
}
}
sqlx::query("UPDATE users SET cloaks = ?, hats = ? WHERE uuid = ?")
.bind(serde_json::to_string(&user.cloaks).unwrap())
.bind(serde_json::to_string(&user.hats).unwrap())
.bind(uuid)
.execute(pool)
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} bought successfully!", id))
}
async fn equip(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item equipped"))
// Equip a cosmetic
pub async fn equip(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
match kind {
CosmeticKind::Hat => {
if !user.hats.contains(&id.to_string()) {
return Err("You don't own this hat".into());
}
user.hat = id.to_string();
}
CosmeticKind::Cloak => {
if !user.cloaks.contains(&id.to_string()) {
return Err("You don't own this cloak".into());
}
user.cloak = id.to_string();
}
}
sqlx::query("UPDATE users SET cloak = ?, hat = ? WHERE uuid = ?")
.bind(&user.cloak)
.bind(&user.hat)
.bind(uuid)
.execute(pool)
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} equipped successfully!", id))
}