working buy system

This commit is contained in:
2025-06-22 15:44:22 -05:00
parent e994ab77bc
commit 782ffb0520
+19 -7
View File
@@ -4,16 +4,28 @@ use crate::{methods::Session, response::Result};
pub fn buy_cloak(session: &Session, cloak: String) -> Result { pub fn buy_cloak(session: &Session, cloak: String) -> Result {
let filter = doc! { let filter = doc! {
"uuid": session.local_player.id.clone(), "uuid": session.local_player.id.clone()
"points": { "$gte": 100 }
}; };
if let Some(doc) = session.database.players.find_one(filter).run()? { if let Some(doc) = session.database.players.find_one(filter.clone()).run()? {
println!("Found matching document: {:?}", doc); if doc.coins >= 100 {
Ok(crate::response::Response::Success) session
.database
.players
.update_one(
filter,
doc! { "$inc": { "points": -100 }, "$push": { "cloaks": cloak } },
)
.run()?;
Ok(crate::response::Response::Success)
} else {
Err(crate::response::Error::TransactionError(
"Not enough coins".to_string(),
))
}
} else { } else {
Err(crate::response::Error::TransactionError( Err(crate::response::Error::DatabaseError(
"Not enough coins".to_string(), "Player not found".to_string(),
)) ))
} }
} }