summaryrefslogtreecommitdiff
path: root/src/livepos.rs
blob: b19eda6ffebfbd8191b18bccf21b7fe3270dca1a (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::types::*;
use std::sync::Arc;
use tokio::sync::mpsc as tokio_mpsc;
use tokio::sync::{ RwLock, Mutex };
use std::collections::{HashMap,HashSet};
use tokio::time::{self, Duration};
use warp::ws::Message;

pub enum ReqData {
    Pos((usize,usize)),
    StateDump,
    Quit,
}

pub struct Req {
    pub id: usize,
    pub data: ReqData,
}

pub async fn livepos(players: Arc<RwLock<PlayerMap>>, mut recv: tokio_mpsc::UnboundedReceiver<Req>) {
    let positions = Mutex::new(HashMap::new());
    let dirty = Mutex::new(HashSet::new());
    let process_upds = async {
        while let Some(update) = recv.recv().await {
            let mut dirty = dirty.lock().await;
            let mut positions = positions.lock().await;
            match update.data {
                ReqData::Pos(p) => {
                    let old = positions.get(&update.id).unwrap_or(&(0,0));
                    if p != *old {
                        dirty.insert(update.id);
                    }
                    positions.insert(update.id, p);
                },
                ReqData::StateDump => {
                    dirty.clear();
                    dirty.extend(positions.keys().copied());
                },
                ReqData::Quit => {
                    positions.remove(&update.id);
                    dirty.retain(|x| *x != update.id);
                }
            }
        }
    };
    let periodic_send = async {
        let mut interv = tokio::time::interval(Duration::from_millis(16));
        interv.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
        loop {
            interv.tick().await;
            let mut dirty = dirty.lock().await;
            if dirty.len() > 0 {
                let mut positions = positions.lock().await;
                let msg = jsonenc_ids(&mut positions, &*dirty).expect("couldn't JSONify player positions");
                dirty.clear();
                let plock = players.read().await;
                for player in plock.values() {
                    if let Err(e) = player.conn.tx.send(Message::text(format!("pos {}", msg))) {
                        println!("E: couldn't send livepos update to {}: {}", player, e);
                    }
                }
            }
        }
    };

    tokio::select!(
        _ = process_upds => (),
        _ = periodic_send => ()
    );
}

fn jsonenc_ids<'a, I: IntoIterator<Item=&'a usize>>(positions: &mut HashMap<usize, (usize,usize)>, ids: I) -> Result<String, serde_json::Error> {
    let mut pairs = Vec::new();
    for id in ids {
        pairs.push((id, positions[id]));
    };
    serde_json::to_string(&pairs)
}