2026-08-24 01:37:00 +03:00
|
|
|
use rayon::iter::ParallelIterator;
|
|
|
|
|
use rayon::iter::IndexedParallelIterator;
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
|
use log::info;
|
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
|
use rayon::prelude::IntoParallelRefIterator;
|
|
|
|
|
use rhai::{Engine, Scope};
|
|
|
|
|
use crate::app::AppState;
|
|
|
|
|
use crate::core::srs::{OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule, WordOpenMode, WorstWordsSRSModule, FADE_PER_DAY, MAX_SCORE};
|
|
|
|
|
use crate::data_provider::card_stats::{load_stats_of_deck, update_stat_score};
|
2026-08-24 11:35:37 +03:00
|
|
|
use crate::data_provider::history::{push_note};
|
|
|
|
|
use crate::data::entities::*;
|
|
|
|
|
use crate::data::Id;
|
2026-08-24 01:37:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DeckData {
|
|
|
|
|
pub(crate) words: Vec<WordData>,
|
|
|
|
|
pub(crate) set: Vec<CardStatistics>,
|
|
|
|
|
current_word_index: Option<usize>,
|
|
|
|
|
state: Arc<Mutex<AppState>>,
|
|
|
|
|
order_module: OrderModule,
|
|
|
|
|
settings: DeckSettings,
|
|
|
|
|
}
|
|
|
|
|
impl DeckData {
|
|
|
|
|
pub fn new(settings: &DeckSettings, state: Arc<Mutex<AppState>>) -> Self {
|
|
|
|
|
let state_for = state.clone();
|
|
|
|
|
let state_locked = state.lock();
|
|
|
|
|
|
|
|
|
|
let stats = load_stats_of_deck(settings, &state_locked.connection);
|
|
|
|
|
let words = stats
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|stat| {
|
|
|
|
|
state_locked
|
|
|
|
|
.dictionary
|
|
|
|
|
.binary_search_by_key(&stat.word_id, |w| w.id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
})
|
|
|
|
|
.map(|index| &state_locked.dictionary[index])
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
set: stats,
|
|
|
|
|
words,
|
|
|
|
|
current_word_index: None,
|
|
|
|
|
state: state_for,
|
|
|
|
|
order_module: match settings.open_mode {
|
|
|
|
|
OrderMode::Default => OrderModule::SemiRandomSRS(SemiRandomSRSModule::new()),
|
|
|
|
|
OrderMode::TrainWorstFirst => {
|
|
|
|
|
OrderModule::WorstWordsSRS(WorstWordsSRSModule::new())
|
|
|
|
|
}
|
|
|
|
|
OrderMode::FullRandom => OrderModule::RandomSRS(RandomSRSModule::new()),
|
|
|
|
|
},
|
|
|
|
|
settings: settings.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self) -> (WordData, CardStatistics) {
|
|
|
|
|
let index = match self.order_module.clone() {
|
|
|
|
|
OrderModule::SemiRandomSRS(mut module) => {
|
|
|
|
|
if !module.initialized {
|
|
|
|
|
module.init(self)
|
|
|
|
|
}
|
|
|
|
|
let index = module.next(self);
|
|
|
|
|
self.order_module = OrderModule::SemiRandomSRS(module);
|
|
|
|
|
index
|
|
|
|
|
}
|
|
|
|
|
OrderModule::RandomSRS(mut module) => {
|
|
|
|
|
if !module.initialized {
|
|
|
|
|
module.init(self)
|
|
|
|
|
}
|
|
|
|
|
let index = module.next(self);
|
|
|
|
|
self.order_module = OrderModule::RandomSRS(module);
|
|
|
|
|
index
|
|
|
|
|
}
|
|
|
|
|
OrderModule::WorstWordsSRS(mut module) => {
|
|
|
|
|
if !module.initialized {
|
|
|
|
|
module.init(self)
|
|
|
|
|
}
|
|
|
|
|
let index = module.next(self);
|
|
|
|
|
self.order_module = OrderModule::WorstWordsSRS(module);
|
|
|
|
|
index
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.current_word_index = Some(index);
|
|
|
|
|
(self.words[index].clone(), self.set[index].clone())
|
|
|
|
|
}
|
|
|
|
|
pub fn open(&mut self, status: WordOpenMode) {
|
|
|
|
|
if self.current_word_index.is_none() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let index = self.current_word_index.unwrap();
|
|
|
|
|
|
|
|
|
|
let word : &mut CardStatistics = &mut self.set[index];
|
|
|
|
|
let old_score = word.score;
|
|
|
|
|
word.update(status);
|
|
|
|
|
let new_score = word.score;
|
|
|
|
|
|
|
|
|
|
match self.order_module {
|
|
|
|
|
OrderModule::SemiRandomSRS(ref mut module) => {
|
|
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
|
|
|
|
OrderModule::RandomSRS(ref mut module) => {
|
|
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
|
|
|
|
OrderModule::WorstWordsSRS(ref mut module) => {
|
|
|
|
|
module.open(status, index, word);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
update_stat_score(word, &self.state.lock().connection);
|
|
|
|
|
let _ = push_note(
|
|
|
|
|
self.settings.id,
|
|
|
|
|
HistoryItem {
|
|
|
|
|
timestamp: Utc::now(),
|
|
|
|
|
word_id: word.word_id,
|
|
|
|
|
mode: status,
|
|
|
|
|
before: old_score,
|
|
|
|
|
after: new_score,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.set.len()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|