Add social credit system

This commit is contained in:
joeyahines committed 2025-04-20 20:16:28 -06:00
1 parent 32fa1a11d1
commit 6b2d4641cc
10 files changed
+257 -5

No files matched your search

+1
View File
@@ -6,4 +6,5 @@ pub mod managed_roles;
pub mod motivation;
pub mod race;
pub mod random;
pub mod social_credit;
pub mod task;
+62
View File
@@ -0,0 +1,62 @@
use crate::error::Error;
use j_db::database::Database;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SocialCreditPhrase {
id: Option<u64>,
pub phrase: String,
pub lower_bound: i64,
pub upper_bound: i64,
}
impl j_db::model::JdbModel for SocialCreditPhrase {
fn id(&self) -> Option<u64> {
self.id
}
fn set_id(&mut self, id: u64) {
self.id = Some(id)
}
fn tree() -> String {
"SocialCreditPhrase".to_string()
}
fn check_unique(&self, other: &Self) -> bool {
other.phrase.to_lowercase() == self.phrase.to_lowercase()
}
}
impl SocialCreditPhrase {
pub fn check_if_match(db: &Database, msg: &str) -> Result<Option<Self>, Error> {
let lowercase_msg = msg.to_lowercase();
let matches = db
.filter(|_, phrase: &Self| lowercase_msg.contains(&phrase.phrase.to_lowercase()))?
.next();
Ok(matches)
}
pub fn add_new_phrase(
db: &Database,
phrase: String,
lower_bound: i64,
upper_bound: i64,
) -> Result<Self, Error> {
let new_phrase = db.insert(Self {
id: None,
phrase,
lower_bound,
upper_bound,
})?;
Ok(new_phrase)
}
pub fn get_phrases(db: &Database) -> Result<Vec<Self>, Error> {
let phrases = db.filter(|_, _phrase: &Self| true)?.collect();
Ok(phrases)
}
}