good structure
This commit is contained in:
+4
-1
@@ -1,3 +1,6 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
pub cloaks: Vec<String>,
|
pub cloaks: Vec<String>,
|
||||||
@@ -10,7 +13,7 @@ pub struct Database {
|
|||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub fn new(client: &mongodb::sync::Client) -> Self {
|
pub fn new(client: &mongodb::sync::Client) -> Self {
|
||||||
let db = client.database("saturn");
|
let db = client.database("saturnclient");
|
||||||
Self {
|
Self {
|
||||||
players: db.collection("players"),
|
players: db.collection("players"),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
mod player;
|
||||||
mod set_cloak;
|
mod set_cloak;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
@@ -66,6 +67,8 @@ impl Session {
|
|||||||
|
|
||||||
"set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?),
|
"set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?),
|
||||||
|
|
||||||
|
"player" => player::player(self, params.parse_param("uuid")?),
|
||||||
|
|
||||||
_ => Err(crate::response::Error::InvalidRequest),
|
_ => Err(crate::response::Error::InvalidRequest),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ pub type Result<T = Response> = std::result::Result<T, Error>;
|
|||||||
pub enum Response {
|
pub enum Response {
|
||||||
Pong,
|
Pong,
|
||||||
Success,
|
Success,
|
||||||
|
Player { cloak: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Response {
|
impl std::fmt::Display for Response {
|
||||||
@@ -11,6 +12,7 @@ impl std::fmt::Display for Response {
|
|||||||
match self {
|
match self {
|
||||||
Response::Pong => write!(f, "Pong"),
|
Response::Pong => write!(f, "Pong"),
|
||||||
Response::Success => write!(f, "Success"),
|
Response::Success => write!(f, "Success"),
|
||||||
|
Response::Player { cloak } => write!(f, "@cloak={cloak}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,6 +25,7 @@ pub enum Error {
|
|||||||
ParameterNotFound,
|
ParameterNotFound,
|
||||||
InvalidSession,
|
InvalidSession,
|
||||||
InvalidHandshake,
|
InvalidHandshake,
|
||||||
|
DatabaseError,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl std::fmt::Display for Error {
|
||||||
@@ -34,6 +37,14 @@ impl std::fmt::Display for Error {
|
|||||||
Error::ParameterNotFound => write!(f, "Parameter not found"),
|
Error::ParameterNotFound => write!(f, "Parameter not found"),
|
||||||
Error::InvalidSession => write!(f, "Invalid session"),
|
Error::InvalidSession => write!(f, "Invalid session"),
|
||||||
Error::InvalidHandshake => write!(f, "Invalid handshake"),
|
Error::InvalidHandshake => write!(f, "Invalid handshake"),
|
||||||
|
Error::DatabaseError => write!(f, "Database error"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<mongodb::error::Error> for Error {
|
||||||
|
fn from(e: mongodb::error::Error) -> Self {
|
||||||
|
println!("{e}");
|
||||||
|
Error::DatabaseError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user