shared/domain/module/body/
card_quiz.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::domain::module::{
    body::{Body, BodyConvert, BodyExt, ThemeId, _groups::cards::*},
    ModuleKind,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// The body for [`CardQuiz`](crate::domain::module::ModuleKind::CardQuiz) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct ModuleData {
    /// The content
    pub content: Option<Content>,
}

/// The content for [`CardQuiz`](crate::domain::module::ModuleKind::CardQuiz) modules.
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Content {
    /// The base content for all cards modules
    pub base: BaseContent,
    /// Settings for playback
    pub player_settings: PlayerSettings,
}

/// Player settings
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct PlayerSettings {
    /// number of choices
    pub n_choices: u8,

    /// swap the display to be primary left vs. right
    pub swap: bool,

    /// number of rounds to play
    pub n_rounds: u32,

    /// time limit in minutes
    pub time_limit: Option<u32>,
}

impl Default for PlayerSettings {
    fn default() -> Self {
        Self {
            n_choices: 3,
            swap: false,
            n_rounds: 3,
            time_limit: None,
        }
    }
}
/// Module is a memory game, and has a memory game's body.
impl BodyExt<Mode, Step> for ModuleData {
    fn as_body(&self) -> Body {
        Body::CardQuiz(self.clone())
    }

    fn is_complete(&self) -> bool {
        self.content
            .as_ref()
            .map_or(false, |content| content.base.is_valid())
    }

    fn kind() -> ModuleKind {
        ModuleKind::CardQuiz
    }

    fn new_with_mode_and_theme(mode: Mode, theme: ThemeId) -> Self {
        ModuleData {
            content: Some(Content {
                base: BaseContent {
                    mode,
                    theme,
                    ..Default::default()
                },
                ..Default::default()
            }),
        }
    }

    fn mode(&self) -> Option<Mode> {
        self.content.as_ref().map(|c| c.base.mode.clone())
    }

    fn requires_choose_mode(&self) -> bool {
        self.content.is_none()
    }

    fn set_editor_state_step(&mut self, step: Step) {
        if let Some(content) = self.content.as_mut() {
            content.base.editor_state.step = step;
        }
    }
    fn set_editor_state_steps_completed(&mut self, steps_completed: HashSet<Step>) {
        if let Some(content) = self.content.as_mut() {
            content.base.editor_state.steps_completed = steps_completed;
        }
    }

    fn get_editor_state_step(&self) -> Option<Step> {
        self.content
            .as_ref()
            .map(|content| content.base.editor_state.step)
    }

    fn get_editor_state_steps_completed(&self) -> Option<HashSet<Step>> {
        self.content
            .as_ref()
            .map(|content| content.base.editor_state.steps_completed.clone())
    }

    fn set_theme(&mut self, theme_id: ThemeId) {
        if let Some(content) = self.content.as_mut() {
            content.base.theme = theme_id;
        }
    }

    fn get_theme(&self) -> Option<ThemeId> {
        self.content.as_ref().map(|content| content.base.theme)
    }
}

impl BodyConvert for ModuleData {
    fn convertable_list() -> Vec<ModuleKind> {
        vec![
            ModuleKind::Memory,
            ModuleKind::Matching,
            ModuleKind::Flashcards,
        ]
    }

    fn convert_to_memory(&self) -> Result<super::memory::ModuleData, &'static str> {
        Ok(super::memory::ModuleData {
            content: self.content.as_ref().map(|content| super::memory::Content {
                base: content.base.clone(),
                player_settings: super::memory::PlayerSettings::default(),
            }),
        })
    }
    fn convert_to_matching(&self) -> Result<super::matching::ModuleData, &'static str> {
        Ok(super::matching::ModuleData {
            content: self
                .content
                .as_ref()
                .map(|content| super::matching::Content {
                    base: content.base.clone(),
                    player_settings: super::matching::PlayerSettings::default(),
                }),
        })
    }

    fn convert_to_flashcards(&self) -> Result<super::flashcards::ModuleData, &'static str> {
        Ok(super::flashcards::ModuleData {
            content: self
                .content
                .as_ref()
                .map(|content| super::flashcards::Content {
                    base: content.base.clone(),
                    player_settings: super::flashcards::PlayerSettings::default(),
                }),
        })
    }
}

impl TryFrom<Body> for ModuleData {
    type Error = &'static str;

    fn try_from(body: Body) -> Result<Self, Self::Error> {
        match body {
            Body::CardQuiz(data) => Ok(data),
            _ => Err("cannot convert body to flashcards!"),
        }
    }
}