From 8170fb5e432cc81986479a6a3a400e009426d76a Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 13 Feb 2022 09:30:42 +0100 Subject: Properly shuffle card presentation from card insertion Instead of messing with deck_read, subtracting a random amount of seconds to it, pick a random card instead of the first ready. This permits to remove the dependency to the rand crate. --- Cargo.lock | 47 ----------------------------------------------- Cargo.toml | 1 - src/db/db.rs | 17 +++++------------ src/gui/gui.rs | 2 +- 4 files changed, 6 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55d77f0..d7bf371 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,7 +109,6 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", - "rand", "rusqlite", "rusqlite_migration", "serde", @@ -246,12 +245,6 @@ version = "0.3.22" 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-macro-error" version = "1.0.4" @@ -294,46 +287,6 @@ dependencies = [ "proc-macro2", ] -[[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" diff --git a/Cargo.toml b/Cargo.toml index 95f0f5c..ce79a66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] 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/src/db/db.rs b/src/db/db.rs index 590f3ea..30ea1da 100644 --- a/src/db/db.rs +++ b/src/db/db.rs @@ -4,7 +4,6 @@ use crate::{ util::serialization, }; use anyhow::Result; -use rand::{rngs::ThreadRng, Rng}; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; @@ -29,18 +28,17 @@ pub fn synchronize(conn: &Connection, entries: Vec) -> Result<()> { let now = time::now()?; 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 w in entry.part_1.iter() { - insert(&conn, &mut rng, now, &w, &concat_2, &state)?; + insert(&conn, now, &w, &concat_2, &state)?; } for w in entry.part_2.iter() { - insert(&conn, &mut rng, now, &w, &concat_1, &state)?; + insert(&conn, now, &w, &concat_1, &state)?; } } @@ -51,23 +49,18 @@ pub fn synchronize(conn: &Connection, entries: Vec) -> Result<()> { fn insert( conn: &Connection, - rng: &mut ThreadRng, now: 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, responses) DO UPDATE SET deck_read = ?, deleted = null ", - params![question, responses, state, now, now, now - ready_sub, now], + params![question, responses, state, now, now, now, now], )?; Ok(()) @@ -82,14 +75,14 @@ fn delete_read_before(conn: &Connection, t: u64) -> Result<()> { Ok(()) } -pub fn next_ready(conn: &Connection) -> Option { +pub fn pick_random_ready(conn: &Connection) -> Option { let mut stmt = conn .prepare( " SELECT question, responses, state, ready FROM cards WHERE deleted IS NULL - ORDER BY ready + ORDER BY RANDOM() LIMIT 1 ", ) diff --git a/src/gui/gui.rs b/src/gui/gui.rs index 94e15d8..2f41a0b 100644 --- a/src/gui/gui.rs +++ b/src/gui/gui.rs @@ -20,7 +20,7 @@ pub fn start(conn: &Connection, deck_name: &String) -> Result<()> { let now = time::now()?; let title = title(deck_name, answers, db::count_available(&conn).unwrap_or(0)); - match db::next_ready(&conn) { + match db::pick_random_ready(&conn) { Some(card) if card.ready <= now => { let difficulty = question::ask(&mut terminal, &events, &title, &card)?; answers += 1; -- cgit v1.2.3