aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/progress.rs
blob: 170ed973536c9ad4fe0ebe47c62d4395136954e8 (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
use std::collections::HashMap;

use super::ui::{account::session::Session, error::MyResult};
use jellybase::database::Database;
use rocket::{post, State};

#[post("/n/<id>/progress?<t>")]
pub async fn r_player_progress(
    session: Session,
    db: &State<Database>,
    id: &str,
    t: Option<f64>,
) -> MyResult<()> {
    db.user_progess.fetch_and_update(&session.user.name, |p| {
        let mut m = p.unwrap_or_else(|| HashMap::new());
        if let Some(t) = t {
            m.insert(id.to_string(), t);
        } else {
            m.remove(&id.to_string());
        }
        if m.is_empty() {
            None
        } else {
            Some(m)
        }
    })?;
    Ok(())
}