From 08dfa32088290ea337e8515ea0f68ccd865e6a88 Mon Sep 17 00:00:00 2001 From: catvayor Date: Wed, 12 Jun 2024 15:11:23 +0200 Subject: [PATCH] feat(bonus): rewrite --- src/main.rs | 136 ++++++++++++++++++++++++++++++++++-- templates/conscrit.html.hbs | 8 +-- 2 files changed, 133 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7a56a54..401be0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use rocket::{ serde::{json::Json, Deserialize, Serialize}, tokio::{ self, select, - time::{self, Duration, Instant}, + time::{self, sleep, Duration, Instant}, }, Shutdown, State, }; @@ -37,6 +37,8 @@ enum TrackedState { use TrackedState::{Conscrit, Vieux}; const BLURRED_MOVE: (f32, f32) = (0.0005, 0.0005); +const BONUS_TIMEOUT: Duration = Duration::from_millis(5000); +const EVENT_TIMEOUT: Duration = Duration::from_millis(100); impl TrackedState { fn invisible(&self) -> bool { @@ -142,7 +144,7 @@ struct QueuedEvent { impl QueuedEvent { fn expired(&self) -> bool { - self.date.elapsed() >= Duration::from_millis(100) + self.date.elapsed() >= EVENT_TIMEOUT } } impl From for QueuedEvent { @@ -255,8 +257,8 @@ fn tracked_view( if let Some(tracked) = tracking.read().unwrap().get(&id.to_string()) { Some(Template::render( match tracked.read().unwrap().state { - TrackedState::Vieux { .. } => "vieux", - TrackedState::Conscrit { .. } => "conscrit", + Vieux { .. } => "vieux", + Conscrit { .. } => "conscrit", }, context! { name: &tracked.read().unwrap().name, @@ -292,7 +294,7 @@ fn tracked_events<'a>( ) -> Option { if evt_queue.read().unwrap().contains_key(&id.to_string()) { Some(EventStream! { - let mut interval = time::interval(Duration::from_millis(100)); + let mut interval = time::interval(EVENT_TIMEOUT); loop { select!{ _ = interval.tick() =>{ @@ -364,7 +366,7 @@ fn admin_events<'a>( .collect(); Some(EventStream! { yield Event::json(&full_info).event("full_update"); - let mut interval = time::interval(Duration::from_millis(100)); + let mut interval = time::interval(EVENT_TIMEOUT); loop { select!{ _ = interval.tick() =>{ @@ -437,6 +439,124 @@ fn set_state( } } +#[put("/track//vanish")] +async fn activate_invisibility( + id: &str, + tracking: &State, + evt_queues: &State, + admin_queue: &State, +) -> Option<()> { + let tracking_lock = tracking.read().unwrap(); + let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap(); + if let Conscrit { + ref mut invisible, + ref mut invisibility_codes, + .. + } = tracked.state + { + if *invisibility_codes > 0 { + *invisibility_codes -= 1; + *invisible = true; + state_update(&tracked, &evt_queues, &admin_queue); + let track_clone = (*tracking).clone(); + let queue_clone = (*evt_queues).clone(); + let admin_clone = (*admin_queue).clone(); + let id_str = id.to_string(); + tokio::spawn(async move { + sleep(BONUS_TIMEOUT).await; + if let Conscrit { + ref mut invisible, .. + } = track_clone + .read() + .unwrap() + .get(&id_str) + .unwrap() + .write() + .unwrap() + .state + { + *invisible = false; + } + state_update( + &track_clone + .read() + .unwrap() + .get(&id_str) + .unwrap() + .read() + .unwrap(), + &queue_clone, + &admin_clone, + ); + }); + Some(()) + } else { + None + } + } else { + None + } +} + +#[put("/track//blur")] +async fn activate_blur( + id: &str, + tracking: &State, + evt_queues: &State, + admin_queue: &State, +) -> Option<()> { + let tracking_lock = tracking.read().unwrap(); + let tracked = &mut tracking_lock.get(&id.to_string()).unwrap().write().unwrap(); + if let Conscrit { + ref mut blurred, + ref mut blur_codes, + .. + } = tracked.state + { + if *blur_codes > 0 { + *blur_codes -= 1; + *blurred = true; + state_update(&tracked, &evt_queues, &admin_queue); + let track_clone = (*tracking).clone(); + let queue_clone = (*evt_queues).clone(); + let admin_clone = (*admin_queue).clone(); + let id_str = id.to_string(); + tokio::spawn(async move { + sleep(BONUS_TIMEOUT).await; + if let Conscrit { + ref mut blurred, .. + } = track_clone + .read() + .unwrap() + .get(&id_str) + .unwrap() + .write() + .unwrap() + .state + { + *blurred = false; + } + state_update( + &track_clone + .read() + .unwrap() + .get(&id_str) + .unwrap() + .read() + .unwrap(), + &queue_clone, + &admin_clone, + ); + }); + Some(()) + } else { + None + } + } else { + None + } +} + #[get("/")] fn index() -> &'static str { "Hello, world!" @@ -529,11 +649,13 @@ async fn rocket() -> _ { admin_page, admin_events, admin_set_state, + activate_invisibility, + activate_blur, ], ) .mount("/", FileServer::from(relative!("static"))); tokio::spawn(async move { - let mut clean_interval = time::interval(Duration::from_millis(500)); + let mut clean_interval = time::interval(5 * EVENT_TIMEOUT); let mut coord_interval = time::interval(Duration::from_millis(3000)); loop { select! { diff --git a/templates/conscrit.html.hbs b/templates/conscrit.html.hbs index 68fbe3c..2638405 100644 --- a/templates/conscrit.html.hbs +++ b/templates/conscrit.html.hbs @@ -49,12 +49,12 @@ function self_state_hook(state){ var code_buttons = ""; - if(state.Conscrit.invisibility_codes > 0)//onclick="socket.emit('useCode', 'invisibility')" - code_buttons += ``; - if(state.Conscrit.blur_codes > 0)//onclick="socket.emit('useCode', 'blurred')" - code_buttons += ``; document.getElementById(`codes`).innerHTML = code_buttons;