few bug fixes
This commit is contained in:
+10
-5
@@ -25,25 +25,30 @@ fn main() -> std::io::Result<()> {
|
|||||||
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
||||||
|
|
||||||
match methods::Session::new(&mut reader, database) {
|
match methods::Session::new(&mut reader, database) {
|
||||||
Ok(session) => loop {
|
Ok((session, res)) => {
|
||||||
|
writeln!(stream, "{res}").unwrap();
|
||||||
|
stream.flush().unwrap();
|
||||||
|
|
||||||
|
loop {
|
||||||
let mut request_string = String::new();
|
let mut request_string = String::new();
|
||||||
match reader.read_line(&mut request_string).unwrap() {
|
match reader.read_line(&mut request_string).unwrap() {
|
||||||
0 => break,
|
0 => break,
|
||||||
_ => {
|
_ => {
|
||||||
let (method, params) = parser::parse(&request_string).unwrap();
|
let (method, params) = parser::parse(&request_string).unwrap();
|
||||||
let response = session.handle_request(&method, ¶ms);
|
let response = session.handle_request(&method, ¶ms);
|
||||||
stream
|
writeln!(
|
||||||
.write_all(
|
stream,
|
||||||
|
"{}",
|
||||||
match response {
|
match response {
|
||||||
Ok(response) => response.to_string(),
|
Ok(response) => response.to_string(),
|
||||||
Err(e) => format!("!{e}"),
|
Err(e) => format!("!{e}"),
|
||||||
}
|
}
|
||||||
.as_bytes(),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
stream.write_all(format!("!{e}").as_bytes()).unwrap();
|
stream.write_all(format!("!{e}").as_bytes()).unwrap();
|
||||||
stream
|
stream
|
||||||
|
|||||||
+12
-6
@@ -24,14 +24,14 @@ pub struct LocalPlayer {
|
|||||||
pub struct Session {
|
pub struct Session {
|
||||||
pub session_token: String,
|
pub session_token: String,
|
||||||
pub database: Arc<crate::database::Database>,
|
pub database: Arc<crate::database::Database>,
|
||||||
pub player: LocalPlayer,
|
pub local_player: LocalPlayer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
impl Session {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
reader: &mut BufReader<TcpStream>,
|
reader: &mut BufReader<TcpStream>,
|
||||||
database: Arc<crate::database::Database>,
|
database: Arc<crate::database::Database>,
|
||||||
) -> Result<Self> {
|
) -> Result<(Self, Response)> {
|
||||||
// Read the session token
|
// Read the session token
|
||||||
let mut session_token = String::new();
|
let mut session_token = String::new();
|
||||||
reader
|
reader
|
||||||
@@ -50,15 +50,21 @@ impl Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse the player data
|
// Parse the player data
|
||||||
let player: LocalPlayer = response
|
let local_player: LocalPlayer = response
|
||||||
.json()
|
.json()
|
||||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||||
|
|
||||||
Ok(Self {
|
let uuid = local_player.id.clone();
|
||||||
|
|
||||||
|
let session = Self {
|
||||||
session_token,
|
session_token,
|
||||||
database,
|
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 {
|
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 {
|
Ok(Response::Player {
|
||||||
cloak: player.cloak,
|
cloak: player.cloak,
|
||||||
|
uuid,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,20 @@ use crate::response::{Response, Result};
|
|||||||
use super::Session;
|
use super::Session;
|
||||||
|
|
||||||
pub fn set_cloak(session: &Session, cloak: String) -> Result {
|
pub fn set_cloak(session: &Session, cloak: String) -> Result {
|
||||||
session.database.players.update_one(
|
session
|
||||||
|
.database
|
||||||
|
.players
|
||||||
|
.update_one(
|
||||||
doc! {
|
doc! {
|
||||||
"uuid": session.player.id.clone(),
|
"uuid": session.local_player.id.clone(),
|
||||||
},
|
},
|
||||||
doc! {
|
doc! {
|
||||||
"$set": {
|
"$set": {
|
||||||
"cloak": cloak,
|
"cloak": cloak,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
).run()?;
|
)
|
||||||
|
.run()?;
|
||||||
|
|
||||||
Ok(Response::Success)
|
Ok(Response::Success)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -4,7 +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 },
|
Player { cloak: String, uuid: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Response {
|
impl std::fmt::Display for Response {
|
||||||
@@ -12,7 +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}"),
|
Response::Player { cloak, uuid } => write!(f, "@cloak={cloak}@uuid={uuid}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user