52 lines
1.8 KiB
Rust
52 lines
1.8 KiB
Rust
use chrono::{Datelike, Local};
|
|
use equatorprice::Level;
|
|
use serde::Serialize;
|
|
use sqlx::SqlitePool;
|
|
|
|
use super::{logbook::Logbook, stat::Stat, user::User};
|
|
|
|
pub(crate) mod equatorprice;
|
|
pub(crate) mod rowingbadge;
|
|
|
|
#[derive(Serialize)]
|
|
pub(crate) struct Achievements {
|
|
pub(crate) equatorprice: equatorprice::Next,
|
|
pub(crate) curr_equatorprice_name: String,
|
|
pub(crate) new_equatorprice_this_season: bool,
|
|
pub(crate) rowingbadge: Option<rowingbadge::Status>,
|
|
pub(crate) all_time_km: i32,
|
|
pub(crate) year_first_mentioned: Option<i32>,
|
|
pub(crate) year_last_mentioned: Option<i32>,
|
|
}
|
|
|
|
impl Achievements {
|
|
pub(crate) async fn for_user(db: &SqlitePool, user: &User) -> Self {
|
|
let rowed_km = Stat::total_km(db, user).await.rowed_km;
|
|
let rowed_km_this_season = if Local::now().month() == 1 {
|
|
Stat::person(db, Some(Local::now().year() - 1), user)
|
|
.await
|
|
.rowed_km
|
|
+ Stat::person(db, Some(Local::now().year()), user)
|
|
.await
|
|
.rowed_km
|
|
} else {
|
|
Stat::person(db, Some(Local::now().year()), user)
|
|
.await
|
|
.rowed_km
|
|
};
|
|
|
|
let new_equatorprice_this_season =
|
|
Level::curr_level(rowed_km) != Level::curr_level(rowed_km - rowed_km_this_season);
|
|
|
|
Self {
|
|
equatorprice: equatorprice::Next::new(rowed_km),
|
|
curr_equatorprice_name: equatorprice::Level::curr_level(rowed_km).desc().to_string(),
|
|
new_equatorprice_this_season,
|
|
rowingbadge: rowingbadge::Status::for_user(db, user).await,
|
|
all_time_km: rowed_km,
|
|
year_first_mentioned: Logbook::year_first_logbook_entry(db, user).await,
|
|
year_last_mentioned: Logbook::year_last_logbook_entry(db, user).await,
|
|
}
|
|
}
|
|
}
|