Skip to content

Commit 0bd7372

Browse files
committed
Fix autosuccess/autofailure SL calculation
1 parent 186c058 commit 0bd7372

File tree

2 files changed

+71
-6
lines changed
  • common/src
    • commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/core/domain/rolls
    • commonTest/kotlin/cz/frantisekmasa/wfrp_master/common/core/domain/rolls

2 files changed

+71
-6
lines changed

common/src/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/core/domain/rolls/TestResult.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cz.frantisekmasa.wfrp_master.common.core.domain.NamedEnum
66
import dev.icerock.moko.parcelize.Parcelable
77
import dev.icerock.moko.parcelize.Parcelize
88
import dev.icerock.moko.resources.StringResource
9+
import kotlin.math.abs
910

1011
@Parcelize
1112
@Immutable
@@ -24,18 +25,20 @@ data class TestResult(
2425
get() = isSuccess && rollsDouble()
2526

2627
private val isSuccess: Boolean
27-
get() = successLevel > 0 || rollValue <= testedValue
28+
get() = rollValue < AUTO_FAILURE_THRESHOLD && (rollValue <= testedValue || rollValue <= AUTO_SUCCESS_THRESHOLD)
2829

2930
val successLevel: Int
30-
get() =
31-
when (rollValue) {
32-
in 1..5 -> 1
33-
in 96..100 -> -1
31+
get() {
32+
val sl = testedValue / 10 - rollValue / 10
33+
return when {
34+
rollValue <= AUTO_SUCCESS_THRESHOLD -> maxOf(sl, 1)
35+
rollValue >= AUTO_FAILURE_THRESHOLD -> minOf(sl, -1)
3436
else -> testedValue / 10 - rollValue / 10
3537
}
38+
}
3639

3740
val successLevelText: String
38-
get() = (if (isSuccess) "+" else "") + successLevel
41+
get() = (if (isSuccess) "+" else "-") + abs(successLevel)
3942

4043
val dramaticResult: DramaticResult
4144
get() {
@@ -66,4 +69,9 @@ data class TestResult(
6669
IMPRESSIVE_FAILURE(-5..-4, Str.tests_results_impressive_failure),
6770
ASTOUNDING_FAILURE(Int.MIN_VALUE..-6, Str.tests_results_astounding_failure),
6871
}
72+
73+
companion object {
74+
private const val AUTO_SUCCESS_THRESHOLD = 5
75+
private const val AUTO_FAILURE_THRESHOLD = 96
76+
}
6977
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cz.frantisekmasa.wfrp_master.common.core.domain.rolls
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
6+
7+
class TestResultTest {
8+
@Test
9+
fun `rolling 100 is fumble`() {
10+
val testResult = TestResult(100, 60)
11+
assertTrue(testResult.isFumble)
12+
}
13+
14+
@Test
15+
fun `rolling under tested value is success`() {
16+
val testResult = TestResult(9, 60)
17+
assertEquals("+6", testResult.successLevelText)
18+
}
19+
20+
@Test
21+
fun `rolling exactly the value is success`() {
22+
val testResult = TestResult(60, 60)
23+
assertEquals("+0", testResult.successLevelText)
24+
}
25+
26+
@Test
27+
fun `rolling bellow auto success threshold is success`() {
28+
assertEquals("+2", TestResult(5, 25).successLevelText)
29+
assertEquals("+1", TestResult(5, 6).successLevelText)
30+
assertEquals("+1", TestResult(5, 1).successLevelText)
31+
}
32+
33+
@Test
34+
fun `rolling double under tested value is critical`() {
35+
val testResult = TestResult(11, 60)
36+
assertTrue(testResult.isCritical)
37+
}
38+
39+
@Test
40+
fun `rolling over value is failure`() {
41+
val testResult = TestResult(16, 15)
42+
assertEquals("-0", testResult.successLevelText)
43+
}
44+
45+
@Test
46+
fun `rolling above auto failure threshold is success`() {
47+
assertEquals("-2", TestResult(96, 70).successLevelText)
48+
assertEquals("-1", TestResult(96, 97).successLevelText)
49+
assertEquals("-1", TestResult(96, 95).successLevelText)
50+
}
51+
52+
@Test
53+
fun `rolling double over roll value is fumble`() {
54+
val testResult = TestResult(77, 70)
55+
assertTrue(testResult.isFumble)
56+
}
57+
}

0 commit comments

Comments
 (0)