diff options
author | stale <redkugelblitzin@gmail.com> | 2022-06-30 07:18:45 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-06-30 07:18:45 -0300 |
commit | 9a745bb56d592f9b02f94c1d681fe607670fae02 (patch) | |
tree | c8d0ba62c39b22fc3f1c20d88fa1178b8f03f68d /src | |
parent | f70c1df91afa0a211de0b743da6c0dad59fd23be (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.rs | 28 | ||||
-rw-r--r-- | src/livepos.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/minesweeper.rs | 9 | ||||
-rw-r--r-- | src/types.rs | 4 |
5 files changed, 18 insertions, 35 deletions
diff --git a/src/conn.rs b/src/conn.rs index f67121b..addf3c5 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -8,7 +8,6 @@ use tokio::sync::mpsc as tokio_mpsc; use futures::{SinkExt, StreamExt, TryStreamExt, stream::SplitStream}; use warp::ws::{ WebSocket, Message }; use crate::livepos; -use ammonia; const MAX_IN: usize = 2048; @@ -94,25 +93,10 @@ pub async fn handle_room(streams: RoomStreams, addr: SocketAddr, rinfo: (RoomId, match players_lock.get_mut(&addr) { Some(me) => match cmd_name { "pos" => { - match parse_pos(fields) { - Some(pos) => { - if let Err(e) = pos_tx.send(livepos::Req { id: me.uid, data: livepos::ReqData::Pos(pos) }) { - println!("{room_id} E: couldn't process {me}'s position update: {e}"); - }; - // me.position = pos.clone(); - // let sanitized_name = me.name.replace(" ", " ").to_string(); - // let msg = format!("pos {} {sanitized_name} {} {} {}", me.uid, me.clr, pos.0, pos.1); - // for peer_tx in players_lock.iter().filter(|(s, _)| **s != addr).map(|(_,p)| &p.conn.tx) { - // let r = peer_tx.send(Message::text(&msg)); - // if let Err(e) = r { - // println!("{room_id} E: error sending pos update: {e}"); - // } - // } - }, - None => { - // Too spammy - //println!("{room_id} E: bad position update from {me}"); - }, + if let Some(pos) = parse_pos(fields) { + if let Err(e) = pos_tx.send(livepos::Req { id: me.uid, data: livepos::ReqData::Pos(pos) }) { + println!("{room_id} E: couldn't process {me}'s position update: {e}"); + }; } }, "reveal" => { @@ -168,7 +152,7 @@ pub async fn handle_room(streams: RoomStreams, addr: SocketAddr, rinfo: (RoomId, let players_lock = players.read().await; let me = players_lock.get(&addr).unwrap(); tx.send(Message::text(format!("regack {} {} {} {}", - room_conf.name.replace(" ", " "), name.replace(" ", " "), uid, room_conf.board_conf)) + room_conf.name.replace(' ', " "), name.replace(' ', " "), uid, room_conf.board_conf)) ).expect("couldn't send register ack"); { @@ -201,7 +185,7 @@ pub async fn handle_room(streams: RoomStreams, addr: SocketAddr, rinfo: (RoomId, fn jsonenc_players<'a, I: IntoIterator<Item=&'a Player>>(players: I) -> Result<String, serde_json::Error> { let mut pairs = Vec::new(); for player in players { - pairs.push((player.uid, player.name.replace(" ", " "), player.clr.clone())); + pairs.push((player.uid, player.name.replace(' ', " "), player.clr.clone())); } serde_json::to_string(&pairs) } diff --git a/src/livepos.rs b/src/livepos.rs index be56b1e..9112755 100644 --- a/src/livepos.rs +++ b/src/livepos.rs @@ -33,7 +33,7 @@ pub async fn livepos(players: PlayerMapData, mut recv: tokio_mpsc::UnboundedRece }, ReqData::StateDump => { dirty.clear(); - dirty.extend(positions.keys().map(|x| *x)); + dirty.extend(positions.keys().copied()); }, ReqData::Quit => { positions.remove(&update.id); diff --git a/src/main.rs b/src/main.rs index 82f1c8e..07ba00e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -181,12 +181,12 @@ async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> { .and_then(move |id: String, websocket: warp::ws::Ws, saddr: Option<SocketAddr>| { let rooms = rooms_ws.clone(); async move { - let id = RoomId {0: id}; - match rooms.read().await.get(&id).map(|x| x.clone()) { + let id = RoomId(id); + match rooms.read().await.get(&id).cloned() { Some(r) => { println!("{id} I: conn from {saddr:?}"); Ok(websocket.on_upgrade(move |socket| { - conn::lobby(socket, saddr.expect("socket without address"), (id,r.clone())) + conn::lobby(socket, saddr.expect("socket without address"), (id,r)) })) }, None => { @@ -201,7 +201,7 @@ async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> { .then(move |id: String, f: fs::File| { let rooms = rooms_lobby.clone(); async move { - if rooms.read().await.contains_key(&RoomId {0: id}) { + if rooms.read().await.contains_key(&RoomId(id)) { f.into_response() } else { reply::with_status("No such room", http::StatusCode::BAD_REQUEST).into_response() @@ -258,7 +258,7 @@ async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, p board_encoder.write_all(&game.board.render()).unwrap(); let compressed_board = board_encoder.finish().unwrap(); let mut reply = vec![Message::binary(compressed_board)]; - let lpname = latest_player_name.as_ref().map(|s| s.as_str()).unwrap_or("unknown player").replace(" ", " "); + let lpname = latest_player_name.as_deref().unwrap_or("unknown player").replace(' ', " "); match game.phase { Phase::Win => { reply.push(Message::text(format!("win {lpname}"))); }, Phase::Die => { reply.push(Message::text(format!("lose {lpname}"))); }, diff --git a/src/minesweeper.rs b/src/minesweeper.rs index 288847e..9e362dc 100644 --- a/src/minesweeper.rs +++ b/src/minesweeper.rs @@ -122,7 +122,7 @@ impl Board { width: w, height: h, hidden_tiles: area, - mine_count: mine_count.clone(), + mine_count, }; b.spread_mines(mine_count) } @@ -195,7 +195,6 @@ impl Board { *c &= !(HIDDEN_BIT | FLAGGED_BIT); self.hidden_tiles -= 1; if *c > 0 { continue; } - drop(c); if let Some(mut adj) = self.neighs(pos) { queue.append(&mut adj); } @@ -207,9 +206,9 @@ impl Board { if let Some(off) = self.pos_to_off(pos) { self.flood_reveal(pos); let c = self.data[off]; - MoveResult { 0: self, 1: (c & !(FLAGGED_BIT | CORRECT_BIT)) == TILE_NUMBITS } + MoveResult(self, (c & !(FLAGGED_BIT | CORRECT_BIT)) == TILE_NUMBITS) } else { - MoveResult { 0: self, 1: false } + MoveResult(self, false) } } pub fn grade(mut self) -> Board { @@ -224,7 +223,7 @@ impl Board { if let Some(off) = self.pos_to_off(pos) { self.data[off] ^= FLAGGED_BIT; } - MoveResult { 0: self, 1: false } + MoveResult(self, false) } pub fn render(&self) -> Vec<u8> { diff --git a/src/types.rs b/src/types.rs index cb16042..f9f166a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -85,11 +85,11 @@ impl std::borrow::Borrow<str> for RoomId { impl RoomId { pub fn new_in<T>(map: &HashMap<RoomId, T>) -> Self { use rand::{ thread_rng, Rng, distributions::Alphanumeric }; - let id = RoomId { 0: thread_rng() + let id = RoomId(thread_rng() .sample_iter(&Alphanumeric) .take(16) .map(char::from) - .collect::<String>() }; + .collect::<String>()); if map.contains_key(&id) { RoomId::new_in(map) } else { id } } |