2026-09-04 13:00:41 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::time::Instant;
|
|
|
|
|
use chrono::Utc;
|
2026-08-24 01:37:00 +03:00
|
|
|
use crate::app::AppState;
|
2026-08-29 22:02:01 +03:00
|
|
|
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
|
2026-08-24 23:27:24 +03:00
|
|
|
use crate::core::srs::{
|
2026-09-04 11:18:23 +03:00
|
|
|
OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule,
|
2026-08-29 21:31:54 +03:00
|
|
|
WordOpenMode, WorstWordsSRSModule,
|
2026-08-24 23:27:24 +03:00
|
|
|
};
|
2026-08-24 11:35:37 +03:00
|
|
|
use crate::data::entities::*;
|
2026-09-04 17:26:59 +03:00
|
|
|
use crate::data_provider::card_states::{CardStateRepositoryInner};
|
2026-08-24 23:42:58 +03:00
|
|
|
use crate::notification::{MaybeError, NotificationData};
|
2026-09-04 13:00:41 +03:00
|
|
|
use log::info;
|
|
|
|
|
use rhai::{Engine, Scope};
|
|
|
|
|
use crate::data::repositories::Repository;
|
2026-08-24 01:37:00 +03:00
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct DeckData {
|
2026-09-04 13:00:41 +03:00
|
|
|
pub(crate) words: Vec<EntityHandle<WordData>>,
|
|
|
|
|
pub(crate) set: Vec<EntityHandle<CardState>>,
|
2026-08-24 01:37:00 +03:00
|
|
|
current_word_index: Option<usize>,
|
2026-08-31 11:46:35 +03:00
|
|
|
state: AppState,
|
2026-08-24 01:37:00 +03:00
|
|
|
order_module: OrderModule,
|
2026-08-29 21:31:54 +03:00
|
|
|
open_mode: OrderMode,
|
2026-08-24 01:37:00 +03:00
|
|
|
settings: DeckSettings,
|
|
|
|
|
}
|
|
|
|
|
impl DeckData {
|
2026-08-24 23:27:24 +03:00
|
|
|
pub fn new(
|
|
|
|
|
settings: &DeckSettings,
|
2026-08-31 11:46:35 +03:00
|
|
|
state: AppState,
|
2026-08-24 23:27:24 +03:00
|
|
|
) -> Result<Self, NotificationData> {
|
2026-08-31 14:50:21 +03:00
|
|
|
todo!()
|
|
|
|
|
// let state_for = state.clone();
|
|
|
|
|
//
|
|
|
|
|
// let stats = state (settings, &state.connection.read())?;
|
|
|
|
|
// let words = stats
|
|
|
|
|
// .iter()
|
|
|
|
|
// .map(|stat| {
|
|
|
|
|
// state
|
|
|
|
|
// .dictionary
|
|
|
|
|
// .binary_search_by_key(&stat.word_id, |w| w.id)
|
|
|
|
|
// .expect("No word found in dictionary")
|
|
|
|
|
// })
|
|
|
|
|
// .map(|index| &state.dictionary[index])
|
|
|
|
|
// .cloned()
|
|
|
|
|
// .collect();
|
|
|
|
|
//
|
|
|
|
|
// Ok(Self {
|
|
|
|
|
// set: stats,
|
|
|
|
|
// words,
|
|
|
|
|
// current_word_index: None,
|
|
|
|
|
// state: state_for,
|
|
|
|
|
// order_module: Fsrs(FsrsModule::new()),
|
|
|
|
|
// open_mode: settings.open_mode,
|
|
|
|
|
// settings: settings.clone(),
|
|
|
|
|
// })
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 13:00:41 +03:00
|
|
|
pub fn next(&mut self) -> (EntityHandle<WordData>, EntityHandle<CardState>) {
|
|
|
|
|
let repo = self.state.card_states.read();
|
|
|
|
|
let links = Self::set_links(self.set.as_slice(), &repo);
|
2026-08-25 01:05:15 +03:00
|
|
|
let index = match self.order_module {
|
2026-08-29 21:31:54 +03:00
|
|
|
SemiRandomSRS(ref mut module) => {
|
|
|
|
|
if !module.initialized {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.init(links.as_slice());
|
2026-08-29 21:31:54 +03:00
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
module.next(links.as_slice())
|
2026-08-29 21:31:54 +03:00
|
|
|
}
|
2026-08-29 21:32:54 +03:00
|
|
|
|
2026-08-29 21:31:54 +03:00
|
|
|
RandomSRS(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.init(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
module.next(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
WorstWordsSRS(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.init(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
module.next(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 22:02:01 +03:00
|
|
|
Fsrs(ref mut module) => {
|
2026-08-24 01:37:00 +03:00
|
|
|
if !module.initialized {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.init(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
module.next(links.as_slice())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.current_word_index = Some(index);
|
|
|
|
|
(self.words[index].clone(), self.set[index].clone())
|
|
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
|
|
|
|
|
fn set_links<'a>(set: &[EntityHandle<CardState>], repo: &'a CardStateRepositoryInner) -> Vec<&'a CardState> {
|
|
|
|
|
set.iter().map(|h| repo.get_ref_static(*h).unwrap()).collect::<Vec<_>>()
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-24 23:42:58 +03:00
|
|
|
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
|
2026-08-24 23:27:24 +03:00
|
|
|
if let Some(index) = self.current_word_index {
|
2026-09-04 13:00:41 +03:00
|
|
|
let mut repo = self.state.card_states.write();
|
|
|
|
|
let card_state_handle = &mut self.set[index];
|
|
|
|
|
let card_state = repo.get_mut(card_state_handle).unwrap();
|
|
|
|
|
let old_score = card_state.score;
|
|
|
|
|
card_state.update(status);
|
|
|
|
|
let new_score = card_state.score;
|
2026-08-24 01:37:00 +03:00
|
|
|
|
2026-09-04 13:00:41 +03:00
|
|
|
let mut fsrs = false;
|
2026-08-24 23:27:24 +03:00
|
|
|
match self.order_module {
|
2026-08-29 21:31:54 +03:00
|
|
|
SemiRandomSRS(ref mut module) => {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.open(status, index, card_state);
|
2026-08-29 21:31:54 +03:00
|
|
|
}
|
|
|
|
|
RandomSRS(ref mut module) => {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.open(status, index, card_state);
|
2026-08-24 23:27:24 +03:00
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
WorstWordsSRS(ref mut module) => {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.open(status, index, card_state);
|
2026-08-24 23:27:24 +03:00
|
|
|
}
|
2026-08-29 22:02:01 +03:00
|
|
|
Fsrs(ref mut module) => {
|
2026-09-04 13:00:41 +03:00
|
|
|
module.open(status, index, card_state);
|
|
|
|
|
fsrs = true;
|
2026-08-24 23:27:24 +03:00
|
|
|
}
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-29 21:31:54 +03:00
|
|
|
|
2026-08-29 22:02:01 +03:00
|
|
|
if let Fsrs(ref fsrs) = self.order_module
|
|
|
|
|
&& fsrs.finished()
|
|
|
|
|
{
|
|
|
|
|
self.order_module = match self.open_mode {
|
|
|
|
|
OrderMode::Default => SemiRandomSRS(SemiRandomSRSModule::new()),
|
|
|
|
|
OrderMode::TrainWorstFirst => WorstWordsSRS(WorstWordsSRSModule::new()),
|
|
|
|
|
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
2026-08-29 21:31:54 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
|
|
|
|
|
let word_id = card_state.word_id;
|
|
|
|
|
repo.save(card_state_handle)?;
|
|
|
|
|
|
|
|
|
|
let mut history = self.state.history.write();
|
|
|
|
|
history.create_now(HistoryNote {
|
|
|
|
|
id: Default::default(),
|
|
|
|
|
timestamp: Utc::now(),
|
|
|
|
|
set_id: self.settings.id,
|
|
|
|
|
word_id,
|
|
|
|
|
answer: status.clone(),
|
|
|
|
|
before: old_score,
|
|
|
|
|
after: new_score,
|
|
|
|
|
fsrs,
|
|
|
|
|
})?;
|
2026-09-04 11:18:23 +03:00
|
|
|
todo!("Implement history pushing")
|
|
|
|
|
// let _ = push_note(
|
|
|
|
|
// self.settings.id,
|
|
|
|
|
// HistoryNote {
|
|
|
|
|
// timestamp: Utc::now(),
|
|
|
|
|
// word_id: word.word_id,
|
|
|
|
|
// answer: status,
|
|
|
|
|
// before: old_score,
|
|
|
|
|
// after: new_score,
|
|
|
|
|
// },
|
|
|
|
|
// );
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
2026-08-24 23:27:24 +03:00
|
|
|
|
2026-08-24 18:16:41 +03:00
|
|
|
Ok(())
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.set.len()
|
|
|
|
|
}
|
2026-09-04 13:00:41 +03:00
|
|
|
|
2026-09-06 23:55:50 +03:00
|
|
|
pub fn list(deck: &DeckSettings, state: &AppState) -> Vec<EntityHandle<WordData>> {
|
2026-09-04 13:00:41 +03:00
|
|
|
let time = Instant::now();
|
2026-09-06 23:55:50 +03:00
|
|
|
let list = vec![];
|
2026-09-04 13:00:41 +03:00
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
|
|
let ast = engine.compile(&deck.filter);
|
|
|
|
|
if let Ok(ast) = ast {
|
|
|
|
|
let groups = state.word_groups.read().iter().cloned().collect::<Vec<_>>();
|
|
|
|
|
let dictionary = &state.dictionary.read();
|
|
|
|
|
let query = dictionary.query_par(Arc::new(move |word| {
|
|
|
|
|
let mut more = rhai::Map::new();
|
|
|
|
|
for iced in &word.additional {
|
|
|
|
|
more.insert(iced.0.clone().into(), iced.1.clone().into());
|
|
|
|
|
}
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
let group_name = groups[groups.binary_search_by_key(&word.group_id, |g| g.id).unwrap()].name.clone();
|
|
|
|
|
scope
|
|
|
|
|
.push_constant("id", word.id.0 as i64)
|
|
|
|
|
.push_constant("key", word.key.clone())
|
|
|
|
|
.push_constant("value", word.value.clone())
|
|
|
|
|
.push_constant("tags", word.tags.clone())
|
|
|
|
|
.push_constant("more", more)
|
|
|
|
|
.push_constant(
|
|
|
|
|
"group",
|
|
|
|
|
group_name,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let result = engine.eval_ast_with_scope::<bool>(&mut scope, &ast);
|
|
|
|
|
if let Ok(result) = result {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
false
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
info!("Collecting available words is {:?}", time.elapsed());
|
|
|
|
|
let handles =query.handles().collect();
|
|
|
|
|
drop(query);
|
2026-09-06 23:55:50 +03:00
|
|
|
handles
|
2026-09-04 13:00:41 +03:00
|
|
|
} else {
|
2026-09-06 23:55:50 +03:00
|
|
|
list
|
2026-09-04 13:00:41 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-24 01:37:00 +03:00
|
|
|
}
|