CardStates rewritten to TableAccessQuerySet

This commit is contained in:
micialware committed 2026-08-30 20:43:31 +03:00
1 parent 45830a7985
commit 068b9c7197
7 files changed
+88 -40

No files matched your search

+3 -3
View File
@@ -15,7 +15,7 @@ use std::sync::Arc;
#[derive(Clone)]
pub struct DeckData {
pub(crate) words: Vec<WordData>,
pub(crate) set: Vec<CardStatistics>,
pub(crate) set: Vec<CardState>,
current_word_index: Option<usize>,
state: Arc<Mutex<AppState>>,
order_module: OrderModule,
@@ -54,7 +54,7 @@ impl DeckData {
})
}
pub fn next(&mut self) -> (WordData, CardStatistics) {
pub fn next(&mut self) -> (WordData, CardState) {
let index = match self.order_module {
SemiRandomSRS(ref mut module) => {
if !module.initialized {
@@ -88,7 +88,7 @@ impl DeckData {
}
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
if let Some(index) = self.current_word_index {
let word: &mut CardStatistics = &mut self.set[index];
let word: &mut CardState = &mut self.set[index];
let old_score = word.score;
word.update(status);
let new_score = word.score;
+18 -18
View File
@@ -1,4 +1,4 @@
use crate::data::entities::CardStatistics;
use crate::data::entities::CardState;
use chrono::{Local, TimeDelta};
use fsrs::FSRS;
use rand::distr::weighted::WeightedIndex;
@@ -31,9 +31,9 @@ pub enum OrderModule {
Fsrs(FsrsModule),
}
pub trait SRSModule {
fn next(&mut self, set: &[CardStatistics]) -> usize;
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardStatistics);
fn init(&mut self, set: &[CardStatistics]);
fn next(&mut self, set: &[CardState]) -> usize;
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState);
fn init(&mut self, set: &[CardState]);
}
#[derive(Clone)]
pub struct RandomSRSModule {
@@ -49,7 +49,7 @@ impl RandomSRSModule {
}
}
impl SRSModule for RandomSRSModule {
fn next(&mut self, set: &[CardStatistics]) -> usize {
fn next(&mut self, set: &[CardState]) -> usize {
match self.basket.pop() {
Some(index) => index,
None => {
@@ -60,9 +60,9 @@ impl SRSModule for RandomSRSModule {
}
}
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardStatistics) {}
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
fn init(&mut self, set: &[CardStatistics]) {
fn init(&mut self, set: &[CardState]) {
self.initialized = true;
self.basket = (0..set.len()).collect::<Vec<usize>>();
self.basket.shuffle(&mut rand::rng())
@@ -86,7 +86,7 @@ impl SemiRandomSRSModule {
}
}
impl SRSModule for SemiRandomSRSModule {
fn next(&mut self, set: &[CardStatistics]) -> usize {
fn next(&mut self, set: &[CardState]) -> usize {
let index = self.last_weights.sample(&mut self.generator);
if self.history.contains(&index) && self.history.len() != 1 {
@@ -100,13 +100,13 @@ impl SRSModule for SemiRandomSRSModule {
index
}
fn open(&mut self, _: WordOpenMode, index: usize, word: &mut CardStatistics) {
fn open(&mut self, _: WordOpenMode, index: usize, word: &mut CardState) {
let new_weight = (100.0 / word.calculated_score()).powf(2.0);
self.last_weights
.update_weights(&[(index, &new_weight)])
.expect("update weights error");
}
fn init(&mut self, set: &[CardStatistics]) {
fn init(&mut self, set: &[CardState]) {
self.initialized = true;
let weights = set
.iter()
@@ -116,7 +116,7 @@ impl SRSModule for SemiRandomSRSModule {
}
}
impl SemiRandomSRSModule {
fn history_len(&self, set: &[CardStatistics]) -> usize {
fn history_len(&self, set: &[CardState]) -> usize {
((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN)
}
}
@@ -130,7 +130,7 @@ pub struct WorstWordsSRSModule {
rounds_count: u8,
}
impl SRSModule for WorstWordsSRSModule {
fn next(&mut self, set: &[CardStatistics]) -> usize {
fn next(&mut self, set: &[CardState]) -> usize {
if self.rounds_remaining == 0 {
self.fill_pool(set);
self.rounds_remaining = self.rounds_count;
@@ -146,9 +146,9 @@ impl SRSModule for WorstWordsSRSModule {
}
}
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardStatistics) {}
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
fn init(&mut self, _: &[CardStatistics]) {
fn init(&mut self, _: &[CardState]) {
self.initialized = true;
}
}
@@ -163,7 +163,7 @@ impl WorstWordsSRSModule {
queue: vec![],
}
}
fn fill_pool(&mut self, set: &[CardStatistics]) {
fn fill_pool(&mut self, set: &[CardState]) {
let mut sorted = set.iter().enumerate().collect::<Vec<_>>();
sorted.sort_by_key(|c| c.1.score);
let mut worst = sorted[0..self.pool_size]
@@ -187,7 +187,7 @@ pub struct FsrsModule {
}
impl SRSModule for FsrsModule {
fn next(&mut self, _: &[CardStatistics]) -> usize {
fn next(&mut self, _: &[CardState]) -> usize {
if self.current_group.is_empty() {
self.fill_partial_pool();
}
@@ -200,7 +200,7 @@ impl SRSModule for FsrsModule {
self.queue.pop().unwrap_or(0)
}
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardStatistics) {
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardState) {
let today = Local::now().date_naive();
let delta = today - updated_word.fsrs_opened;
let delta_days = delta.num_days();
@@ -233,7 +233,7 @@ impl SRSModule for FsrsModule {
}
}
fn init(&mut self, set: &[CardStatistics]) {
fn init(&mut self, set: &[CardState]) {
self.rng = rng();
let today = Local::now().date_naive();
self.today_set = set