97 lines
2.6 KiB
Rust
97 lines
2.6 KiB
Rust
use chrono::Datelike;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
|
pub struct UserState {
|
|
pub is_checkin_today: bool,
|
|
pub level: UserLevel,
|
|
pub checkin_unix_mills: u64,
|
|
pub checkin_times_in_month: usize,
|
|
pub checkin_days: Vec<u32>, //每月在哪天签到了
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum UserLevel {
|
|
Super = 0, //表示不受限制的用户,直接享受最高权限,主要用于管理员账号
|
|
Bronze = 1,
|
|
Silver = 2,
|
|
Gold = 3,
|
|
Platinum = 4,
|
|
Diamond = 5,
|
|
}
|
|
impl Default for UserLevel {
|
|
fn default() -> Self {
|
|
UserLevel::Silver
|
|
}
|
|
}
|
|
|
|
/*
|
|
key:用户id
|
|
value:UserState的json字符串
|
|
|
|
|
|
*/
|
|
|
|
impl UserLevel {
|
|
pub fn to_str(&self) -> &'static str {
|
|
match self {
|
|
UserLevel::Super => "Super",
|
|
UserLevel::Bronze => "Bronze",
|
|
UserLevel::Silver => "Silver",
|
|
UserLevel::Gold => "Gold",
|
|
UserLevel::Platinum => "Platinum",
|
|
UserLevel::Diamond => "Diamond",
|
|
}
|
|
}
|
|
|
|
pub fn from_str(s: &str) -> Option<Self> {
|
|
match s {
|
|
"Super" => Some(UserLevel::Super),
|
|
"Bronze" => Some(UserLevel::Bronze),
|
|
"Silver" => Some(UserLevel::Silver),
|
|
"Gold" => Some(UserLevel::Gold),
|
|
"Platinum" => Some(UserLevel::Platinum),
|
|
"Diamond" => Some(UserLevel::Diamond),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl UserState {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
is_checkin_today: false,
|
|
checkin_unix_mills: 0 as u64,
|
|
checkin_times_in_month: 0,
|
|
checkin_days: Vec::new(),
|
|
level: UserLevel::Silver,
|
|
}
|
|
}
|
|
|
|
pub fn checkin(&mut self) {
|
|
self.is_checkin_today = true;
|
|
let unix_time = chrono::Utc::now().timestamp_millis() as u64;
|
|
|
|
self.checkin_unix_mills = unix_time;
|
|
self.checkin_times_in_month += 1;
|
|
let day = chrono::Utc::now().day();
|
|
if !self.checkin_days.contains(&day) {
|
|
self.checkin_days.push(day);
|
|
}
|
|
if self.level == UserLevel::Super {
|
|
return;
|
|
}
|
|
match self.checkin_times_in_month {
|
|
0..=7 => self.level = UserLevel::Bronze,
|
|
8..=14 => self.level = UserLevel::Silver,
|
|
15..=29 => self.level = UserLevel::Gold,
|
|
30..=44 => self.level = UserLevel::Platinum,
|
|
_ => self.level = UserLevel::Diamond,
|
|
}
|
|
}
|
|
|
|
pub fn reset_daily(&mut self) {
|
|
self.is_checkin_today = false;
|
|
}
|
|
}
|