working buy system

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