feat(bonus): rewrite

This commit is contained in:
catvayor 2024-06-12 15:11:23 +02:00
parent a00df617c5
commit 08dfa32088
2 changed files with 133 additions and 11 deletions

View file

@ -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<Event> 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<EventStream![Event + 'a]> {
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/<id>/vanish")]
async fn activate_invisibility(
id: &str,
tracking: &State<Tracking>,
evt_queues: &State<TrackingEventQueue>,
admin_queue: &State<AdminEventQueue>,
) -> 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/<id>/blur")]
async fn activate_blur(
id: &str,
tracking: &State<Tracking>,
evt_queues: &State<TrackingEventQueue>,
admin_queue: &State<AdminEventQueue>,
) -> 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! {

View file

@ -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 += `<button>
if(state.Conscrit.invisibility_codes > 0)
code_buttons += `<button onclick="fetch('/track/{{id}}/vanish', { method: 'PUT' })">
Invisibilité
</button>`;
if(state.Conscrit.blur_codes > 0)//onclick="socket.emit('useCode', 'blurred')"
code_buttons += `<button>
if(state.Conscrit.blur_codes > 0)
code_buttons += `<button onclick="fetch('/track/{{id}}/blur', { method: 'PUT' })">
Brouillage
</button>`;
document.getElementById(`codes`).innerHTML = code_buttons;