add: user achievements

This commit is contained in:
trisua 2025-06-27 03:45:50 -04:00
parent e7c4cf14aa
commit b860f74124
15 changed files with 318 additions and 11 deletions

View file

@ -1,6 +1,6 @@
use super::common::NAME_REGEX;
use oiseau::cache::Cache;
use crate::model::auth::UserConnections;
use crate::model::auth::{Achievement, AchievementRarity, Notification, UserConnections};
use crate::model::moderation::AuditLogEntry;
use crate::model::oauth::AuthGrant;
use crate::model::permissions::SecondaryPermission;
@ -111,6 +111,7 @@ impl DataManager {
associated: serde_json::from_str(&get!(x->20(String)).to_string()).unwrap(),
invite_code: get!(x->21(i64)) as usize,
secondary_permissions: SecondaryPermission::from_bits(get!(x->22(i32)) as u32).unwrap(),
achievements: serde_json::from_str(&get!(x->23(String)).to_string()).unwrap(),
}
}
@ -266,7 +267,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23)",
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)",
params![
&(data.id as i64),
&(data.created as i64),
@ -291,6 +292,7 @@ impl DataManager {
&serde_json::to_string(&data.associated).unwrap(),
&(data.invite_code as i64),
&(SecondaryPermission::DEFAULT.bits() as i32),
&serde_json::to_string(&data.achievements).unwrap(),
]
);
@ -707,6 +709,66 @@ impl DataManager {
Ok(())
}
/// Add an achievement to a user.
///
/// Still returns `Ok` if the user already has the achievement.
pub async fn add_achievement(&self, user: &User, achievement: Achievement) -> Result<()> {
if user
.achievements
.iter()
.find(|x| x.name == achievement.name)
.is_some()
{
return Ok(());
}
// send notif
self.create_notification(Notification::new(
"You've earned a new achievement!".to_string(),
format!(
"You've earned the \"{}\" [achievement](/achievements)!",
achievement.name.title()
),
user.id,
))
.await?;
// add achievement
let mut user = user.clone();
user.achievements.push(achievement);
self.update_user_achievements(user.id, user.achievements)
.await?;
Ok(())
}
/// Fill achievements with their title and description.
///
/// # Returns
/// `(name, description, rarity, achievement)`
pub fn fill_achievements(
&self,
mut list: Vec<Achievement>,
) -> Vec<(String, String, AchievementRarity, Achievement)> {
let mut out = Vec::new();
// sort by unlocked desc
list.sort_by(|a, b| a.unlocked.cmp(&b.unlocked));
list.reverse();
// ...
for x in list {
out.push((
x.name.title().to_string(),
x.name.description().to_string(),
x.name.rarity(),
x,
))
}
out
}
/// Validate a given TOTP code for the given profile.
pub fn check_totp(&self, ua: &User, code: &str) -> bool {
let totp = ua.totp(Some(
@ -857,6 +919,7 @@ impl DataManager {
auto_method!(update_user_settings(UserSettings)@get_user_by_id -> "UPDATE users SET settings = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_connections(UserConnections)@get_user_by_id -> "UPDATE users SET connections = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_associated(Vec<usize>)@get_user_by_id -> "UPDATE users SET associated = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_achievements(Vec<Achievement>)@get_user_by_id -> "UPDATE users SET achievements = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(get_user_by_stripe_id(&str)@get_user_from_row -> "SELECT * FROM users WHERE stripe_id = $1" --name="user" --returns=User);
auto_method!(update_user_stripe_id(&str)@get_user_by_id -> "UPDATE users SET stripe_id = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);

View file

@ -20,5 +20,6 @@ CREATE TABLE IF NOT EXISTS users (
stripe_id TEXT NOT NULL,
grants TEXT NOT NULL,
associated TEXT NOT NULL,
secondary_permissions INT NOT NULL
secondary_permissions INT NOT NULL,
achievements TEXT NOT NULL
)