diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs index fe55ab2..0a17346 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{ - collections::HashMap, error::Error, net::SocketAddr, + sync::Arc, }; use warp::Filter; @@ -22,43 +22,47 @@ fn main() -> Result<(), Box<dyn Error>> { socket_addr: ([0,0,0,0],31235).into(), }; - let state = State { - conf, - peers: PeerMap::new(RwLock::new(HashMap::new())), - }; - - tokio_main(state) + tokio_main(conf) } #[tokio::main] -async fn tokio_main(state: State) -> Result<(), Box<dyn Error>> { - // Start the temporary single lobby - let (cmd_tx, cmd_rx) = tokio::sync::mpsc::unbounded_channel(); - let _game_t = tokio::spawn(gameloop(cmd_rx, state.peers.clone())); - - let cmd_filter = warp::any().map(move || cmd_tx.clone()); +async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> { + // Start the temporary single room + let room = Arc::new(RwLock::new({ + let name = "Testing room".to_string(); + let players = PlayerMap::default(); + let bconf = BoardConf { w: 75, h: 35, mine_ratio: (1, 8) }; + let (cmd_tx, cmd_rx) = tokio::sync::mpsc::unbounded_channel(); + let handle = tokio::spawn(gameloop(cmd_rx, players.clone(), bconf)); + Room { + name, + players, + peer_limit: 32, + board_conf: bconf, + cmd_stream: cmd_tx, + driver: handle, + } + })); let page_route = { use warp::*; get() .and(path("font.ttf")) .map(|| FONT_FILE) - .or(fs::file(state.conf.page_path.clone())) + .or(fs::file(conf.page_path.clone())) }; let websocket_route = { - let state = state.clone(); + let room = room.clone(); use warp::*; path("ws") .and(ws()) .and(addr::remote()) - .and(cmd_filter) - .map(move |ws: warp::ws::Ws, saddr: Option<SocketAddr>, cmd_tx: CmdTx| { - let state = state.clone(); + .map(move |ws: warp::ws::Ws, saddr: Option<SocketAddr>| { + let room = room.clone(); println!("conn from {saddr:?}"); ws.on_upgrade(move |socket| { - let conn_data = types::ConnData { remote_addr: saddr.unwrap(), cmd_tx: cmd_tx.clone(), peers: state.peers.clone() }; - conn::peer_connection(socket, conn_data) + conn::lobby(socket, saddr.expect("socket without address"), room.clone()) }) }) }; @@ -66,18 +70,19 @@ async fn tokio_main(state: State) -> Result<(), Box<dyn Error>> { let server = warp::serve(routes) .tls() - .cert_path(state.conf.cert_path) - .key_path(state.conf.pkey_path) - .run(state.conf.socket_addr); - println!("Serving on {}", state.conf.socket_addr); + .cert_path(conf.cert_path) + .key_path(conf.pkey_path) + .run(conf.socket_addr); + println!("Serving on {}", conf.socket_addr); server.await; Ok(()) } // If a move is made, broadcast new board, else just send current board -async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, peers: PeerMap) { +async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, players: PlayerMapData, bconf: BoardConf) { use minesweeper::*; - let mut game = Game::new(Board::new(75,35), (75*35)/8); + let mine_cnt = (bconf.w * bconf.h * bconf.mine_ratio.0)/(bconf.mine_ratio.1); + let mut game = Game::new(Board::new(bconf.w, bconf.h), mine_cnt); let mut latest_player_name = None; while let Some(req) = move_rx.recv().await { let done = game.phase == Phase::Die || game.phase == Phase::Win; @@ -87,10 +92,10 @@ async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, p if game.phase == Phase::Win || game.phase == Phase::Die { game.board = game.board.grade(); } - latest_player_name = peers.read().await.get(&o).map(|p| p.name.clone()); + latest_player_name = players.read().await.get(&o).map(|p| p.name.clone()); }, MetaMove::Dump => (), - MetaMove::Reset => { game = Game::new(Board::new(75,35), (75*35)/8); }, + MetaMove::Reset => { game = Game::new(Board::new(bconf.w, bconf.h), mine_cnt); }, } use warp::ws::Message; let mut reply = vec![Message::binary(game.board.render())]; @@ -101,10 +106,10 @@ async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver<MetaMove>, p _ => (), } { - let peers = peers.read().await; + let peers = players.read().await; for (addr, p) in peers.iter() { for r in reply.iter() { - if let Err(e) = p.tx.send(r.clone()) { + if let Err(e) = p.conn.tx.send(r.clone()) { println!("couldn't send game update {r:?} to {addr}: {e}"); } } |