diff --git a/db.md b/db.md index 4810880..20b4813 100644 --- a/db.md +++ b/db.md @@ -20,8 +20,6 @@ **Games**: Directories with the name of the game, with contents that define the game -// TODO figure out how to implement the game type itself - **Table**: game table - **Game Settigns**: Game specific settings (might not be needed) diff --git a/readme.md b/readme.md index 7fbfff7..d98dd86 100644 --- a/readme.md +++ b/readme.md @@ -5,8 +5,8 @@ [ ] implement tavern functions [ ] token drag & drop [ ] Chat - [ ] Send new chat messages - [ ] recv new chat messages + [x] Send new chat messages + [x] recv new chat messages [ ] roll a die when chat message requests that [ ] figure out how to zoom on the mouse instead of the center of the div [ ] data reqs @@ -16,7 +16,7 @@ [ ] impl different requests [ ] actual normal login [ ] allow sending of old info -[ ] chat history +[x] chat history [ ] send texture (map/token/image) [ ] force show something [ ] mouse ping (ideally multiple types) diff --git a/src/lib.rs b/src/lib.rs index 0cb9552..af1e2be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ impl GameServer { } } - pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender) { + pub async fn server_loop(mut self, mut msgs: mpsc::Receiver<(String, api::Request)>, broadcast: broadcast::Sender<(Option, api::Response)>) { while let Some(req) = msgs.recv().await { // TODO: do stuff yo! let (id, req) = req; @@ -44,9 +44,9 @@ impl GameServer { msg.source = id.clone(); } self.chat.push((id.clone(), msg.clone())); - _ = broadcast.send(api::Response::Message(msg)); + _ = broadcast.send((None, api::Response::Message(msg))); }, - api::Request::Quit => { _ = broadcast.send(api::Response::Quit { id })}, + api::Request::Quit => { _ = broadcast.send((None, api::Response::Quit { id }))}, api::Request::Shutdown => todo!(), api::Request::GetChatHistory { mut amount, from: last_msg } => { if amount == 0 { amount = self.chat.len(); } @@ -55,7 +55,7 @@ impl GameServer { .take(amount) .map(|m| m.1.clone()) .collect(); - _ = broadcast.send(api::Response::GetChatHistory(history)); + _ = broadcast.send((Some(id), api::Response::GetChatHistory(history))); }, api::Request::GetLastMessages { mut amount } => { if amount == 0 { amount = self.chat.len(); } @@ -64,10 +64,10 @@ impl GameServer { .skip(start) .map(|m| m.1.clone()) .collect(); - _ = broadcast.send(api::Response::GetChatHistory(history)); + _ = broadcast.send((Some(id), api::Response::GetChatHistory(history))); }, } } - _ = broadcast.send(api::Response::Shutdown); + _ = broadcast.send((None, api::Response::Shutdown)); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e6a775e..309c395 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ async fn main() { axum::serve(listener, app).await.expect("axum server crashed, yaaaaay (unless i crashed him that yay)"); } -async fn ws_handler(ws: ws::WebSocketUpgrade, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver) -> impl axum::response::IntoResponse { +async fn ws_handler(ws: ws::WebSocketUpgrade, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<(Option, Response)>) -> impl axum::response::IntoResponse { ws.on_upgrade(|w| handle_socket(w, msend, brecv)) } @@ -50,16 +50,18 @@ async fn socket_receiver(mut recv: SplitStream, msend: mpsc::Send } } -async fn socket_sender(mut send: SplitSink, mut brecv: broadcast::Receiver) { - while let Ok(msg) = brecv.recv().await { - let err = send.send(ws::Message::Text(serde_json::to_string(&msg).unwrap())).await.is_err(); - if err { - break; +async fn socket_sender(id: String, mut send: SplitSink, mut brecv: broadcast::Receiver<(Option, Response)>) { + while let Ok((to_id, msg)) = brecv.recv().await { + if to_id.is_none() || to_id.map(|t| t == id).unwrap_or(false) { + let err = send.send(ws::Message::Text(serde_json::to_string(&msg).unwrap())).await.is_err(); + if err { + break; + } } } } -async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver) { +async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, Request)>, brecv: broadcast::Receiver<(Option, Response)>) { let mut id: Option = None; loop { if let Some(msg) = socket.recv().await { @@ -101,8 +103,8 @@ async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, R if let Some(id) = id { println!("Got id for socket: {}", &id); let (send, recv) = socket.split(); - tokio::spawn(socket_receiver(recv, msend, id)); - tokio::spawn(socket_sender(send, brecv)); + tokio::spawn(socket_receiver(recv, msend, id.clone())); + tokio::spawn(socket_sender(id, send, brecv)); } println!("Done with so-cat"); } diff --git a/src/table/texture.rs b/src/table/texture.rs index 38dda70..09657df 100644 --- a/src/table/texture.rs +++ b/src/table/texture.rs @@ -7,7 +7,7 @@ pub enum Grid { } /// Texture directory source and file pub enum TextureSource { - // TODO: I think os string would better fit, to do later + // TODO: I think os string would better fit /// Server's shared textures directory Shared(String), /// Custom table directory