aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoris2022-01-26 14:06:45 +0100
committerJoris2022-01-26 14:06:45 +0100
commitb743fc24ec3ba1aa3ff9bfe5c802019e90034312 (patch)
tree26e3b88da79ac2b5987fbd223b6d72d6ff0f9e0f /src
parent9f94611a42d41cf94cdccb00b5d2eec0d5d02970 (diff)
downloadflashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.tar.gz
flashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.tar.bz2
flashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.zip
Shuffle initial presentiation order from insert order
Diffstat (limited to 'src')
-rw-r--r--src/db/db.rs20
1 files changed, 12 insertions, 8 deletions
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<Connection> {
pub fn add_missing_deck_entries(conn: &Connection, entries: Vec<Entry>) -> 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<Entry>) -> 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(())