Skip to content

Commit

Permalink
Merge pull request #407 from fmasa/fix-auto-success
Browse files Browse the repository at this point in the history
Fix autosuccess/autofailure SL calculation
  • Loading branch information
fmasa authored Jan 27, 2025
2 parents 186c058 + 0bd7372 commit 604c35e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import cz.frantisekmasa.wfrp_master.common.core.domain.NamedEnum
import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.parcelize.Parcelize
import dev.icerock.moko.resources.StringResource
import kotlin.math.abs

@Parcelize
@Immutable
Expand All @@ -24,18 +25,20 @@ data class TestResult(
get() = isSuccess && rollsDouble()

private val isSuccess: Boolean
get() = successLevel > 0 || rollValue <= testedValue
get() = rollValue < AUTO_FAILURE_THRESHOLD && (rollValue <= testedValue || rollValue <= AUTO_SUCCESS_THRESHOLD)

val successLevel: Int
get() =
when (rollValue) {
in 1..5 -> 1
in 96..100 -> -1
get() {
val sl = testedValue / 10 - rollValue / 10
return when {
rollValue <= AUTO_SUCCESS_THRESHOLD -> maxOf(sl, 1)
rollValue >= AUTO_FAILURE_THRESHOLD -> minOf(sl, -1)
else -> testedValue / 10 - rollValue / 10
}
}

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

val dramaticResult: DramaticResult
get() {
Expand Down Expand Up @@ -66,4 +69,9 @@ data class TestResult(
IMPRESSIVE_FAILURE(-5..-4, Str.tests_results_impressive_failure),
ASTOUNDING_FAILURE(Int.MIN_VALUE..-6, Str.tests_results_astounding_failure),
}

companion object {
private const val AUTO_SUCCESS_THRESHOLD = 5
private const val AUTO_FAILURE_THRESHOLD = 96
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cz.frantisekmasa.wfrp_master.common.core.domain.rolls

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class TestResultTest {
@Test
fun `rolling 100 is fumble`() {
val testResult = TestResult(100, 60)
assertTrue(testResult.isFumble)
}

@Test
fun `rolling under tested value is success`() {
val testResult = TestResult(9, 60)
assertEquals("+6", testResult.successLevelText)
}

@Test
fun `rolling exactly the value is success`() {
val testResult = TestResult(60, 60)
assertEquals("+0", testResult.successLevelText)
}

@Test
fun `rolling bellow auto success threshold is success`() {
assertEquals("+2", TestResult(5, 25).successLevelText)
assertEquals("+1", TestResult(5, 6).successLevelText)
assertEquals("+1", TestResult(5, 1).successLevelText)
}

@Test
fun `rolling double under tested value is critical`() {
val testResult = TestResult(11, 60)
assertTrue(testResult.isCritical)
}

@Test
fun `rolling over value is failure`() {
val testResult = TestResult(16, 15)
assertEquals("-0", testResult.successLevelText)
}

@Test
fun `rolling above auto failure threshold is success`() {
assertEquals("-2", TestResult(96, 70).successLevelText)
assertEquals("-1", TestResult(96, 97).successLevelText)
assertEquals("-1", TestResult(96, 95).successLevelText)
}

@Test
fun `rolling double over roll value is fumble`() {
val testResult = TestResult(77, 70)
assertTrue(testResult.isFumble)
}
}

0 comments on commit 604c35e

Please sign in to comment.