Fix deck data
This commit is contained in:
1 parent
9a7578727b
commit
08ab55f283
7 files changed
+145
-96
No files matched your search
+95
-22
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-12
@@ -31,9 +31,9 @@ pub enum OrderModule {
|
||||
Fsrs(FsrsModule),
|
||||
}
|
||||
pub trait SRSModule {
|
||||
fn next(&mut self, set: &[CardState]) -> usize;
|
||||
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]);
|
||||
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: &[CardState]) -> usize {
|
||||
fn next(&mut self, set: &[&CardState]) -> usize {
|
||||
match self.basket.pop() {
|
||||
Some(index) => index,
|
||||
None => {
|
||||
@@ -62,7 +62,7 @@ impl SRSModule for RandomSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
||||
|
||||
fn init(&mut self, set: &[CardState]) {
|
||||
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: &[CardState]) -> 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 {
|
||||
@@ -106,7 +106,7 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
.update_weights(&[(index, &new_weight)])
|
||||
.expect("update weights error");
|
||||
}
|
||||
fn init(&mut self, set: &[CardState]) {
|
||||
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: &[CardState]) -> 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: &[CardState]) -> usize {
|
||||
fn next(&mut self, set: &[&CardState]) -> usize {
|
||||
if self.rounds_remaining == 0 {
|
||||
self.fill_pool(set);
|
||||
self.rounds_remaining = self.rounds_count;
|
||||
@@ -148,7 +148,7 @@ impl SRSModule for WorstWordsSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &mut CardState) {}
|
||||
|
||||
fn init(&mut self, _: &[CardState]) {
|
||||
fn init(&mut self, _: &[&CardState]) {
|
||||
self.initialized = true;
|
||||
}
|
||||
}
|
||||
@@ -163,7 +163,7 @@ impl WorstWordsSRSModule {
|
||||
queue: vec![],
|
||||
}
|
||||
}
|
||||
fn fill_pool(&mut self, set: &[CardState]) {
|
||||
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, _: &[CardState]) -> usize {
|
||||
fn next(&mut self, _: &[&CardState]) -> usize {
|
||||
if self.current_group.is_empty() {
|
||||
self.fill_partial_pool();
|
||||
}
|
||||
@@ -233,7 +233,7 @@ impl SRSModule for FsrsModule {
|
||||
}
|
||||
}
|
||||
|
||||
fn init(&mut self, set: &[CardState]) {
|
||||
fn init(&mut self, set: &[&CardState]) {
|
||||
self.rng = rng();
|
||||
let today = Local::now().date_naive();
|
||||
self.today_set = set
|
||||
|
||||
Reference in new issue
Block a user