Working code

This commit is contained in:
2026-02-23 02:49:03 +01:00
parent cdb40f64f4
commit 02985ce60a
4 changed files with 119 additions and 63 deletions
+28 -25
View File
@@ -1,4 +1,7 @@
use std::sync::Arc;
use sqlx::SqlitePool;
use tokio::sync::Mutex;
use crate::user::get_put;
@@ -15,7 +18,7 @@ pub const CLOAKS: &[&str] = &[
pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"];
enum CosmeticKind {
pub enum CosmeticKind {
Hat,
Cloak,
}
@@ -23,27 +26,27 @@ enum CosmeticKind {
// Buy a cosmetic
pub async fn buy(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
uuid: Arc<Mutex<String>>,
item_id: String,
pool: Arc<SqlitePool>,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
let mut user = get_put(uuid.lock().await.as_str(), &pool).await?;
match kind {
CosmeticKind::Hat => {
if !HATS.contains(&id) {
if !HATS.contains(&item_id.as_str()) {
return Err("Hat does not exist".into());
}
if !user.hats.contains(&id.to_string()) {
user.hats.push(id.to_string());
if !user.hats.contains(&item_id.to_string()) {
user.hats.push(item_id.to_string());
}
}
CosmeticKind::Cloak => {
if !CLOAKS.contains(&id) {
if !CLOAKS.contains(&item_id.as_str()) {
return Err("Cloak does not exist".into());
}
if !user.cloaks.contains(&id.to_string()) {
user.cloaks.push(id.to_string());
if !user.cloaks.contains(&item_id.to_string()) {
user.cloaks.push(item_id.to_string());
}
}
}
@@ -51,45 +54,45 @@ pub async fn buy(
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)
.bind(uuid.lock().await.as_str())
.execute(pool.as_ref())
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} bought successfully!", id))
Ok(format!("Item {} bought successfully!", item_id))
}
// Equip a cosmetic
pub async fn equip(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
uuid: Arc<Mutex<String>>,
item_id: String,
pool: Arc<SqlitePool>,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
let mut user = get_put(uuid.lock().await.as_str(), &pool).await?;
match kind {
CosmeticKind::Hat => {
if !user.hats.contains(&id.to_string()) {
if !user.hats.contains(&item_id) {
return Err("You don't own this hat".into());
}
user.hat = id.to_string();
user.hat = item_id.to_string();
}
CosmeticKind::Cloak => {
if !user.cloaks.contains(&id.to_string()) {
if !user.cloaks.contains(&item_id) {
return Err("You don't own this cloak".into());
}
user.cloak = id.to_string();
user.cloak = item_id.to_string();
}
}
sqlx::query("UPDATE users SET cloak = ?, hat = ? WHERE uuid = ?")
.bind(&user.cloak)
.bind(&user.hat)
.bind(uuid)
.execute(pool)
.bind(uuid.lock().await.as_str())
.execute(pool.as_ref())
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} equipped successfully!", id))
Ok(format!("Item {} equipped successfully!", item_id))
}