diff --git a/src/cosmetics.rs b/src/cosmetics.rs new file mode 100644 index 0000000..4146423 --- /dev/null +++ b/src/cosmetics.rs @@ -0,0 +1,3 @@ +pub const CLOAKS: &[&str] = &["mercedes_flow", "glitch"]; + +pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"]; diff --git a/src/main.rs b/src/main.rs index e8098db..7241e4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ pub mod encryption; pub mod methods; pub mod parser; pub mod response; +pub mod cosmetics; fn main() -> Result<(), response::Error> { let rsa = Rsa::generate(2048) diff --git a/src/methods/buy.rs b/src/methods/buy.rs new file mode 100644 index 0000000..03370cf --- /dev/null +++ b/src/methods/buy.rs @@ -0,0 +1,19 @@ +use mongodb::bson::doc; + +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 } + }; + + if let Some(doc) = session.database.players.find_one(filter).run()? { + println!("Found matching document: {:?}", doc); + Ok(crate::response::Response::Success) + } else { + Err(crate::response::Error::TransactionError( + "Not enough coins".to_string(), + )) + } +} diff --git a/src/methods/mod.rs b/src/methods/mod.rs index 50fa7b1..82eca16 100644 --- a/src/methods/mod.rs +++ b/src/methods/mod.rs @@ -1,3 +1,4 @@ +mod buy; pub mod player; mod set_cloak; mod set_hat; @@ -126,6 +127,8 @@ impl Session { Ok(Response::Players(players)) } + "buy_cloak" => buy::buy_cloak(self, params.parse_param("cloak")?), + _ => Err(crate::response::Error::InvalidMethod(method.to_string())), } } diff --git a/src/response.rs b/src/response.rs index 65469af..4505abd 100644 --- a/src/response.rs +++ b/src/response.rs @@ -74,6 +74,7 @@ pub enum Error { AuthenticationError(String), ValidationError(String), EncryptionError(String), + TransactionError(String), } impl std::fmt::Display for Error { @@ -81,7 +82,9 @@ impl std::fmt::Display for Error { match self { Error::InvalidRequest(msg) => write!(f, "Invalid request: {}", msg), Error::InvalidMethod(method) => write!(f, "Invalid method: {}", method), - Error::InvalidParameter { param, reason } => write!(f, "Invalid parameter '{}': {}", param, reason), + Error::InvalidParameter { param, reason } => { + write!(f, "Invalid parameter '{}': {}", param, reason) + } Error::ParameterNotFound(param) => write!(f, "Required parameter not found: {}", param), Error::InvalidSession(details) => write!(f, "Invalid session: {}", details), Error::InvalidHandshake(details) => write!(f, "Handshake failed: {}", details),