diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.rs | 23 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/types.rs | 1 |
3 files changed, 15 insertions, 10 deletions
diff --git a/src/conn.rs b/src/conn.rs index fda3a2b..de0f704 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -47,17 +47,17 @@ pub async fn peer_connection(socket: warp::ws::WebSocket, conndata: ConnData) { "pos" => { match parse_pos(fields) { Some(pos) => { - let (name, id) = { + let (name, clr, id) = { let mut peers = peers.write().await; let mut entry = peers.get_mut(&addr).unwrap(); entry.position = pos.clone(); - (entry.name.clone(), entry.seq_id) + (entry.name.clone(), entry.clr.clone(), entry.seq_id) }; let sanitized_name = name.replace(" ", " ").to_string(); { let peers = peers.read().await; for peer_tx in peers.iter().filter(|(s, _)| **s != addr).map(|(_,p)| &p.tx) { - let r = peer_tx.send(Message::text(format!("pos {id} {sanitized_name} {} {}", pos.0, pos.1))); + let r = peer_tx.send(Message::text(format!("pos {id} {sanitized_name} {} {} {}", clr, pos.0, pos.1))); if let Err(e) = r { println!("error sending pos update: {e}"); } @@ -102,12 +102,16 @@ pub async fn peer_connection(socket: warp::ws::WebSocket, conndata: ConnData) { } }, "register" => { - let name = fields.collect::<Vec<&str>>().join(&" "); + let all_fields = fields.collect::<Vec<&str>>(); + let fcount = all_fields.len(); + peer_name = "anon".into(); let id; - if name.is_empty() { - peer_name = "anon".to_string(); - } else { - peer_name = name; + if fcount >= 2 { + peer_name = all_fields[..fcount-1].join(" "); + } + let mut clr = all_fields[fcount-1].chars().filter(|c| c.is_digit(16) || *c == '#').collect::<String>(); + if clr.is_empty() { + clr = "#f03333".into(); } { // new scope cuz paranoid bout deadlocks let mut peers = peers.write().await; @@ -116,10 +120,11 @@ pub async fn peer_connection(socket: warp::ws::WebSocket, conndata: ConnData) { tx: tx.clone(), seq_id: id, name: peer_name.clone(), + clr, position: (0,0) }); } - tx.send(Message::text(format!("id {id}"))).unwrap(); + tx.send(Message::text(format!("id {}", id))).unwrap(); if let Err(e) = cmd_tx.send(MetaMove::Dump) { println!("couldn't send game dump to \"{peer_name}\"@{addr}: {e}"); } diff --git a/src/main.rs b/src/main.rs index b3c3cf9..fe55ab2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use std::{ - convert::Infallible, collections::HashMap, error::Error, net::SocketAddr, diff --git a/src/types.rs b/src/types.rs index 73d51b8..54a93bc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -33,6 +33,7 @@ pub struct Peer { pub tx: tokio::sync::mpsc::UnboundedSender<Message>, pub seq_id: usize, pub name: String, + pub clr: String, pub position: (usize, usize), } |