bloop_server_framework/evaluator/
mod.rs

1use crate::achievement::AchievementContext;
2use uuid::Uuid;
3
4pub(crate) mod boxed;
5pub mod distinct_values;
6pub mod min_bloops;
7pub mod registration_number;
8pub mod spelling_bee;
9pub mod streak;
10pub mod time;
11pub mod trigger;
12
13/// Result of evaluating whether an achievement should be awarded.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum EvalResult {
16    /// Award the achievement to the evaluating player only.
17    AwardSelf,
18    /// Award the achievement to multiple players, identified by UUIDs.
19    AwardMultiple(Vec<Uuid>),
20    /// Do not award the achievement.
21    NoAward,
22}
23
24impl From<bool> for EvalResult {
25    fn from(value: bool) -> Self {
26        if value {
27            EvalResult::AwardSelf
28        } else {
29            EvalResult::NoAward
30        }
31    }
32}
33
34impl From<Option<Uuid>> for EvalResult {
35    fn from(value: Option<Uuid>) -> Self {
36        value.map_or(EvalResult::NoAward, |value| {
37            EvalResult::AwardMultiple(vec![value])
38        })
39    }
40}
41
42impl From<Vec<Uuid>> for EvalResult {
43    fn from(value: Vec<Uuid>) -> Self {
44        Self::AwardMultiple(value)
45    }
46}
47
48impl From<Option<Vec<Uuid>>> for EvalResult {
49    fn from(value: Option<Vec<Uuid>>) -> Self {
50        value.map_or(EvalResult::NoAward, |value| {
51            EvalResult::AwardMultiple(value)
52        })
53    }
54}
55
56/// Which players receive the award when the evaluation passes.
57#[derive(Debug, Default)]
58pub enum AwardMode {
59    /// Award only the current player
60    #[default]
61    Current,
62    /// Award all players whose bloops were involved in completing the collection.
63    All,
64}
65
66/// Trait for statically typed achievement evaluators.
67///
68/// This is the primary abstraction for writing custom logic to evaluate whether
69/// an achievement should be awarded.
70pub trait Evaluator<Player, State, Trigger> {
71    /// Evaluate the achievement for the given context.
72    fn evaluate(&self, ctx: &AchievementContext<Player, State, Trigger>) -> impl Into<EvalResult>;
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use uuid::Uuid;
79
80    #[test]
81    fn converts_bool_to_eval_result() {
82        assert_eq!(EvalResult::from(true), EvalResult::AwardSelf);
83        assert_eq!(EvalResult::from(false), EvalResult::NoAward);
84    }
85
86    #[test]
87    fn converts_option_uuid_to_eval_result() {
88        let uuid = Uuid::new_v4();
89        assert_eq!(
90            EvalResult::from(Some(uuid)),
91            EvalResult::AwardMultiple(vec![uuid])
92        );
93
94        assert_eq!(EvalResult::from(None::<Uuid>), EvalResult::NoAward);
95    }
96
97    #[test]
98    fn converts_vec_uuid_to_eval_result() {
99        let uuid1 = Uuid::new_v4();
100        let uuid2 = Uuid::new_v4();
101        let uuids = vec![uuid1, uuid2];
102
103        assert_eq!(
104            EvalResult::from(uuids.clone()),
105            EvalResult::AwardMultiple(uuids)
106        );
107    }
108
109    #[test]
110    fn converts_option_vec_uuid_to_eval_result() {
111        let uuid = Uuid::new_v4();
112
113        assert_eq!(
114            EvalResult::from(Some(vec![uuid])),
115            EvalResult::AwardMultiple(vec![uuid])
116        );
117
118        assert_eq!(
119            EvalResult::from(Some(vec![])),
120            EvalResult::AwardMultiple(vec![])
121        );
122
123        assert_eq!(EvalResult::from(None::<Vec<Uuid>>), EvalResult::NoAward);
124    }
125}