cloak sec and hats
This commit is contained in:
@@ -26,3 +26,7 @@ response
|
|||||||
!Invalid method
|
!Invalid method
|
||||||
```
|
```
|
||||||
On a response `!` means it's an error.
|
On a response `!` means it's an error.
|
||||||
|
|
||||||
|
# Methods
|
||||||
|
### `set_cloak`
|
||||||
|
- `cloak` -> `string`
|
||||||
|
|||||||
+18
-5
@@ -1,5 +1,6 @@
|
|||||||
mod player;
|
mod player;
|
||||||
mod set_cloak;
|
mod set_cloak;
|
||||||
|
mod set_hat;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
@@ -13,7 +14,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
parser::ParamMap,
|
parser::ParamMap,
|
||||||
response::{Response, Result},
|
response::{PlayerResponse, Response, Result},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
@@ -55,17 +56,14 @@ impl Session {
|
|||||||
.json()
|
.json()
|
||||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||||
|
|
||||||
let uuid = local_player.id.clone();
|
|
||||||
|
|
||||||
let session = Self {
|
let session = Self {
|
||||||
session_token,
|
session_token,
|
||||||
database,
|
database,
|
||||||
local_player,
|
local_player,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Capture the player
|
// Capture the player
|
||||||
match player::player(&session, uuid) {
|
match player::login(&session) {
|
||||||
Ok(player) => Ok((session, player)),
|
Ok(player) => Ok((session, player)),
|
||||||
Err(crate::response::Error::DatabaseError) => {
|
Err(crate::response::Error::DatabaseError) => {
|
||||||
let player = create(&session)?;
|
let player = create(&session)?;
|
||||||
@@ -81,8 +79,23 @@ impl Session {
|
|||||||
|
|
||||||
"set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?),
|
"set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?),
|
||||||
|
|
||||||
|
"set_hat" => set_hat::set_hat(self, params.parse_param("hat")?),
|
||||||
|
|
||||||
"player" => player::player(self, params.parse_param("uuid")?),
|
"player" => player::player(self, params.parse_param("uuid")?),
|
||||||
|
|
||||||
|
"players" => {
|
||||||
|
let mut players: Vec<PlayerResponse> = Vec::new();
|
||||||
|
for uuid in params.parse_param::<String>("uuids")?.split("$") {
|
||||||
|
match player::player(self, uuid.to_string())? {
|
||||||
|
Response::Player(p) => {
|
||||||
|
players.push(p);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Response::Players(players))
|
||||||
|
}
|
||||||
|
|
||||||
_ => Err(crate::response::Error::InvalidRequest),
|
_ => Err(crate::response::Error::InvalidRequest),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-26
@@ -19,37 +19,18 @@ pub fn player(session: &Session, uuid: String) -> Result {
|
|||||||
match cursor.next() {
|
match cursor.next() {
|
||||||
Some(Ok(player)) => {
|
Some(Ok(player)) => {
|
||||||
// Player exists, deserialize and return their data
|
// Player exists, deserialize and return their data
|
||||||
Ok(Response::Player {
|
Ok(Response::Player(crate::response::PlayerResponse::Player {
|
||||||
cloak: player.cloak,
|
cloak: player.cloak,
|
||||||
uuid,
|
uuid,
|
||||||
cloaks: player.cloaks,
|
cloaks: player.cloaks,
|
||||||
hats: player.hats,
|
hats: player.hats,
|
||||||
hat: player.hat,
|
hat: player.hat,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// No document found or deserialization error
|
_ => Ok(crate::response::Response::Player(
|
||||||
_ => {
|
crate::response::PlayerResponse::NonSaturnPlayer,
|
||||||
let player = Player {
|
)),
|
||||||
uuid: uuid.clone(),
|
|
||||||
cloaks: Vec::new(),
|
|
||||||
cloak: "".to_string(),
|
|
||||||
hats: Vec::new(),
|
|
||||||
hat: String::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Insert the new player
|
|
||||||
session.database.players.insert_one(player.clone()).run()?;
|
|
||||||
|
|
||||||
// Return the newly created player's data
|
|
||||||
Ok(Response::Player {
|
|
||||||
cloak: player.cloak,
|
|
||||||
uuid,
|
|
||||||
cloaks: player.cloaks,
|
|
||||||
hats: player.hats,
|
|
||||||
hat: player.hat,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +47,59 @@ pub fn create(session: &Session) -> Result {
|
|||||||
|
|
||||||
session.database.players.insert_one(player.clone()).run()?;
|
session.database.players.insert_one(player.clone()).run()?;
|
||||||
|
|
||||||
Ok(Response::Player {
|
Ok(Response::Player(crate::response::PlayerResponse::Player {
|
||||||
cloak: player.cloak,
|
cloak: player.cloak,
|
||||||
uuid,
|
uuid,
|
||||||
cloaks: player.cloaks,
|
cloaks: player.cloaks,
|
||||||
hats: player.hats,
|
hats: player.hats,
|
||||||
hat: player.hat,
|
hat: player.hat,
|
||||||
})
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn login(session: &Session) -> Result {
|
||||||
|
let uuid = session.local_player.id.clone();
|
||||||
|
|
||||||
|
// Try to find the player with the given UUID
|
||||||
|
let mut cursor = session
|
||||||
|
.database
|
||||||
|
.players
|
||||||
|
.find(doc! { "uuid": uuid.clone() })
|
||||||
|
.run()?;
|
||||||
|
|
||||||
|
// Check if there's a document in the cursor
|
||||||
|
match cursor.next() {
|
||||||
|
Some(Ok(player)) => {
|
||||||
|
// Player exists, deserialize and return their data
|
||||||
|
Ok(Response::Player(crate::response::PlayerResponse::Player {
|
||||||
|
cloak: player.cloak,
|
||||||
|
uuid,
|
||||||
|
cloaks: player.cloaks,
|
||||||
|
hats: player.hats,
|
||||||
|
hat: player.hat,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// No document found or deserialization error
|
||||||
|
_ => {
|
||||||
|
let player = Player {
|
||||||
|
uuid: uuid.clone(),
|
||||||
|
cloaks: Vec::new(),
|
||||||
|
cloak: "".to_string(),
|
||||||
|
hats: Vec::new(),
|
||||||
|
hat: String::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Insert the new player
|
||||||
|
session.database.players.insert_one(player.clone()).run()?;
|
||||||
|
|
||||||
|
// Return the newly created player's data
|
||||||
|
Ok(Response::Player(crate::response::PlayerResponse::Player {
|
||||||
|
cloak: player.cloak,
|
||||||
|
uuid,
|
||||||
|
cloaks: player.cloaks,
|
||||||
|
hats: player.hats,
|
||||||
|
hat: player.hat,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-18
@@ -1,24 +1,32 @@
|
|||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
|
|
||||||
use crate::response::{Response, Result};
|
use crate::response::{Error, Response, Result};
|
||||||
|
|
||||||
use super::Session;
|
use super::{player, Session};
|
||||||
|
|
||||||
pub fn set_cloak(session: &Session, cloak: String) -> Result {
|
pub fn set_cloak(session: &Session, cloak: String) -> Result {
|
||||||
session
|
match player::player(session, session.local_player.id.clone())? {
|
||||||
.database
|
Response::Player(crate::response::PlayerResponse::Player { cloaks, .. }) => {
|
||||||
.players
|
if cloaks.contains(&cloak) || cloak.is_empty() {
|
||||||
.update_one(
|
session
|
||||||
doc! {
|
.database
|
||||||
"uuid": session.local_player.id.clone(),
|
.players
|
||||||
},
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"uuid": session.local_player.id.clone(),
|
||||||
"cloak": cloak,
|
},
|
||||||
},
|
doc! {
|
||||||
},
|
"$set": {
|
||||||
)
|
"cloak": cloak,
|
||||||
.run()?;
|
},
|
||||||
|
},
|
||||||
Ok(Response::Success)
|
)
|
||||||
|
.run()?;
|
||||||
|
Ok(Response::Success)
|
||||||
|
} else {
|
||||||
|
Err(Error::SomethingWentWrong)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(Error::SomethingWentWrong),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
use mongodb::bson::doc;
|
||||||
|
|
||||||
|
use crate::response::{Error, Response, Result};
|
||||||
|
|
||||||
|
use super::{player, Session};
|
||||||
|
|
||||||
|
pub fn set_hat(session: &Session, hat: String) -> Result {
|
||||||
|
match player::player(session, session.local_player.id.clone())? {
|
||||||
|
Response::Player(crate::response::PlayerResponse::Player { hats, .. }) => {
|
||||||
|
if hats.contains(&hat) || hat.is_empty() {
|
||||||
|
session
|
||||||
|
.database
|
||||||
|
.players
|
||||||
|
.update_one(
|
||||||
|
doc! {
|
||||||
|
"uuid": session.local_player.id.clone(),
|
||||||
|
},
|
||||||
|
doc! {
|
||||||
|
"$set": {
|
||||||
|
"hat": hat,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.run()?;
|
||||||
|
|
||||||
|
Ok(Response::Success)
|
||||||
|
} else {
|
||||||
|
Err(Error::SomethingWentWrong)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Err(Error::SomethingWentWrong),
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
-9
@@ -1,9 +1,7 @@
|
|||||||
pub type Result<T = Response> = std::result::Result<T, Error>;
|
pub type Result<T = Response> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Response {
|
pub enum PlayerResponse {
|
||||||
Pong,
|
|
||||||
Success,
|
|
||||||
Player {
|
Player {
|
||||||
uuid: String,
|
uuid: String,
|
||||||
cloaks: Vec<String>,
|
cloaks: Vec<String>,
|
||||||
@@ -11,14 +9,12 @@ pub enum Response {
|
|||||||
hats: Vec<String>,
|
hats: Vec<String>,
|
||||||
hat: String,
|
hat: String,
|
||||||
},
|
},
|
||||||
|
NonSaturnPlayer,
|
||||||
}
|
}
|
||||||
|
impl std::fmt::Display for PlayerResponse {
|
||||||
impl std::fmt::Display for Response {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Response::Pong => write!(f, "Pong"),
|
PlayerResponse::Player {
|
||||||
Response::Success => write!(f, "Success"),
|
|
||||||
Response::Player {
|
|
||||||
uuid,
|
uuid,
|
||||||
cloaks,
|
cloaks,
|
||||||
cloak,
|
cloak,
|
||||||
@@ -26,10 +22,40 @@ impl std::fmt::Display for Response {
|
|||||||
hat,
|
hat,
|
||||||
} => write!(
|
} => write!(
|
||||||
f,
|
f,
|
||||||
"@cloak={cloak}@uuid={uuid}@cloaks={}@hats={}@hat={hat}",
|
"player@cloak={cloak}@uuid={uuid}@cloaks={}@hats={}@hat={hat}@saturn=true",
|
||||||
cloaks.join("$"),
|
cloaks.join("$"),
|
||||||
hats.join("$"),
|
hats.join("$"),
|
||||||
),
|
),
|
||||||
|
PlayerResponse::NonSaturnPlayer => write!(f, "@saturn=false"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Response {
|
||||||
|
Pong,
|
||||||
|
Success,
|
||||||
|
Player(PlayerResponse),
|
||||||
|
Players(Vec<PlayerResponse>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Response {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Response::Pong => write!(f, "Pong"),
|
||||||
|
Response::Success => write!(f, "Success"),
|
||||||
|
Response::Player(player) => write!(f, "{player}"),
|
||||||
|
Response::Players(players) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
players
|
||||||
|
.iter()
|
||||||
|
.map(|p| format!("{p}"))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n")
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,6 +69,7 @@ pub enum Error {
|
|||||||
InvalidSession,
|
InvalidSession,
|
||||||
InvalidHandshake,
|
InvalidHandshake,
|
||||||
DatabaseError,
|
DatabaseError,
|
||||||
|
SomethingWentWrong,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Error {
|
impl std::fmt::Display for Error {
|
||||||
@@ -55,6 +82,7 @@ impl std::fmt::Display for Error {
|
|||||||
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"),
|
Error::DatabaseError => write!(f, "Database error"),
|
||||||
|
Error::SomethingWentWrong => write!(f, "Something went wrong"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user