1use chrono::{DateTime, Utc};
2use std::collections::HashMap;
3
4use enum_iterator::Sequence;
5use strum_macros::{Display, EnumString};
6
7use crate::prelude::*;
8
9#[derive(
10 Default,
11 Debug,
12 Clone,
13 Copy,
14 PartialEq,
15 Eq,
16 Hash,
17 EnumString,
18 Sequence,
19 Display,
20 Deserialize,
21 Serialize,
22 JsonSchema,
23 Tsify,
24)]
25#[tsify(namespace)]
26pub enum AdPlacement {
27 #[default]
28 ChestUpgrade,
29 #[strum(serialize = "TalentUpgrade")]
30 TalentUpgrade,
31 #[strum(serialize = "AfkInstant")]
32 AfkInstant,
33 #[strum(serialize = "SkillGacha")]
34 SkillGacha,
35 #[strum(serialize = "PetGacha")]
36 PetGacha,
37 #[strum(serialize = "AfkBoost")]
38 AfkBoost,
39 #[strum(serialize = "DungeonRaid")]
40 DungeonRaid,
41 #[strum(serialize = "ArenaRefresh")]
42 ArenaRefresh,
43 #[strum(serialize = "DailyBooster")]
44 DailyBooster,
45 #[strum(serialize = "Bird")]
46 Bird,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Tsify)]
50pub struct AdUsageData {
51 pub daily_count: i64,
52 pub last_reset_at: DateTime<Utc>,
53}
54
55impl AdUsageData {
56 pub fn new() -> Self {
57 Self {
58 daily_count: 0,
59 last_reset_at: DateTime::<Utc>::from_timestamp(0, 0).unwrap(),
60 }
61 }
62
63 pub fn should_reset(&self, now: DateTime<Utc>) -> bool {
64 self.last_reset_at.date_naive() < now.date_naive()
65 }
66
67 pub fn reset_if_needed(&mut self, now: DateTime<Utc>) {
68 if self.should_reset(now) {
69 self.daily_count = 0;
70 self.last_reset_at = now;
71 }
72 }
73}
74
75impl Default for AdUsageData {
76 fn default() -> Self {
77 Self::new()
78 }
79}
80
81pub type AdUsageMap = HashMap<AdPlacement, AdUsageData>;