diff options
author | stale <redkugelblitzin@gmail.com> | 2022-04-30 17:32:53 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-04-30 17:32:53 -0300 |
commit | c9272f5580b00180738eb6dde623ea0abb37f7af (patch) | |
tree | 2659ae18c11ea3c08801bd0ad4aa6bd680aef9e9 /src/main.rs | |
parent | 9987d7abf6acd647b4a3a00c20d15f716dce653c (diff) |
lazy sync
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 74 |
1 files changed, 49 insertions, 25 deletions
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<Message>; -type MovReqTx = mpsc::UnboundedSender<BoardRequest>; +type MovReqTx = mpsc::UnboundedSender<MetaMove>; type PeerMap = Arc<RwLock<HashMap<SocketAddr, (Tx, usize, String, (usize, usize))>>>; type PeerInfo = (PeerMap, Arc::<AtomicUsize>); -// Which will either perform a move, or simply request the current board -type BoardRequest = Option<Move>; +#[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<BoardRequest>, peers: PeerMap) { - let mut game = Game::new(Board::new(30,10), (30*10)/8); +async fn gameloop(mut move_rx: mpsc::UnboundedReceiver<MetaMove>, 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::<usize>().unwrap(), fields.next().unwrap().parse::<usize>().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::<usize>().unwrap(), fields.next().unwrap().parse::<usize>().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::<usize>().unwrap(), fields.next().unwrap().parse::<usize>().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(); } } }; |