diff options
Diffstat (limited to 'src/conn.rs')
-rw-r--r-- | src/conn.rs | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/src/conn.rs b/src/conn.rs index 5325bf4..01bc1e8 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -26,23 +26,23 @@ pub async fn lobby(socket: WebSocket, addr: SocketAddr, room: Arc<RwLock<Room>>) let mut fields = cmd.split(" "); if fields.next() == Some("register") { - let all_fields = fields.collect::<Vec<&str>>(); - let fcount = all_fields.len(); - let mut name = "anon".to_string(); - if fcount >= 2 { - 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(); - } - println!("registered {name}"); + let mut all_fields = fields.collect::<Vec<&str>>(); + let clr = all_fields.pop().expect("register without color").chars().filter(|c| c.is_digit(16) || *c == '#').collect::<String>(); + let name = { + let def = "anon".to_string(); + if all_fields.is_empty() { def } + else { + let n = all_fields.join(" "); + if n.is_empty() { def } else { n } + } + }; + println!("registered \"{name}@{addr}\""); let uid = { // new scope cuz paranoid bout deadlocks let conn = Conn { addr, tx: tx.clone() }; - room.write().await.players.insert_conn(conn, name, clr).await + room.write().await.players.insert_conn(conn, name.clone(), clr).await }; - tx.send(Message::text(format!("id {uid}"))).expect("couldn't send register ack"); + tx.send(Message::text(format!("regack {} {uid} {}", name.replace(" ", " "), room.read().await.board_conf))).expect("couldn't send register ack"); if let Err(e) = room.read().await.cmd_stream.send(MetaMove::Dump) { println!("couldn't request game dump in behalf of {addr}: {e}"); } @@ -72,15 +72,13 @@ pub async fn lobby(socket: WebSocket, addr: SocketAddr, room: Arc<RwLock<Room>>) let room_lock = room.read().await; let mut players = room_lock.players.write().await; - if players.contains_key(&addr) { - players.remove(&addr); - } - for (paddr, p) in players.iter() { - if let Err(e) = p.conn.tx.send(Message::text("logoff {p.seqid} {p.name}")) { - println!("couldn't deliver logoff info to \"{}\"@{}: {}", p.name, paddr, e); + let disconn_p = players.remove(&addr); + for p in players.values() { + if let Err(e) = p.conn.tx.send(Message::text("logoff {disconn_p.seqid} {disconn_p.name}")) { + println!("couldn't deliver logoff info to {}: {}", p, e); } } - println!("{addr} disconnected"); + println!("{} disconnected", if let Some(p) = disconn_p { p.to_string() } else { addr.to_string() }); } pub async fn handle_room(mut incoming: SplitStream<WebSocket>, addr: SocketAddr, room: Arc<RwLock<Room>>) { |