Skip to main content

logicaffeine_web/
struggle.rs

1//! Struggle Detection Logic
2//!
3//! Detects when a user is struggling with an exercise based on:
4//! - Inactivity (no answer attempt after threshold time)
5//! - Wrong attempts (incorrect answers)
6
7/// Reasons why a user might be struggling
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum StruggleReason {
10    Inactivity,
11    WrongAttempt,
12}
13
14impl StruggleReason {
15    /// Get a message describing the struggle reason
16    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/// Configuration for struggle detection
25#[derive(Debug, Clone, Copy)]
26pub struct StruggleConfig {
27    /// Seconds of inactivity before considering the user stuck
28    pub inactivity_threshold_secs: u64,
29    /// Number of wrong attempts before showing help
30    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/// Tracks struggle state for an exercise
43#[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    /// Record a wrong attempt - may trigger struggle state
65    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    /// Record a correct attempt - resets inactivity but keeps struggle state for hints
74    pub fn record_correct_attempt(&mut self) {
75        // User got it right - they're no longer struggling
76        self.is_struggling = false;
77        self.inactivity_triggered = false;
78    }
79
80    /// Record user activity (typing, clicking) - resets inactivity timer
81    pub fn record_activity(&mut self) {
82        // Activity resets inactivity detection
83        self.inactivity_triggered = false;
84    }
85
86    /// Called when inactivity threshold is reached
87    pub fn trigger_inactivity(&mut self) {
88        if !self.inactivity_triggered {
89            self.inactivity_triggered = true;
90            self.is_struggling = true;
91            // Only set reason if not already struggling from wrong attempts
92            if self.reason.is_none() {
93                self.reason = Some(StruggleReason::Inactivity);
94            }
95        }
96    }
97
98    /// Reset struggle state (e.g., when moving to next exercise)
99    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    /// Check if we should show hints
107    pub fn should_show_hints(&self) -> bool {
108        self.is_struggling
109    }
110
111    /// Get the current struggle reason
112    pub fn reason(&self) -> Option<StruggleReason> {
113        self.reason
114    }
115
116    /// Get the current struggle reason for display
117    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;