Fix panic when clearing today limits on the day collection was made (#3877)

* fix panic on clearing today limits on the day collection was made

* avoid possible overflow

* clear future today limits
This commit is contained in:
llama 2025-03-25 01:24:11 +08:00 committed by GitHub
parent a766f511dd
commit 886c5795d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -411,10 +411,16 @@ fn update_deck_limits(deck: &mut NormalDeck, limits: &Limits, today: u32) {
fn update_day_limit(day_limit: &mut Option<DayLimit>, new_limit: Option<u32>, today: u32) {
if let Some(limit) = new_limit {
day_limit.replace(DayLimit { limit, today });
} else if let Some(limit) = day_limit {
// instead of setting to None, only make sure today is in the past,
// thus preserving last used value
limit.today = limit.today.min(today - 1);
} else {
// if the collection was created today, the
// "preserve last value" hack below won't work
// clear "future" limits as well (from imports)
day_limit.take_if(|limit| limit.today == 0 || limit.today > today);
if let Some(limit) = day_limit {
// instead of setting to None, only make sure today is in the past,
// thus preserving last used value
limit.today = limit.today.min(today.saturating_sub(1));
}
}
}