working buy system
This commit is contained in:
+16
-4
@@ -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 {
|
||||||
|
session
|
||||||
|
.database
|
||||||
|
.players
|
||||||
|
.update_one(
|
||||||
|
filter,
|
||||||
|
doc! { "$inc": { "points": -100 }, "$push": { "cloaks": cloak } },
|
||||||
|
)
|
||||||
|
.run()?;
|
||||||
Ok(crate::response::Response::Success)
|
Ok(crate::response::Response::Success)
|
||||||
} else {
|
} else {
|
||||||
Err(crate::response::Error::TransactionError(
|
Err(crate::response::Error::TransactionError(
|
||||||
"Not enough coins".to_string(),
|
"Not enough coins".to_string(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Err(crate::response::Error::DatabaseError(
|
||||||
|
"Player not found".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user