From 65d237254c945f8f02b6c921dc0ae3df1d01ee62 Mon Sep 17 00:00:00 2001 From: KristianTashkov Date: Sun, 12 Sep 2021 16:56:08 +0300 Subject: [PATCH] simplify scoring code --- .../org/isoron/uhabits/core/models/ScoreList.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/ScoreList.kt b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/ScoreList.kt index f96d7857e..a062f7ede 100644 --- a/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/ScoreList.kt +++ b/uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/models/ScoreList.kt @@ -100,19 +100,18 @@ class ScoreList { rollingSum -= max(0, values[offset + denominator]) } - var percentageCompleted = 0.0 val normalizedRollingSum = rollingSum / 1000 - if (numericalHabitType == NumericalHabitType.AT_LEAST) { - percentageCompleted = if (targetValue > 0) + val percentageCompleted = if (numericalHabitType == NumericalHabitType.AT_LEAST) { + if (targetValue > 0) min(1.0, normalizedRollingSum / targetValue) else 1.0 - } else if (numericalHabitType == NumericalHabitType.AT_MOST) { - percentageCompleted = if (targetValue > 0 && normalizedRollingSum > targetValue) - max( - 0.0, 1 - ((normalizedRollingSum - targetValue) / targetValue) - ) - else if (normalizedRollingSum <= targetValue) 1.0 else 0.0 + } else { + if (targetValue > 0) { + (1 - ((normalizedRollingSum - targetValue) / targetValue)).coerceIn(0.0, 1.0) + } else { + if (normalizedRollingSum > 0) 0.0 else 1.0 + } } previousValue = compute(freq, previousValue, percentageCompleted)