cloak sec and hats
This commit is contained in:
+18
-5
@@ -1,5 +1,6 @@
|
||||
mod player;
|
||||
mod set_cloak;
|
||||
mod set_hat;
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@@ -13,7 +14,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
parser::ParamMap,
|
||||
response::{Response, Result},
|
||||
response::{PlayerResponse, Response, Result},
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
@@ -55,17 +56,14 @@ impl Session {
|
||||
.json()
|
||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||
|
||||
let uuid = local_player.id.clone();
|
||||
|
||||
let session = Self {
|
||||
session_token,
|
||||
database,
|
||||
local_player,
|
||||
};
|
||||
|
||||
|
||||
// Capture the player
|
||||
match player::player(&session, uuid) {
|
||||
match player::login(&session) {
|
||||
Ok(player) => Ok((session, player)),
|
||||
Err(crate::response::Error::DatabaseError) => {
|
||||
let player = create(&session)?;
|
||||
@@ -81,8 +79,23 @@ impl Session {
|
||||
|
||||
"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")?),
|
||||
|
||||
"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),
|
||||
}
|
||||
}
|
||||
|
||||
+55
-26
@@ -19,37 +19,18 @@ pub fn player(session: &Session, uuid: String) -> Result {
|
||||
match cursor.next() {
|
||||
Some(Ok(player)) => {
|
||||
// Player exists, deserialize and return their data
|
||||
Ok(Response::Player {
|
||||
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 {
|
||||
cloak: player.cloak,
|
||||
uuid,
|
||||
cloaks: player.cloaks,
|
||||
hats: player.hats,
|
||||
hat: player.hat,
|
||||
})
|
||||
}
|
||||
_ => Ok(crate::response::Response::Player(
|
||||
crate::response::PlayerResponse::NonSaturnPlayer,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +47,59 @@ pub fn create(session: &Session) -> Result {
|
||||
|
||||
session.database.players.insert_one(player.clone()).run()?;
|
||||
|
||||
Ok(Response::Player {
|
||||
Ok(Response::Player(crate::response::PlayerResponse::Player {
|
||||
cloak: player.cloak,
|
||||
uuid,
|
||||
cloaks: player.cloaks,
|
||||
hats: player.hats,
|
||||
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 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 {
|
||||
session
|
||||
.database
|
||||
.players
|
||||
.update_one(
|
||||
doc! {
|
||||
"uuid": session.local_player.id.clone(),
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"cloak": cloak,
|
||||
},
|
||||
},
|
||||
)
|
||||
.run()?;
|
||||
|
||||
Ok(Response::Success)
|
||||
match player::player(session, session.local_player.id.clone())? {
|
||||
Response::Player(crate::response::PlayerResponse::Player { cloaks, .. }) => {
|
||||
if cloaks.contains(&cloak) || cloak.is_empty() {
|
||||
session
|
||||
.database
|
||||
.players
|
||||
.update_one(
|
||||
doc! {
|
||||
"uuid": session.local_player.id.clone(),
|
||||
},
|
||||
doc! {
|
||||
"$set": {
|
||||
"cloak": cloak,
|
||||
},
|
||||
},
|
||||
)
|
||||
.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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user