From b743fc24ec3ba1aa3ff9bfe5c802019e90034312 Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 26 Jan 2022 14:06:45 +0100 Subject: Shuffle initial presentiation order from insert order --- src/db/db.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/db/db.rs b/src/db/db.rs index 93c9564..a3398e2 100644 --- a/src/db/db.rs +++ b/src/db/db.rs @@ -4,6 +4,7 @@ use crate::{ util::serialization, }; use anyhow::Result; +use rand::{rngs::ThreadRng, Rng}; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; use std::time::SystemTime; @@ -18,22 +19,20 @@ pub fn init() -> Result { pub fn add_missing_deck_entries(conn: &Connection, entries: Vec) -> Result<()> { let now = get_current_time()?; let ready = now; - let day: u64 = 60 * 60 * 24; let state = serde_json::to_string(&space_repetition::init())?; + let mut rng = rand::thread_rng(); for entry in entries { let concat_1 = serialization::words_to_line(&entry.part_1); let concat_2 = serialization::words_to_line(&entry.part_2); - for (i, w) in entry.part_1.iter().enumerate() { - let r = ready + (i as u64) * day; - insert(&conn, now, r, &w, &concat_2, &state)?; + for w in entry.part_1.iter() { + insert(&conn, &mut rng, now, ready, &w, &concat_2, &state)?; } - for (i, w2) in entry.part_2.iter().enumerate() { - let r = ready + ((entry.part_1.len() + i) as u64) * day; - insert(&conn, now, r, &w2, &concat_1, &state)?; + for w in entry.part_2.iter() { + insert(&conn, &mut rng, now, ready, &w, &concat_1, &state)?; } } @@ -44,19 +43,24 @@ pub fn add_missing_deck_entries(conn: &Connection, entries: Vec) -> Resul fn insert( conn: &Connection, + rng: &mut ThreadRng, now: u64, ready: u64, question: &String, responses: &String, state: &String, ) -> Result<()> { + // Subtract a random amount of time so that cards are not initially given in the same order as + // in the deck + let ready_sub: u64 = rng.gen_range(0..100); + conn.execute( " INSERT INTO cards (question, responses, state, created, deck_read, ready) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (question) DO UPDATE SET deck_read = ?, deleted = null ", - params![question, responses, state, now, now, ready, now], + params![question, responses, state, now, now, ready - ready_sub, now], )?; Ok(()) -- cgit v1.2.3