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. --- src/db/db.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'src/db/db.rs') 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 ", ) -- cgit v1.2.3