Fix deck data

This commit is contained in:
micialware committed 2026-09-04 13:00:41 +03:00
1 parent 9a7578727b
commit 08ab55f283
7 files changed
+145 -96

No files matched your search

+95 -22
View File
@@ -1,3 +1,6 @@
use std::sync::Arc;
use std::time::Instant;
use chrono::Utc;
use crate::app::AppState;
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
use crate::core::srs::{
@@ -5,14 +8,16 @@ use crate::core::srs::{
WordOpenMode, WorstWordsSRSModule,
};
use crate::data::entities::*;
use crate::data_provider::card_states::update_stat_score;
use crate::data_provider::card_states::{update_stat_score, CardStateRepository, CardStateRepositoryInner};
use crate::notification::{MaybeError, NotificationData};
use chrono::Utc;
use log::info;
use rhai::{Engine, Scope};
use crate::data::repositories::Repository;
#[derive(Clone)]
pub struct DeckData {
pub(crate) words: Vec<WordData>,
pub(crate) set: Vec<CardState>,
pub(crate) words: Vec<EntityHandle<WordData>>,
pub(crate) set: Vec<EntityHandle<CardState>>,
current_word_index: Option<usize>,
state: AppState,
order_module: OrderModule,
@@ -51,57 +56,68 @@ impl DeckData {
// })
}
pub fn next(&mut self) -> (WordData, CardState) {
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);
let index = match self.order_module {
SemiRandomSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
module.init(links.as_slice());
}
module.next(self.set.as_slice())
module.next(links.as_slice())
}
RandomSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
module.init(links.as_slice())
}
module.next(self.set.as_slice())
module.next(links.as_slice())
}
WorstWordsSRS(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
module.init(links.as_slice())
}
module.next(self.set.as_slice())
module.next(links.as_slice())
}
Fsrs(ref mut module) => {
if !module.initialized {
module.init(self.set.as_slice())
module.init(links.as_slice())
}
module.next(self.set.as_slice())
module.next(links.as_slice())
}
};
self.current_word_index = Some(index);
(self.words[index].clone(), self.set[index].clone())
}
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<_>>()
}
pub fn open(&mut self, status: WordOpenMode) -> MaybeError {
if let Some(index) = self.current_word_index {
let word: &mut CardState = &mut self.set[index];
let old_score = word.score;
word.update(status);
let new_score = word.score;
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;
let mut fsrs = false;
match self.order_module {
SemiRandomSRS(ref mut module) => {
module.open(status, index, word);
module.open(status, index, card_state);
}
RandomSRS(ref mut module) => {
module.open(status, index, word);
module.open(status, index, card_state);
}
WorstWordsSRS(ref mut module) => {
module.open(status, index, word);
module.open(status, index, card_state);
}
Fsrs(ref mut module) => {
module.open(status, index, word);
module.open(status, index, card_state);
fsrs = true;
}
}
@@ -114,7 +130,21 @@ impl DeckData {
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
}
}
update_stat_score(word, &self.state.connection.read())?;
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,
})?;
todo!("Implement history pushing")
// let _ = push_note(
// self.settings.id,
@@ -133,4 +163,47 @@ impl DeckData {
pub fn len(&self) -> usize {
self.set.len()
}
pub fn list(deck: &DeckSettings, state: &AppState) -> Result<Vec<EntityHandle<WordData>>, NotificationData> {
let time = Instant::now();
let mut list = vec![];
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);
Ok(handles)
} else {
Ok(list)
}
}
}