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
)

View file

@ -58,6 +58,9 @@ pub struct User {
/// Secondary permissions because the regular permissions struct ran out of possible bits.
#[serde(default)]
pub secondary_permissions: SecondaryPermission,
/// Users collect achievements through little actions across the site.
#[serde(default)]
pub achievements: Vec<Achievement>,
}
pub type UserConnections =
@ -297,6 +300,7 @@ impl User {
associated: Vec::new(),
invite_code: 0,
secondary_permissions: SecondaryPermission::DEFAULT,
achievements: Vec::new(),
}
}
@ -470,6 +474,92 @@ pub struct ExternalConnectionData {
pub data: HashMap<String, String>,
}
/// The total number of achievements needed to 100% Tetratto!
pub const ACHIEVEMENTS: usize = 8;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum AchievementName {
/// Create your first post.
CreatePost,
/// Follow somebody.
FollowUser,
/// Create your 50th post.
Create50Posts,
/// Create your 100th post.
Create100Posts,
/// Create your 1000th post.
Create1000Posts,
/// Ask your first question.
CreateQuestion,
/// Edit your settings.
EditSettings,
/// Create your first journal.
CreateJournal,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum AchievementRarity {
Common,
Uncommon,
Rare,
}
impl AchievementName {
pub fn title(&self) -> &str {
match self {
Self::CreatePost => "Dear friends,",
Self::FollowUser => "Virtual connections...",
Self::Create50Posts => "Hello, world!",
Self::Create100Posts => "It's my world",
Self::Create1000Posts => "Timeline domination",
Self::CreateQuestion => "Big questions...",
Self::EditSettings => "Just how I like it!",
Self::CreateJournal => "Dear diary...",
}
}
pub fn description(&self) -> &str {
match self {
Self::CreatePost => "Create your first post!",
Self::FollowUser => "Follow somebody!",
Self::Create50Posts => "Create your 50th post.",
Self::Create100Posts => "Create your 100th post.",
Self::Create1000Posts => "Create your 1000th post.",
Self::CreateQuestion => "Ask your first question!",
Self::EditSettings => "Edit your settings.",
Self::CreateJournal => "Create your first journal.",
}
}
pub fn rarity(&self) -> AchievementRarity {
match self {
Self::CreatePost => AchievementRarity::Common,
Self::FollowUser => AchievementRarity::Common,
Self::Create50Posts => AchievementRarity::Uncommon,
Self::Create100Posts => AchievementRarity::Uncommon,
Self::Create1000Posts => AchievementRarity::Rare,
Self::CreateQuestion => AchievementRarity::Common,
Self::EditSettings => AchievementRarity::Common,
Self::CreateJournal => AchievementRarity::Uncommon,
}
}
}
impl Into<Achievement> for AchievementName {
fn into(self) -> Achievement {
Achievement {
name: self,
unlocked: unix_epoch_timestamp(),
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Achievement {
pub name: AchievementName,
pub unlocked: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Notification {
pub id: usize,