working buy system
This commit is contained in:
+19
-7
@@ -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);
|
||||
Ok(crate::response::Response::Success)
|
||||
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::TransactionError(
|
||||
"Not enough coins".to_string(),
|
||||
Err(crate::response::Error::DatabaseError(
|
||||
"Player not found".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user