diff --git a/src/database.rs b/src/database.rs index b9456ba..4915ef1 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,3 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize)] pub struct Player { pub uuid: String, pub cloaks: Vec, @@ -10,7 +13,7 @@ pub struct Database { impl Database { pub fn new(client: &mongodb::sync::Client) -> Self { - let db = client.database("saturn"); + let db = client.database("saturnclient"); Self { players: db.collection("players"), } diff --git a/src/methods/mod.rs b/src/methods/mod.rs index bd6d420..0f4f341 100644 --- a/src/methods/mod.rs +++ b/src/methods/mod.rs @@ -1,3 +1,4 @@ +mod player; mod set_cloak; use std::{ @@ -66,6 +67,8 @@ impl Session { "set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?), + "player" => player::player(self, params.parse_param("uuid")?), + _ => Err(crate::response::Error::InvalidRequest), } } diff --git a/src/methods/player.rs b/src/methods/player.rs new file mode 100644 index 0000000..1f84eab --- /dev/null +++ b/src/methods/player.rs @@ -0,0 +1,21 @@ +use mongodb::bson::doc; + +use crate::response::{Response, Result}; + +use super::Session; + +pub fn player(session: &Session, uuid: String) -> Result { + let player = session + .database + .players + .find(doc! { + "uuid": uuid.clone(), + }) + .run()? + .deserialize_current() + .unwrap(); + + Ok(Response::Player { + cloak: player.cloak, + }) +} diff --git a/src/methods/set_cloak.rs b/src/methods/set_cloak.rs new file mode 100644 index 0000000..ebb6c0b --- /dev/null +++ b/src/methods/set_cloak.rs @@ -0,0 +1,20 @@ +use mongodb::bson::doc; + +use crate::response::{Response, Result}; + +use super::Session; + +pub fn set_cloak(session: &Session, cloak: String) -> Result { + session.database.players.update_one( + doc! { + "uuid": session.player.id.clone(), + }, + doc! { + "$set": { + "cloak": cloak, + }, + }, + ).run()?; + + Ok(Response::Success) +} diff --git a/src/response.rs b/src/response.rs index 0c0978c..4cd95bb 100644 --- a/src/response.rs +++ b/src/response.rs @@ -4,6 +4,7 @@ pub type Result = std::result::Result; pub enum Response { Pong, Success, + Player { cloak: String }, } impl std::fmt::Display for Response { @@ -11,6 +12,7 @@ impl std::fmt::Display for Response { match self { Response::Pong => write!(f, "Pong"), Response::Success => write!(f, "Success"), + Response::Player { cloak } => write!(f, "@cloak={cloak}"), } } } @@ -23,6 +25,7 @@ pub enum Error { ParameterNotFound, InvalidSession, InvalidHandshake, + DatabaseError, } impl std::fmt::Display for Error { @@ -34,6 +37,14 @@ impl std::fmt::Display for Error { Error::ParameterNotFound => write!(f, "Parameter not found"), Error::InvalidSession => write!(f, "Invalid session"), Error::InvalidHandshake => write!(f, "Invalid handshake"), + Error::DatabaseError => write!(f, "Database error"), } } } + +impl From for Error { + fn from(e: mongodb::error::Error) -> Self { + println!("{e}"); + Error::DatabaseError + } +}