From c9272f5580b00180738eb6dde623ea0abb37f7af Mon Sep 17 00:00:00 2001 From: stale Date: Sat, 30 Apr 2022 17:32:53 -0300 Subject: lazy sync --- src/main.rs | 74 ++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 25 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 28a10ed..8f556c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,12 +26,16 @@ use tokio::fs; use tokio_tungstenite::tungstenite::protocol::Message; type Tx = UnboundedSender; -type MovReqTx = mpsc::UnboundedSender; +type MovReqTx = mpsc::UnboundedSender; type PeerMap = Arc>>; type PeerInfo = (PeerMap, Arc::); -// Which will either perform a move, or simply request the current board -type BoardRequest = Option; +#[derive(Debug)] +enum MetaMove { + Move(Move), + Dump, + Reset, +} const PAGE_RELPATH: &str = "./page.html"; const FONT_FILE_FUCKIT: &[u8] = include_bytes!("./VT323-Regular.ttf"); @@ -71,18 +75,30 @@ async fn main() { } // If a move is made, broadcast new board, else just send current board -async fn gameloop(mut move_rx: mpsc::UnboundedReceiver, peers: PeerMap) { - let mut game = Game::new(Board::new(30,10), (30*10)/8); +async fn gameloop(mut move_rx: mpsc::UnboundedReceiver, peers: PeerMap) { + let mut game = Game::new(Board::new(75,35), (75*35)/8); while let Some(req) = move_rx.recv().await { - let m = req; - if let Some(m) = m { - game = game.act(m); - } - let reply = Message::binary(game.board.render(None)); - { - let peers = peers.read().await; - for (_, (tx, _, _, _)) in peers.iter() { - tx.unbounded_send(reply.clone()).unwrap(); + let mut done = game.phase == Phase::Die || game.phase == Phase::Win; + match req { + MetaMove::Move(m) => if !done { game = game.act(m)}, + MetaMove::Dump => (), + MetaMove::Reset => { game = Game::new(Board::new(75,35), (75*35)/8); done = false;}, + } + if !done { + let mut reply = vec![]; + match game.phase { + Phase::Win => { reply.push(Message::text("win")); game.board = game.board.grade(); }, + Phase::Die => { reply.push(Message::text("lose")); game.board = game.board.grade(); }, + _ => (), + } + reply.push(Message::binary(game.board.render())); + { + let peers = peers.read().await; + for (_, (tx, _, _, _)) in peers.iter() { + for r in reply.iter() { + tx.unbounded_send(r.clone()).unwrap(); + } + } } } } @@ -112,25 +128,15 @@ async fn peer_connection(peer_info: PeerInfo, cmd_tx: MovReqTx, raw_stream: TcpS // Insert the write part of this peer to the peer map. let (tx, rx) = unbounded(); - peer_map.write().await.insert(addr, (tx.clone(), peer_seqid, "Dummy".to_string(), (0,0))); let (outgoing, mut incoming) = ws_stream.split(); - { // 1 - Inform the new player its id - tx.unbounded_send(Message::text(format!("id {}", peer_seqid))).unwrap(); - } - - { // 2 - Queue up a current board broadcast - cmd_tx.send(None).unwrap(); - } - let process_incoming = async { while let Some(cmd) = incoming.try_next().await.unwrap() { let cmd = cmd.to_text().unwrap(); - println!("Received a message from {}: {}", addr, cmd); + let mut fields = cmd.split(" ").skip(1); if cmd.starts_with("pos") { - let mut fields = cmd.split(" ").skip(1); let pos = (fields.next().unwrap().parse::().unwrap(), fields.next().unwrap().parse::().unwrap()); let (name, id) = { let mut peers = peer_map.write().await; @@ -144,6 +150,24 @@ async fn peer_connection(peer_info: PeerInfo, cmd_tx: MovReqTx, raw_stream: TcpS peer_tx.unbounded_send(Message::text(format!("pos {} {} {} {}", id, name, pos.0, pos.1))).unwrap(); } } + } else if cmd.starts_with("reveal") { + println!("got {} from {}", cmd, addr); + let pos = (fields.next().unwrap().parse::().unwrap(), fields.next().unwrap().parse::().unwrap()); + cmd_tx.send(MetaMove::Move(Move { t: MoveType::Reveal, pos })).unwrap(); + } else if cmd.starts_with("flag") { + println!("got {} from {}", cmd, addr); + let pos = (fields.next().unwrap().parse::().unwrap(), fields.next().unwrap().parse::().unwrap()); + cmd_tx.send(MetaMove::Move(Move { t: MoveType::ToggleFlag, pos })).unwrap(); + } else if cmd.starts_with("reset") { + println!("got {} from {}", cmd, addr); + cmd_tx.send(MetaMove::Reset).unwrap(); + } else if cmd.starts_with("register") { + let name = fields.next().unwrap(); + { // new scope cuz paranoid bout deadlocks + peer_map.write().await.insert(addr, (tx.clone(), peer_seqid, name.to_string(), (0,0))); + } + tx.unbounded_send(Message::text(format!("id {}", peer_seqid))).unwrap(); + cmd_tx.send(MetaMove::Dump).unwrap(); } } }; -- cgit v1.2.3