few bug fixes
This commit is contained in:
+12
-6
@@ -24,14 +24,14 @@ pub struct LocalPlayer {
|
||||
pub struct Session {
|
||||
pub session_token: String,
|
||||
pub database: Arc<crate::database::Database>,
|
||||
pub player: LocalPlayer,
|
||||
pub local_player: LocalPlayer,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
database: Arc<crate::database::Database>,
|
||||
) -> Result<Self> {
|
||||
) -> Result<(Self, Response)> {
|
||||
// Read the session token
|
||||
let mut session_token = String::new();
|
||||
reader
|
||||
@@ -50,15 +50,21 @@ impl Session {
|
||||
}
|
||||
|
||||
// Parse the player data
|
||||
let player: LocalPlayer = response
|
||||
let local_player: LocalPlayer = response
|
||||
.json()
|
||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||
|
||||
Ok(Self {
|
||||
let uuid = local_player.id.clone();
|
||||
|
||||
let session = Self {
|
||||
session_token,
|
||||
database,
|
||||
player,
|
||||
})
|
||||
local_player,
|
||||
};
|
||||
|
||||
let player = player::player(&session, uuid)?;
|
||||
|
||||
Ok((session, player))
|
||||
}
|
||||
|
||||
pub fn handle_request(&self, method: &str, params: &HashMap<String, String>) -> Result {
|
||||
|
||||
@@ -17,5 +17,6 @@ pub fn player(session: &Session, uuid: String) -> Result {
|
||||
|
||||
Ok(Response::Player {
|
||||
cloak: player.cloak,
|
||||
uuid,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,16 +5,20 @@ 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,
|
||||
session
|
||||
.database
|
||||
.players
|
||||
.update_one(
|
||||
doc! {
|
||||
"uuid": session.local_player.id.clone(),
|
||||
},
|
||||
},
|
||||
).run()?;
|
||||
doc! {
|
||||
"$set": {
|
||||
"cloak": cloak,
|
||||
},
|
||||
},
|
||||
)
|
||||
.run()?;
|
||||
|
||||
Ok(Response::Success)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user