logicaffeine_web/
struggle.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum StruggleReason {
10 Inactivity,
11 WrongAttempt,
12}
13
14impl StruggleReason {
15 pub fn message(&self) -> &'static str {
17 match self {
18 StruggleReason::Inactivity => "Taking your time? Here's a hint to help you along.",
19 StruggleReason::WrongAttempt => "Not quite! Let me help you think through this.",
20 }
21 }
22}
23
24#[derive(Debug, Clone, Copy)]
26pub struct StruggleConfig {
27 pub inactivity_threshold_secs: u64,
29 pub wrong_attempt_threshold: u32,
31}
32
33impl Default for StruggleConfig {
34 fn default() -> Self {
35 Self {
36 inactivity_threshold_secs: 5,
37 wrong_attempt_threshold: 1,
38 }
39 }
40}
41
42#[derive(Debug, Clone, Default)]
44pub struct StruggleDetector {
45 pub config: StruggleConfig,
46 pub is_struggling: bool,
47 pub reason: Option<StruggleReason>,
48 pub wrong_attempts: u32,
49 pub inactivity_triggered: bool,
50}
51
52impl StruggleDetector {
53 pub fn new() -> Self {
54 Self::default()
55 }
56
57 pub fn with_config(config: StruggleConfig) -> Self {
58 Self {
59 config,
60 ..Default::default()
61 }
62 }
63
64 pub fn record_wrong_attempt(&mut self) {
66 self.wrong_attempts += 1;
67 if self.wrong_attempts >= self.config.wrong_attempt_threshold {
68 self.is_struggling = true;
69 self.reason = Some(StruggleReason::WrongAttempt);
70 }
71 }
72
73 pub fn record_correct_attempt(&mut self) {
75 self.is_struggling = false;
77 self.inactivity_triggered = false;
78 }
79
80 pub fn record_activity(&mut self) {
82 self.inactivity_triggered = false;
84 }
85
86 pub fn trigger_inactivity(&mut self) {
88 if !self.inactivity_triggered {
89 self.inactivity_triggered = true;
90 self.is_struggling = true;
91 if self.reason.is_none() {
93 self.reason = Some(StruggleReason::Inactivity);
94 }
95 }
96 }
97
98 pub fn reset(&mut self) {
100 self.is_struggling = false;
101 self.reason = None;
102 self.wrong_attempts = 0;
103 self.inactivity_triggered = false;
104 }
105
106 pub fn should_show_hints(&self) -> bool {
108 self.is_struggling
109 }
110
111 pub fn reason(&self) -> Option<StruggleReason> {
113 self.reason
114 }
115
116 pub fn struggle_message(&self) -> Option<&'static str> {
118 match self.reason {
119 Some(StruggleReason::Inactivity) => {
120 Some("Taking your time? Here's a hint to help you along.")
121 }
122 Some(StruggleReason::WrongAttempt) => {
123 Some("Not quite! Let me help you think through this.")
124 }
125 None => None,
126 }
127 }
128}
129
130#[cfg(test)]
131#[path = "../../../crates/logicaffeine_tests/unit/logicaffeine_web/struggle/tests.rs"]
132mod tests;