aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-06-13 08:11:46 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-06-13 08:11:46 +0200
commit9549858c7525d3a330d49bd1f8e353d8a9d6eb96 (patch)
tree37e558382be911bc1e6620b0c6d4634db7879b20
parent8c5e5dd0e0aae161c4943e7ff4fe0c93be1c8fb2 (diff)
downloadmihi-9549858c7525d3a330d49bd1f8e353d8a9d6eb96.tar.gz
mihi-9549858c7525d3a330d49bd1f8e353d8a9d6eb96.zip
Add steps into words
This is a way to make more granular the accounting of successful runs, so we don't give a word for done until we have run it successfully at least MAX_STEPS times in a row. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
-rw-r--r--crates/cli/src/run.rs12
-rw-r--r--lib/mihi/src/lib.rs13
2 files changed, 19 insertions, 6 deletions
diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs
index fe193b9..0cce6d6 100644
--- a/crates/cli/src/run.rs
+++ b/crates/cli/src/run.rs
@@ -1,6 +1,10 @@
use inquire::Text;
use mihi::{select_random_words, update_success, Category, Word};
+// Maximum number of times a word has to be run in order to increase the number
+// of successful runs.
+const MAX_STEPS: usize = 5;
+
fn help(msg: Option<&str>) {
if msg.is_some() {
println!("{}.\n", msg.unwrap());
@@ -56,11 +60,15 @@ fn run_words(words: Vec<Word>, locale: Locale) -> i32 {
let found = !answer.is_empty() && tr.split(',').any(|tr| tr.trim().contains(&answer));
if found {
- let _ = update_success(&word, word.succeeded + 1);
+ if word.steps == MAX_STEPS - 1 {
+ let _ = update_success(&word, word.succeeded + 1, 0);
+ } else {
+ let _ = update_success(&word, word.succeeded, word.steps + 1);
+ }
println!("\x1b[92m✓ {}\x1b[0m", tr);
} else {
if word.succeeded > 0 {
- let _ = update_success(&word, word.succeeded - 1);
+ let _ = update_success(&word, word.succeeded - 1, 0);
}
println!("\x1b[91m❌{}\x1b[0m", tr);
errors += 1;
diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs
index e464549..0a3c2f7 100644
--- a/lib/mihi/src/lib.rs
+++ b/lib/mihi/src/lib.rs
@@ -222,6 +222,7 @@ pub struct Word {
pub suffix: Option<String>,
pub translation: Value,
pub succeeded: usize,
+ pub steps: usize,
}
pub fn select_enunciated(filter: Option<String>) -> Result<Vec<String>, String> {
@@ -257,7 +258,8 @@ pub fn select_random_words(category: Category, number: usize) -> Result<Vec<Word
let mut stmt = conn
.prepare(
"SELECT id, enunciated, particle, language_id, declension_id, conjugation_id, \
- kind, category, regular, locative, gender, suffix, translation, succeeded \
+ kind, category, regular, locative, gender, suffix, translation, \
+ succeeded, steps \
FROM words \
WHERE category = ?1 AND translation != '{}' \
ORDER BY succeeded ASC, updated_at DESC
@@ -283,17 +285,20 @@ pub fn select_random_words(category: Category, number: usize) -> Result<Vec<Word
suffix: row.get(11).unwrap(),
translation: serde_json::from_str(&row.get::<usize, String>(12).unwrap()).unwrap(),
succeeded: row.get(13).unwrap(),
+ steps: row.get(14).unwrap(),
});
}
Ok(res)
}
-pub fn update_success(word: &Word, success: usize) -> Result<(), String> {
+pub fn update_success(word: &Word, success: usize, steps: usize) -> Result<(), String> {
let conn = get_connection()?;
match conn.execute(
- "UPDATE words SET succeeded = ?1, updated_at = datetime('now') WHERE id = ?2",
- params![success, word.id],
+ "UPDATE words \
+ SET succeeded = ?1, steps = ?2, updated_at = datetime('now') \
+ WHERE id = ?3",
+ params![success, steps, word.id],
) {
Ok(_) => Ok(()),
Err(e) => return Err(format!("could not update '{}': {}", word.enunciated, e)),