konnektoren_core/challenges/
performance.rs

1use super::ChallengeResult;
2
3pub trait Performance {
4    /// Returns the performance in percentage.
5    fn performance(&self, result: &ChallengeResult) -> u32;
6
7    /// Returns the number of stars based on the performance.
8    /// 3 stars for 80% or more, 2 stars for 60% or more, 1 star for 40% or more, 0 stars otherwise.
9    /// The performance is calculated by the `performance` method.
10    fn stars(&self, result: &ChallengeResult) -> u32 {
11        let performance = self.performance(result);
12        if performance >= 80 {
13            3
14        } else if performance >= 60 {
15            2
16        } else if performance >= 40 {
17            1
18        } else {
19            0
20        }
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27    use crate::challenges::ChallengeResult;
28    use crate::challenges::ChallengeType;
29    use crate::challenges::multiple_choice::MultipleChoiceOption;
30
31    #[test]
32    fn test_performance() {
33        let challenge = ChallengeType::default();
34        let result = ChallengeResult::MultipleChoice(vec![
35            MultipleChoiceOption {
36                id: 1,
37                name: "Option 1".to_string(),
38            },
39            MultipleChoiceOption {
40                id: 2,
41                name: "Option 2".to_string(),
42            },
43        ]);
44        let performance = challenge.performance(&result);
45        assert_eq!(performance, 0);
46    }
47
48    #[test]
49    fn test_stars() {
50        let challenge = ChallengeType::default();
51        let result = ChallengeResult::MultipleChoice(vec![
52            MultipleChoiceOption {
53                id: 1,
54                name: "Option 1".to_string(),
55            },
56            MultipleChoiceOption {
57                id: 2,
58                name: "Option 2".to_string(),
59            },
60            MultipleChoiceOption {
61                id: 3,
62                name: "Option 3".to_string(),
63            },
64            MultipleChoiceOption {
65                id: 4,
66                name: "Option 4".to_string(),
67            },
68            MultipleChoiceOption {
69                id: 5,
70                name: "Option 5".to_string(),
71            },
72        ]);
73        let stars = challenge.stars(&result);
74        assert_eq!(stars, 0);
75    }
76}