aboutsummaryrefslogtreecommitdiff
path: root/server/src/main.rs
blob: 06786c05e93b9341c9e61743b0c070424f2b5ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use anyhow::Result;
use log::{debug, info};
use std::{sync::Arc, time::Duration};
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::TcpListener,
    spawn,
    sync::{broadcast, RwLock},
    time::sleep,
};
use undercooked::{
    game::Game,
    protocol::{PacketC, PacketS},
};

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init_from_env("LOG");
    let listener = TcpListener::bind("0.0.0.0:27031").await?;
    info!("listening on {}", listener.local_addr()?);

    let game = Arc::new(RwLock::new(Game::new()));
    let (tx, rx) = broadcast::channel::<PacketC>(1024);

    {
        let game = game.clone();
        spawn(async move {
            {
                let mut g = game.write().await;
                while let Some(p) = g.packet_out() {
                    debug!("-> {p:?}");
                    let _ = tx.send(p);
                }
            }
            sleep(Duration::from_millis(10)).await;
        });
    }

    for id in 0.. {
        let (sock, addr) = listener.accept().await?;
        let (read, mut write) = sock.into_split();
        let game = game.clone();
        let mut rx = rx.resubscribe();
        info!("{addr} connected");
        spawn(async move {
            while let Ok(packet) = rx.recv().await {
                write
                    .write_all(serde_json::to_string(&packet).unwrap().as_bytes())
                    .await
                    .unwrap();
                write.write_all(b"\n").await.unwrap();
            }
        });
        spawn(async move {
            let mut read = BufReader::new(read).lines();
            while let Ok(Some(line)) = read.next_line().await {
                let packet: PacketS = serde_json::from_str(&line).unwrap();
                debug!("<- {id} {packet:?}");
                game.write().await.packet_in(id, packet).unwrap();
            }
        });
    }

    Ok(())
}