aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris2022-01-26 14:06:45 +0100
committerJoris2022-01-26 14:06:45 +0100
commitb743fc24ec3ba1aa3ff9bfe5c802019e90034312 (patch)
tree26e3b88da79ac2b5987fbd223b6d72d6ff0f9e0f
parent9f94611a42d41cf94cdccb00b5d2eec0d5d02970 (diff)
downloadflashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.tar.gz
flashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.tar.bz2
flashcards-b743fc24ec3ba1aa3ff9bfe5c802019e90034312.zip
Shuffle initial presentiation order from insert order
-rw-r--r--Cargo.lock47
-rw-r--r--Cargo.toml3
-rw-r--r--README.md5
-rw-r--r--src/db/db.rs20
4 files changed, 64 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bd2c0b6..afd9423 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -74,6 +74,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
+ "rand",
"rusqlite",
"rusqlite_migration",
"serde",
@@ -186,6 +187,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12295df4f294471248581bc09bef3c38a5e46f1e36d6a37353621a0c6c357e1f"
[[package]]
+name = "ppv-lite86"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
+
+[[package]]
name = "proc-macro2"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -204,6 +211,46 @@ dependencies = [
]
[[package]]
+name = "rand"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
name = "redox_syscall"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index c9108f1..2c971db 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,8 +5,9 @@ authors = ["Joris GUYONVARCH"]
edition = "2018"
[dependencies]
-chrono = "0.4"
anyhow = "1.0"
+chrono = "0.4"
+rand = "0.8"
rusqlite = { version = "0.26", features = [ "chrono" ] }
rusqlite_migration = "0.5"
serde = { version = "1.0", features = ["derive"] }
diff --git a/README.md b/README.md
index 69a4f26..a6b0d05 100644
--- a/README.md
+++ b/README.md
@@ -17,10 +17,11 @@ Cards are created from a plain text `./deck` file:
# TODO
-- indications in parenthesis ?
+- When changing the back, update the card
- Fix crashes on zoom / changing size
## Nice to have
-- Add phonetics indication
+- Indications in parenthesis
+- Phonetics indication
- Select deck
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(())