From fa26eb3442a76ea352c0297d891727f2145babf9 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Tue, 14 Apr 2026 16:24:45 +0300 Subject: [PATCH] Fixing bugs --- src/data_provider/words.rs | 3 ++- src/lang.rs | 18 +++++++++--------- src/main.rs | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/data_provider/words.rs b/src/data_provider/words.rs index df3dc4f..52a0049 100644 --- a/src/data_provider/words.rs +++ b/src/data_provider/words.rs @@ -9,6 +9,7 @@ pub fn create_db() { if !db_file.exists() { std::fs::File::create(&db_file).unwrap(); let connection = Connection::open(&db_file).unwrap(); + connection.execute("PRAGMA foreign_keys = ON;", []).unwrap(); create_words_table(&connection); create_card_stat_table(&connection); create_card_set_table(&connection); @@ -120,7 +121,7 @@ fn create_card_stat_table(conn: &Connection) { score INTEGER NOT NULL DEFAULT 1, last_opened INTEGER NOT NULL, FOREIGN KEY (word_id) REFERENCES words (id) ON DELETE CASCADE, - FOREIGN KEY (set_id) REFERENCES sets (id) ON DELETE CASCADE + FOREIGN KEY (set_id) REFERENCES card_set (id) ON DELETE CASCADE )", (), ) diff --git a/src/lang.rs b/src/lang.rs index e810ca1..301a96f 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::{Arc, Mutex}; -const MAX_SCORE: u8 = 25_u8; +const MAX_SCORE: i32 = 25; const FADE_PER_DAY: f32 = 0.95; #[derive(Clone, Debug)] pub struct KanaSet { @@ -252,7 +252,7 @@ pub struct CardStatistics { pub word_id: u32, pub set_id: u32, pub last_open: DateTime, - pub score: u8, + pub score: i32, } impl CardStatistics { @@ -260,16 +260,16 @@ impl CardStatistics { self.last_open = Utc::now(); match status { WordOpenMode::Easy => { - self.score = self.calculated_score().round() as u8 + 5; + self.score = (self.calculated_score() + 5.0).round() as i32; } WordOpenMode::Ok => { - self.score += self.calculated_score().round() as u8 + 3; + self.score = (self.calculated_score() + 3.0).round() as i32 } WordOpenMode::Hard => { - self.score = self.calculated_score().round() as u8 - 1; + self.score = (self.calculated_score() - 1.0).round() as i32; } WordOpenMode::None => { - self.score = (self.calculated_score() * 0.5) as u8; + self.score = (self.calculated_score() * 0.5) as i32; } } @@ -284,7 +284,7 @@ impl CardStatistics { let time = Utc::now() - self.last_open; let days = time.num_days(); let multiplier = FADE_PER_DAY.powi(days as i32); - (1.0 / self.score as f32) * multiplier + self.score as f32 * multiplier } } @@ -330,7 +330,7 @@ impl CardSet { } } - let indexes = WeightedIndex::new(current_set.iter().map(|s| 1.0 / s.score as f32)).unwrap(); + let indexes = WeightedIndex::new(current_set.iter().map(|s| 1.0 / s.calculated_score())).unwrap(); Self { set: current_set, @@ -355,7 +355,7 @@ impl CardSet { let word = &mut self.set[self.current_word_index.unwrap()]; word.update(status); - let new_weight = word.calculated_score(); + let new_weight = 1.0 / word.calculated_score(); self.last_weights.update_weights(&[(self.current_word_index.unwrap(), &new_weight)]).unwrap(); { update_stat_score(word, &self.state.lock().unwrap().connection) diff --git a/src/main.rs b/src/main.rs index 80d3d5c..2c77703 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,7 @@ impl Default for ScreenState { let path = app_data_dir(); let db_file = path.join("data.db"); let connection = Connection::open(db_file).unwrap(); + connection.execute("PRAGMA foreign_keys = ON;", []).unwrap(); let list: Vec = load_words(&connection); let sets: Vec = load_sets(&connection);