Compare commits
2 commits
9701e4dcc0
...
c1938347a2
Author | SHA1 | Date | |
---|---|---|---|
|
c1938347a2 | ||
|
457e69bcbb |
4 changed files with 31 additions and 14 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -1715,9 +1715,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_spanned"
|
name = "serde_spanned"
|
||||||
version = "0.6.6"
|
version = "0.6.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0"
|
checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
@ -2052,9 +2052,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.8.15"
|
version = "0.8.16"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac2caab0bf757388c6c0ae23b3293fdb463fee59434529014f85e3263b995c28"
|
checksum = "81967dd0dd2c1ab0bc3468bd7caecc32b8a4aa47d0c8c695d8c2b2108168d62c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_spanned",
|
"serde_spanned",
|
||||||
|
@ -2064,18 +2064,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml_datetime"
|
name = "toml_datetime"
|
||||||
version = "0.6.6"
|
version = "0.6.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf"
|
checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml_edit"
|
name = "toml_edit"
|
||||||
version = "0.22.16"
|
version = "0.22.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "278f3d518e152219c994ce877758516bca5e118eaed6996192a774fb9fbf0788"
|
checksum = "8d9f8729f5aea9562aac1cc0441f5d6de3cff1ee0c5d67293eeca5eb36ee7c16"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -2566,9 +2566,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winnow"
|
name = "winnow"
|
||||||
version = "0.6.15"
|
version = "0.6.16"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "557404e450152cd6795bb558bca69e43c585055f4606e3bcae5894fc6dac9ba0"
|
checksum = "b480ae9340fc261e6be3e95a1ba86d54ae3f9171132a73ce8d4bbaf68339507c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
|
@ -21,12 +21,18 @@ pub struct ServerStateSummary {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// User
|
/// User
|
||||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl User {
|
||||||
|
pub fn change_name(&mut self, new_name: String) {
|
||||||
|
self.name = new_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// New game request structure
|
/// New game request structure
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct NewGameRequest {
|
pub struct NewGameRequest {
|
||||||
|
|
|
@ -27,7 +27,7 @@ fn server_sum_update(state: &Arc<AppState>) -> ServerStateSummary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, who: User) {
|
pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, mut who: User) {
|
||||||
// Add user to users
|
// Add user to users
|
||||||
//if doesn't exist
|
//if doesn't exist
|
||||||
let _true_if_not_exist = &state.users.lock().unwrap().insert(who.clone());
|
let _true_if_not_exist = &state.users.lock().unwrap().insert(who.clone());
|
||||||
|
@ -73,7 +73,7 @@ pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, wh
|
||||||
// handle new incoming messages
|
// handle new incoming messages
|
||||||
let mut recv_task = tokio::spawn(async move {
|
let mut recv_task = tokio::spawn(async move {
|
||||||
while let Some(Ok(message)) = receiver.next().await {
|
while let Some(Ok(message)) = receiver.next().await {
|
||||||
message_handler(message, &state, &who).await
|
message_handler(message, &state, &mut who).await
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::api::*;
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
use crate::Arc;
|
use crate::Arc;
|
||||||
|
|
||||||
pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &User) {
|
pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &mut User) {
|
||||||
let tx = &state.tx;
|
let tx = &state.tx;
|
||||||
|
|
||||||
match message {
|
match message {
|
||||||
|
@ -30,6 +30,17 @@ pub async fn message_handler(message: Message, state: &Arc<AppState>, who: &User
|
||||||
let msg = format! {"{0}: {1}", who.name, chat_message.text};
|
let msg = format! {"{0}: {1}", who.name, chat_message.text};
|
||||||
tracing::debug!("{msg}");
|
tracing::debug!("{msg}");
|
||||||
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
||||||
|
} else if let Ok(user_log_in) = serde_json::from_str::<UserLogIn>(&text) {
|
||||||
|
let old_name = who.name.clone();
|
||||||
|
let new_name = user_log_in.username.clone();
|
||||||
|
who.change_name(user_log_in.username);
|
||||||
|
let msg = format!{
|
||||||
|
"{0} changed name to {1}.",
|
||||||
|
old_name,
|
||||||
|
new_name
|
||||||
|
};
|
||||||
|
tracing::debug!("{msg}");
|
||||||
|
let _res = tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap());
|
||||||
} else {
|
} else {
|
||||||
tracing::debug!("Unhandled message: {}", &text);
|
tracing::debug!("Unhandled message: {}", &text);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue