diff --git a/src/methods/buy.rs b/src/methods/buy.rs index 57aa5eb..6b62403 100644 --- a/src/methods/buy.rs +++ b/src/methods/buy.rs @@ -31,3 +31,33 @@ pub fn buy_cloak(session: &Session, cloak: String) -> Result { )) } } + +pub fn buy_hat(session: &Session, hat: String) -> Result { + let filter = doc! { + "uuid": session.local_player.id.clone() + }; + + if let Some(doc) = session.database.players.find_one(filter.clone()).run()? { + if doc.coins >= 50 { + session + .database + .players + .update_one( + filter, + doc! { "$inc": { "coins": -50 }, "$push": { "hats": hat.clone() } }, + ) + .run()?; + Ok(crate::response::Response::SuccessfulTransaction(format!( + "hat={hat}" + ))) + } else { + Err(crate::response::Error::TransactionError( + "Not enough coins".to_string(), + )) + } + } else { + Err(crate::response::Error::DatabaseError( + "Player not found".to_string(), + )) + } +} diff --git a/src/methods/mod.rs b/src/methods/mod.rs index 82eca16..222a584 100644 --- a/src/methods/mod.rs +++ b/src/methods/mod.rs @@ -128,6 +128,7 @@ impl Session { } "buy_cloak" => buy::buy_cloak(self, params.parse_param("cloak")?), + "buy_hat" => buy::buy_hat(self, params.parse_param("hat")?), _ => Err(crate::response::Error::InvalidMethod(method.to_string())), }