//! Ranks: earned from points (reactions and "Ez segített" marks received) //! and time spent as a member, so nobody climbs in a few days. A rank is //! also a permission level. Buying a package gives no rank: it only lifts the //! newcomer's daily limits (docs/kozosseg.md). use serde::Serialize; use time::OffsetDateTime; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)] #[serde(rename_all = "snake_case")] pub enum Rank { Vandor, Kereso, Fono, Csillagfono, Sorsfono, } /// (rank, points needed, days of membership needed), lowest first. const LADDER: [(Rank, i32, i64); 5] = [ (Rank::Vandor, i32::MIN, 0), (Rank::Kereso, 5, 3), (Rank::Fono, 30, 14), (Rank::Csillagfono, 100, 45), (Rank::Sorsfono, 300, 120), ]; /// What an "Ez segített" mark is worth. pub const HELPED_POINTS: i32 = 5; /// Newcomer limits (lifted by a purchase): new threads and posts in 24 hours. pub const NEWCOMER_THREADS_PER_DAY: i64 = 3; pub const NEWCOMER_POSTS_PER_DAY: i64 = 20; /// A post whose reactions add up to this or less is shown collapsed. pub const COLLAPSE_SCORE: i64 = -3; impl Rank { pub fn of(points: i32, member_since: OffsetDateTime, now: OffsetDateTime) -> Rank { let days = (now - member_since).whole_days(); LADDER.iter().rev().find(|(_, p, d)| points >= *p && days >= *d).map(|(r, _, _)| *r).unwrap_or(Rank::Vandor) } pub fn name(self) -> &'static str { match self { Rank::Vandor => "Vándor", Rank::Kereso => "Kereső", Rank::Fono => "Fonó", Rank::Csillagfono => "Csillagfonó", Rank::Sorsfono => "Sorsfonó", } } /// A newcomer: daily limits, reactions that do not count yet, no ⌄. pub fn is_newcomer(self) -> bool { self == Rank::Vandor } /// May use the ⌄ (never in a listening topic). pub fn may_downvote(self) -> bool { self >= Rank::Kereso } /// The next rank and what it needs, for the member's own profile. pub fn next(self) -> Option<(Rank, i32, i64)> { LADDER.iter().find(|(r, _, _)| *r > self).copied() } } #[cfg(test)] mod tests { use super::*; #[test] fn ranks_need_points_and_time() { let now = OffsetDateTime::now_utc(); let days = |d: i64| now - time::Duration::days(d); assert_eq!(Rank::of(0, days(400), now), Rank::Vandor); assert_eq!(Rank::of(500, days(1), now), Rank::Vandor, "points alone are not enough"); assert_eq!(Rank::of(5, days(3), now), Rank::Kereso); assert_eq!(Rank::of(40, days(20), now), Rank::Fono); assert_eq!(Rank::of(40, days(400), now), Rank::Fono); assert_eq!(Rank::of(300, days(120), now), Rank::Sorsfono); assert_eq!(Rank::Vandor.next().map(|n| n.0), Some(Rank::Kereso)); assert_eq!(Rank::Sorsfono.next(), None); assert!(!Rank::Vandor.may_downvote() && Rank::Kereso.may_downvote()); } }