summaryrefslogtreecommitdiff
path: root/src/conn.rs
diff options
context:
space:
mode:
authorstale <redkugelblitzin@gmail.com>2022-08-21 04:53:12 -0300
committerstale <redkugelblitzin@gmail.com>2022-08-21 04:53:12 -0300
commit5e207ab59b3d5ce2ce316d8727886aa66147f758 (patch)
tree90ede1c22d238bf3c8bf6615701bb6803147588f /src/conn.rs
parente28498c4953f2da5f56a41f3123a413efa9d9a25 (diff)
questioning tiles and irc bot mess
Diffstat (limited to 'src/conn.rs')
-rw-r--r--src/conn.rs62
1 files changed, 36 insertions, 26 deletions
diff --git a/src/conn.rs b/src/conn.rs
index 2b6ee80..c8a0a82 100644
--- a/src/conn.rs
+++ b/src/conn.rs
@@ -7,6 +7,7 @@ use tokio::sync::RwLock;
use futures::{SinkExt, TryStreamExt, StreamExt, stream::SplitStream};
use warp::ws::{ WebSocket, Message };
use crate::livepos;
+use crate::ircbot;
pub async fn setup_conn(socket: WebSocket, addr: SocketAddr, rinfo: (RoomId,Arc<RwLock<Room>>), max_in: usize) {
let (room_id, room) = rinfo;
@@ -61,9 +62,9 @@ pub async fn setup_conn(socket: WebSocket, addr: SocketAddr, rinfo: (RoomId,Arc<
pub async fn drive_conn(conn: (Conn, SplitStream<WebSocket>), rinfo: (RoomId, Arc<RwLock<Room>>), max_in: usize) {
let (conn, mut incoming) = conn;
let (room_id, room) = rinfo;
- let (players, cmd_tx, pos_tx, room_conf) = {
+ let (players, cmd_tx, pos_tx, irc_tx, room_conf) = {
let room = room.read().await;
- (room.players.clone(), room.cmd_stream.clone(), room.pos_stream.clone(), room.conf.clone())
+ (room.players.clone(), room.cmd_stream.clone(), room.pos_stream.clone(), room.irc_stream.clone(), room.conf.clone())
};
while let Ok(cmd) = incoming.try_next().await {
if let Some(cmd) = cmd {
@@ -144,33 +145,42 @@ pub async fn drive_conn(conn: (Conn, SplitStream<WebSocket>), rinfo: (RoomId, Ar
if n.is_empty() { def } else { n }
}
};
- println!("{room_id} I: registered \"{name}@{}\"", conn.addr);
- drop(players_lock);
- let uid = {
- // new scope cuz paranoid bout deadlocks
- room.write().await.players.write().await.insert_conn(conn.clone(), name.clone(), clr)
- };
- let players_lock = players.read().await;
- let me = players_lock.get(&conn.addr).unwrap();
- conn.tx.send(Message::text(format!("regack {} {} {} {}",
- room_conf.name.replace(' ', "&nbsp;"), name.replace(' ', "&nbsp;"), uid, room_conf.board_conf))
- ).expect("couldn't send register ack");
- {
- let msg = Message::text(format!("players {}",
- jsonenc_players(players_lock.values())
- .expect("couldn't JSONify players")));
- for p in players_lock.values() {
- if let Err(e) = p.conn.tx.send(msg.clone()) {
- println!("{room_id} E: couldn't dump players for {me}: {e}");
+ let (nameq_tx, nameq_rx) = tokio::sync::oneshot::channel();
+ irc_tx.send(ircbot::IrcCmd::NameTakenQuery(name.clone(), nameq_tx)).expect("couldn't check for name collision");
+
+ if nameq_rx.await.unwrap() {
+ println!("{room_id} I: name collision \"{name}@{}\"", conn.addr);
+ conn.tx.send(Message::text("namecoll")).expect("couldn't send name collision report");
+ } else {
+ println!("{room_id} I: registered \"{name}@{}\"", conn.addr);
+ drop(players_lock);
+ let uid = {
+ // new scope cuz paranoid bout deadlocks
+ room.write().await.players.write().await.insert_conn(conn.clone(), name.clone(), clr)
+ };
+ let players_lock = players.read().await;
+ let me = players_lock.get(&conn.addr).unwrap();
+ conn.tx.send(Message::text(format!("regack {} {} {} {}",
+ room_conf.name.replace(' ', "&nbsp;"), name.replace(' ', "&nbsp;"), uid, room_conf.board_conf))
+ ).expect("couldn't send register ack");
+
+ {
+ let msg = Message::text(format!("players {}",
+ jsonenc_players(players_lock.values())
+ .expect("couldn't JSONify players")));
+ for p in players_lock.values() {
+ if let Err(e) = p.conn.tx.send(msg.clone()) {
+ println!("{room_id} E: couldn't dump players for {me}: {e}");
+ }
}
}
- }
- if let Err(e) = pos_tx.send(livepos::Req { id: uid, data: livepos::ReqData::StateDump }) {
- println!("{room_id} E: couldn't request position dump for {me}: {e}");
- }
- if let Err(e) = cmd_tx.send(MetaMove::StateDump) {
- println!("{room_id} E: couldn't request game dump for {me}: {e}");
+ if let Err(e) = pos_tx.send(livepos::Req { id: uid, data: livepos::ReqData::StateDump }) {
+ println!("{room_id} E: couldn't request position dump for {me}: {e}");
+ }
+ if let Err(e) = cmd_tx.send(MetaMove::StateDump) {
+ println!("{room_id} E: couldn't request game dump for {me}: {e}");
+ }
}
}
}