From e907ca220c7a7ce8d3fa7a732688d1c7701261c4 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Fri, 8 Jul 2022 20:52:06 +0200 Subject: [PATCH] Add deck-specific limits to DeckNormal --- proto/anki/decks.proto | 4 +++- rslib/src/decks/limits.rs | 42 ++++++++++++++++++++++++------------- rslib/src/decks/schema11.rs | 2 ++ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/proto/anki/decks.proto b/proto/anki/decks.proto index 3409acc97..1380afee1 100644 --- a/proto/anki/decks.proto +++ b/proto/anki/decks.proto @@ -69,8 +69,10 @@ message Deck { uint32 extend_review = 3; string description = 4; bool markdown_description = 5; + optional uint32 review_limit = 6; + optional uint32 new_limit = 7; - reserved 6 to 11; + reserved 8 to 11; } message Filtered { message SearchTerm { diff --git a/rslib/src/decks/limits.rs b/rslib/src/decks/limits.rs index 5120e0d1b..3c71a1a4c 100644 --- a/rslib/src/decks/limits.rs +++ b/rslib/src/decks/limits.rs @@ -5,7 +5,7 @@ use std::{collections::HashMap, iter::Peekable}; use id_tree::{InsertBehavior, Node, NodeId, Tree}; -use super::Deck; +use super::{Deck, NormalDeck}; use crate::{ deckconfig::{DeckConfig, DeckConfigId}, prelude::*, @@ -19,19 +19,33 @@ pub(crate) struct RemainingLimits { impl RemainingLimits { pub(crate) fn new(deck: &Deck, config: Option<&DeckConfig>, today: u32, v3: bool) -> Self { - config - .map(|config| { - let (new_today, mut rev_today) = deck.new_rev_counts(today); - if v3 { - // any reviewed new cards contribute to the review limit - rev_today += new_today; - } - RemainingLimits { - review: ((config.inner.reviews_per_day as i32) - rev_today).max(0) as u32, - new: ((config.inner.new_per_day as i32) - new_today).max(0) as u32, - } - }) - .unwrap_or_default() + if let Ok(normal) = deck.normal() { + if let Some(config) = config { + return Self::new_for_normal_deck(deck, today, v3, normal, config); + } + } + Self::default() + } + + fn new_for_normal_deck( + deck: &Deck, + today: u32, + v3: bool, + normal: &NormalDeck, + config: &DeckConfig, + ) -> RemainingLimits { + let review_limit = normal.review_limit.unwrap_or(config.inner.reviews_per_day); + let new_limit = normal.new_limit.unwrap_or(config.inner.new_per_day); + let (new_today, mut rev_today) = deck.new_rev_counts(today); + if v3 { + // any reviewed new cards contribute to the review limit + rev_today += new_today; + } + + Self { + review: (review_limit as i32 - rev_today).max(0) as u32, + new: (new_limit as i32 - new_today).max(0) as u32, + } } pub(crate) fn cap_to(&mut self, limits: RemainingLimits) { diff --git a/rslib/src/decks/schema11.rs b/rslib/src/decks/schema11.rs index 873a076b4..7b4b1bc54 100644 --- a/rslib/src/decks/schema11.rs +++ b/rslib/src/decks/schema11.rs @@ -298,6 +298,8 @@ impl From for NormalDeck { extend_review: deck.extend_rev.max(0) as u32, markdown_description: deck.common.markdown_description, description: deck.common.desc, + review_limit: None, + new_limit: None, } } }