diff --git a/src/main.rs b/src/main.rs index 5c7b0b9..f6a4798 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,34 +165,46 @@ type TrackingEventQueue = Arc for TrackedInfo { + fn from(admin_info: AdminTrackedInfo) -> TrackedInfo { + TrackedInfo { + name: admin_info.name, + pos: admin_info.pos, + color: admin_info.color, + } + } +} + fn base_view(team: &Tracked) -> TrackedInfo { TrackedInfo { name: team.name.clone(), pos: team.pos, - me: false, color: team.state.color(), } } -fn admin_view(team: &Tracked) -> TrackedInfo { - TrackedInfo { +fn admin_view(team: &Tracked) -> AdminTrackedInfo { + AdminTrackedInfo { name: team.name.clone(), pos: team.pos, - me: false, color: team.state.admin_color(), } } fn apparent_info(watcher: &Tracked, team: &Tracked) -> Option { if watcher.id == team.id { - Some(TrackedInfo { - me: true, - ..admin_view(team) - }) + None } else if let Conscrit { captured, mallette, .. } = watcher.state @@ -221,7 +233,7 @@ fn apparent_info(watcher: &Tracked, team: &Tracked) -> Option { } } } else { - Some(admin_view(team)) + Some(admin_view(team).into()) } } @@ -264,16 +276,19 @@ fn evts_to_send(id: &str, evt_queue: &TrackingEventQueue) -> Vec { #[get("/track//events")] fn tracked_events<'a>( id: &'a str, + tracking: &State, evt_queue: &'a State, mut shutdown: Shutdown, ) -> Option { if evt_queue.read().unwrap().contains_key(&id.to_string()) { + state_update(&tracking.read().unwrap().get(&id.to_string()).unwrap().read().unwrap(), &evt_queue); Some(EventStream! { let mut interval = time::interval(Duration::from_millis(100)); loop { select!{ _ = interval.tick() =>{ for evt in evts_to_send(id, evt_queue){ + //println!("{:?}", evt); yield evt; } }, @@ -286,29 +301,49 @@ fn tracked_events<'a>( } } +fn state_update(tracked: &Tracked, evt_queues: &TrackingEventQueue) { + evt_queues + .read() + .unwrap() + .get(&tracked.id) + .unwrap() + .write() + .unwrap() + .push_back(Event::json(&admin_view(tracked)).event("self_info").into()); +} + #[put("/track//pos?&")] -fn store_pos(id: &str, lat: f32, long: f32, tracking: &State) { +fn store_pos( + id: &str, + lat: f32, + long: f32, + tracking: &State, + evt_queues: &State, +) { if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { tracked.write().unwrap().pos = (lat, long); + state_update(&tracked.read().unwrap(), &evt_queues); } } #[put("/track//state?&")] -fn set_state(id: &str, inv: bool, col: u8, tracking: &State) -> Option<()> { +fn set_state( + id: &str, + inv: bool, + col: u8, + tracking: &State, + evt_queues: &State, +) -> Option<()> { let tracking_lock = tracking.read().unwrap(); - let state = &mut tracking_lock - .get(&id.to_string()) - .unwrap() - .write() - .unwrap() - .state; + let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap(); if let Vieux { ref mut invisible, ref mut color, - } = state + } = tracked.state { *invisible = inv; *color = col; + state_update(&tracked, &evt_queues); Some(()) } else { None diff --git a/static/utils.js b/static/utils.js index 4830334..fce0bab 100644 --- a/static/utils.js +++ b/static/utils.js @@ -1,5 +1,8 @@ var evtsource; var markers = []; +var self_marker; +var name; +var id; var CircleIcon = L.Icon.extend({ options: { @@ -73,36 +76,39 @@ function setup_map(){ }).addTo(map); L.polyline(map_border, {color: 'red'}).addTo(map); + self_marker = L.marker([0,0], {"icon": icons[0] }).addTo(map); + self_marker.setZIndexOffset(1000); + self_marker.bindPopup(name); } ////////////////////////////////////////////////////////////////////////////// // EVENT LISTENNING function setup_evtlisten_common(){ + evtSource = new EventSource("/track/"+id+"/events"); evtSource.addEventListener("coords", (event) => { const data = JSON.parse(event.data); - console.log(data); var i = 0; for (tracked of data) { if (i == markers.length) { markers.push(L.marker([0,0], {"icon": icons[0] }).addTo(map)); markers[i].bindPopup(""); + markers[i].setZIndexOffset(0); } markers[i].setLatLng(tracked.pos); markers[i].setPopupContent(tracked.name); - if (tracked.me) { - markers[i].setIcon(self_icons[tracked.color]); - markers[i].setZIndexOffset(1000); - } else { - markers[i].setIcon(icons[tracked.color]); - markers[i].setZIndexOffset(0); - } + markers[i].setIcon(icons[tracked.color]); ++i; } for (; i < markers.length; ++i) { markers[i].setLatLng([0,0]); } }); + evtSource.addEventListener("self_info", (event) => { + const data = JSON.parse(event.data); + self_marker.setLatLng(data.pos); + self_marker.setIcon(self_icons[data.color]); + }); //socket.on("popup", function(data){ // alert(data.content); diff --git a/templates/conscrit.html.hbs b/templates/conscrit.html.hbs index 21df987..ebe0dfa 100644 --- a/templates/conscrit.html.hbs +++ b/templates/conscrit.html.hbs @@ -36,6 +36,8 @@