Fixed
This commit is contained in:
+28
-16
@@ -1,18 +1,20 @@
|
||||
use sqlx::SqlitePool;
|
||||
use std::sync::Arc;
|
||||
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::{types::UUID, user::get_put};
|
||||
use crate::{types::UUID, user};
|
||||
|
||||
pub const CLOAKS: &[&str] = &[
|
||||
"mercedes_flow",
|
||||
"glitch",
|
||||
"mercedes_flow",
|
||||
"crimson_mark",
|
||||
"bmw",
|
||||
"amg",
|
||||
"amg_petronas",
|
||||
"ferrari",
|
||||
"redbull",
|
||||
"black_hole_amethyst",
|
||||
"black_hole_flame",
|
||||
"black_hole_white",
|
||||
];
|
||||
|
||||
pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"];
|
||||
@@ -29,31 +31,36 @@ pub async fn buy(
|
||||
item_id: String,
|
||||
pool: Arc<SqlitePool>,
|
||||
) -> Result<String, String> {
|
||||
let mut user = get_put(uuid.lock().await.as_str(), &pool).await?;
|
||||
// Lock once
|
||||
let uuid = uuid.lock().await.clone();
|
||||
|
||||
let mut user = user::get(&uuid, &pool).await?;
|
||||
|
||||
match kind {
|
||||
CosmeticKind::Hat => {
|
||||
if !HATS.contains(&item_id.as_str()) {
|
||||
return Err("Hat does not exist".into());
|
||||
}
|
||||
if !user.hats.contains(&item_id.to_string()) {
|
||||
user.hats.push(item_id.to_string());
|
||||
|
||||
if !user.hats.contains(&item_id) {
|
||||
user.hats.push(item_id.clone());
|
||||
}
|
||||
}
|
||||
CosmeticKind::Cloak => {
|
||||
if !CLOAKS.contains(&item_id.as_str()) {
|
||||
return Err("Cloak does not exist".into());
|
||||
}
|
||||
if !user.cloaks.contains(&item_id.to_string()) {
|
||||
user.cloaks.push(item_id.to_string());
|
||||
|
||||
if !user.cloaks.contains(&item_id) {
|
||||
user.cloaks.push(item_id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.lock().await.as_str())
|
||||
.bind(serde_json::to_string(&user.cloaks).map_err(|e| e.to_string())?)
|
||||
.bind(serde_json::to_string(&user.hats).map_err(|e| e.to_string())?)
|
||||
.bind(&uuid)
|
||||
.execute(pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -68,27 +75,32 @@ pub async fn equip(
|
||||
item_id: String,
|
||||
pool: Arc<SqlitePool>,
|
||||
) -> Result<String, String> {
|
||||
let mut user = get_put(uuid.lock().await.as_str(), &pool).await?;
|
||||
// Lock once
|
||||
let uuid = uuid.lock().await.clone();
|
||||
|
||||
let mut user = user::get(&uuid, &pool).await?;
|
||||
|
||||
match kind {
|
||||
CosmeticKind::Hat => {
|
||||
if !user.hats.contains(&item_id) {
|
||||
return Err("You don't own this hat".into());
|
||||
}
|
||||
user.hat = item_id.to_string();
|
||||
|
||||
user.hat = item_id.clone();
|
||||
}
|
||||
CosmeticKind::Cloak => {
|
||||
if !user.cloaks.contains(&item_id) {
|
||||
return Err("You don't own this cloak".into());
|
||||
}
|
||||
user.cloak = item_id.to_string();
|
||||
|
||||
user.cloak = item_id.clone();
|
||||
}
|
||||
}
|
||||
|
||||
sqlx::query("UPDATE users SET cloak = ?, hat = ? WHERE uuid = ?")
|
||||
.bind(&user.cloak)
|
||||
.bind(&user.hat)
|
||||
.bind(uuid.lock().await.as_str())
|
||||
.bind(&uuid)
|
||||
.execute(pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Reference in New Issue
Block a user