Clean up old roll code + refactor some ugly stuff.

This commit is contained in:
Andres Gomez, Thomas (ITDV RL) 2024-06-01 22:39:20 +02:00
parent 8808040e14
commit 8841529b31
26 changed files with 251 additions and 244 deletions

View file

@ -9,7 +9,8 @@ import com.pixelized.rplexicon.data.model.CharacterSheet
import com.pixelized.rplexicon.data.model.DiceThrow
import com.pixelized.rplexicon.data.model.Property
import com.pixelized.rplexicon.data.model.Skill
import com.pixelized.rplexicon.data.model.Throw
import com.pixelized.rplexicon.data.model.roll.Flat
import com.pixelized.rplexicon.data.model.roll.Throw
import com.pixelized.rplexicon.data.network.NetworkThrow
import com.pixelized.rplexicon.data.repository.character.ActionRepository
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
@ -19,6 +20,7 @@ import com.pixelized.rplexicon.data.repository.character.SkillRepository
import com.pixelized.rplexicon.data.repository.character.SpellRepository
import com.pixelized.rplexicon.ui.screens.rolls.composable.RollDiceUio
import com.pixelized.rplexicon.utilitary.extentions.icon
import com.pixelized.rplexicon.utilitary.extentions.toLabel
import com.pixelized.rplexicon.utilitary.extentions.local.advantage
import com.pixelized.rplexicon.utilitary.extentions.local.base
import com.pixelized.rplexicon.utilitary.extentions.local.critical
@ -34,7 +36,6 @@ import com.pixelized.rplexicon.utilitary.extentions.local.sum
import com.pixelized.rplexicon.utilitary.extentions.local.toStatus
import com.pixelized.rplexicon.utilitary.extentions.masteryMultiplier
import com.pixelized.rplexicon.utilitary.extentions.modifier
import com.pixelized.rplexicon.utilitary.extentions.toLabel
import javax.inject.Inject
import kotlin.math.abs
import kotlin.math.max
@ -748,7 +749,7 @@ class DiceThrowUseCase @Inject constructor(
// compute the amount of main dice to throw.
val amount = if (status.isCritical) {
diceThrow?.amount?.times(2)?.let {
diceThrow?.dice?.count?.times(2)?.let {
if (ability == Property.PHYSICAL_MELEE_DAMAGE && status.isSavageAttacks)
it.plus(1) else it
}?.let {
@ -756,12 +757,12 @@ class DiceThrowUseCase @Inject constructor(
it.plus(1) else it
}
} else {
diceThrow?.amount
diceThrow?.dice?.count
}
// main roll
val result = roll(
amount = amount ?: 1,
faces = diceThrow?.faces ?: 20,
faces = diceThrow?.dice?.faces ?: 20,
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -791,7 +792,7 @@ class DiceThrowUseCase @Inject constructor(
// build the result.
return DiceThrowResult(
dice = RollDiceUio(
icon = diceThrow?.faces?.icon ?: R.drawable.ic_d20_24,
icon = diceThrow?.dice?.icon ?: R.drawable.ic_d20_24,
isCriticalSuccess = canMakeCriticalRoll && result.value == 20,
isCriticalFailure = canMakeCriticalRoll && result.value == 1,
result = rollResult,
@ -800,7 +801,7 @@ class DiceThrowUseCase @Inject constructor(
timestamp = System.currentTimeMillis(),
title = titleString.uppercase(),
highlight = action.uppercase(),
face = diceThrow?.faces ?: 20,
face = diceThrow?.dice?.faces ?: 20,
isCriticalSuccess = canMakeCriticalRoll && result.value == 20,
isCriticalFailure = canMakeCriticalRoll && result.value == 1,
roll = allValue.toLabel(),
@ -809,11 +810,11 @@ class DiceThrowUseCase @Inject constructor(
NetworkThrow.Detail(
title = action,
throws = NetworkThrow.Throw(
dice = diceThrow?.faces?.icon ?: R.drawable.ic_d20_24,
dice = diceThrow?.dice?.icon ?: R.drawable.ic_d20_24,
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
roll = "${diceThrow?.amount}d${diceThrow?.faces}",
roll = "${diceThrow?.dice?.count}d${diceThrow?.dice?.faces}",
result = result.label,
),
result = "${result.value}",
@ -840,8 +841,8 @@ class DiceThrowUseCase @Inject constructor(
val emphasis = status[Property.SPELL_EFFECT].emphasis
// main roll
val result = roll(
amount = spell?.effect?.amount ?: 1,
faces = spell?.effect?.faces ?: 4,
amount = spell?.effect?.dice?.count ?: 1,
faces = spell?.effect?.dice?.faces ?: 4,
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -849,12 +850,12 @@ class DiceThrowUseCase @Inject constructor(
critical = status[Property.SPELL_EFFECT].critical,
)
// fetch and build a list of additionnal level effect.
// fetch and build a list of additional level effect.
val levelBonus = if (spell?.level != null) {
((spell.spell.level + 1)..level).map {
val localRoll = roll(
amount = spell.level.amount,
faces = spell.level.faces,
amount = spell.level.dice.count,
faces = spell.level.dice.faces,
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -864,8 +865,8 @@ class DiceThrowUseCase @Inject constructor(
NetworkThrow.Detail(
title = application.getString(R.string.spell_level_chooser_label, "$it"),
throws = NetworkThrow.Throw(
dice = spell.level.faces.icon,
roll = "${spell.level.amount}d${spell.level.faces}",
dice = spell.level.dice.icon,
roll = spell.level.dice.toLabel(),
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -890,22 +891,22 @@ class DiceThrowUseCase @Inject constructor(
// build the result.
return DiceThrowResult(
dice = RollDiceUio(
icon = (spell?.effect?.faces ?: 4).icon,
icon = spell?.effect?.dice?.icon ?: R.drawable.ic_d4_24,
result = rollResult,
),
throws = NetworkThrow(
timestamp = System.currentTimeMillis(),
title = titleString.uppercase(),
highlight = spellName.uppercase(),
face = spell?.effect?.faces ?: 4,
face = spell?.effect?.dice?.faces ?: 4,
roll = allValue.toLabel(),
result = rollResult,
details = listOf(
NetworkThrow.Detail(
title = spellName,
throws = NetworkThrow.Throw(
dice = (spell?.effect?.faces ?: 4).icon,
roll = "${spell?.effect?.amount ?: 1}d${spell?.effect?.faces ?: 4}",
dice = spell?.effect?.dice?.icon ?: R.drawable.ic_d4_24,
roll = spell?.effect?.dice?.toLabel() ?: "1d4",
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -934,8 +935,8 @@ class DiceThrowUseCase @Inject constructor(
val emphasis = status[Property.SKILL].emphasis
// main roll
val result = roll(
amount = skill?.effect?.amount ?: 1,
faces = skill?.effect?.faces ?: 4,
amount = skill?.effect?.dice?.count ?: 1,
faces = skill?.effect?.dice?.faces ?: 4,
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -961,22 +962,22 @@ class DiceThrowUseCase @Inject constructor(
// build the result.
return DiceThrowResult(
dice = RollDiceUio(
icon = (skill?.effect?.faces ?: 4).icon,
icon = skill?.effect?.dice?.icon ?: R.drawable.ic_d4_24,
result = rollResult,
),
throws = NetworkThrow(
timestamp = System.currentTimeMillis(),
title = titleString.uppercase(),
highlight = spellName?.uppercase() ?: "",
face = skill?.effect?.faces ?: 4,
face = skill?.effect?.dice?.faces ?: 4,
roll = allValue.toLabel(),
result = rollResult,
details = listOf(
NetworkThrow.Detail(
title = spellName ?: "",
throws = NetworkThrow.Throw(
dice = (skill?.effect?.faces ?: 4).icon,
roll = "${skill?.effect?.amount ?: 1}d${skill?.effect?.faces ?: 4}",
dice = skill?.effect?.dice?.icon ?: R.drawable.ic_d4_24,
roll = skill?.effect?.dice?.toLabel() ?: "1d4",
advantage = advantage,
disadvantage = disadvantage,
emphasis = emphasis,
@ -1054,15 +1055,17 @@ class DiceThrowUseCase @Inject constructor(
}
}
private fun Int.flatBonus(name: String?): NetworkThrow.Detail? {
allValue.add(this)
return if (this != 0) {
NetworkThrow.Detail(
title = context.getString(R.string.dice_roll_bonus_detail, name),
result = "$this",
)
} else {
null
private fun Flat.flatBonus(name: String?): NetworkThrow.Detail? {
return when {
value != 0 -> {
allValue.add(value)
NetworkThrow.Detail(
title = context.getString(R.string.dice_roll_bonus_detail, name),
result = "$value",
)
}
else -> null
}
}
@ -1082,9 +1085,9 @@ class DiceThrowUseCase @Inject constructor(
critical = status.critical,
)
NetworkThrow.Detail(
title = dice.title ?: "",
title = status.name,
throws = NetworkThrow.Throw(
dice = dice.faces.icon,
dice = dice.icon,
advantage = dice.advantage,
disadvantage = dice.disadvantage,
emphasis = dice.emphasis,
@ -1101,11 +1104,11 @@ class DiceThrowUseCase @Inject constructor(
* Fetch any flat number related bonus and build a ThrowsCardUio.Detail for each.
*/
fun List<Alteration.Status>?.flatAlterationBonus(): List<NetworkThrow.Detail> {
return this?.flatMap { status ->
status.bonus.map { bonus ->
return this?.mapNotNull { status ->
status.bonus?.let { bonus ->
allValue.add(bonus.value)
NetworkThrow.Detail(
title = bonus.title,
title = status.name,
result = "${bonus.value}",
)
}