Fix CharacterSheet AC alteration

This commit is contained in:
Thomas Andres Gomez 2023-09-26 16:01:17 +02:00
parent b36cb9b601
commit a97a1b1ce0
5 changed files with 47 additions and 17 deletions

View file

@ -60,14 +60,6 @@ class LauncherViewModel @Inject constructor(
_error.tryEmit("Quest fail to update")
}
}
val alteration = async {
try {
alterationRepository.fetchAlterationSheet()
alterationRepository.fetchStatusSheet()
} catch (exception: Exception) {
_error.tryEmit("Alteration fail to update")
}
}
val characterSheet = async {
try {
characterSheetRepository.fetchCharacterSheet()
@ -75,7 +67,21 @@ class LauncherViewModel @Inject constructor(
_error.tryEmit("CharacterSheet fail to update")
}
}
awaitAll(lexicon, location, quest, alteration, characterSheet)
val alteration = async {
try {
alterationRepository.fetchAlterationSheet()
} catch (exception: Exception) {
_error.tryEmit("Alteration lexicon fail to update")
}
}
val status = async {
try {
alterationRepository.fetchStatusSheet()
} catch (exception: Exception) {
_error.tryEmit("Alteration status fail to update")
}
}
awaitAll(lexicon, location, quest, alteration, status, characterSheet)
val action = async {
try {

View file

@ -18,7 +18,7 @@ data class CharacterSheet(
val spell8: Counter?,
val spell9: Counter?,
val criticalModifier: Int, // Critical Dice Multiplier
val armorClass: String, // Classe d'armure
val armorClass: Int, // Classe d'armure
val speed: Int, // Vitesse
val strength: Int, // Force
val dexterity: Int, // Dextérité

View file

@ -81,7 +81,7 @@ class CharacterSheetParser @Inject constructor(
value = item.parseString(SPELL_LEVEL_9)
),
criticalModifier = item.parseInt(CRITICAL_MODIFIER) ?: 2,
armorClass = item.parseString(ARMOR_CLASS) ?: "10",
armorClass = item.parseInt(ARMOR_CLASS) ?: 10,
speed = item.parseInt(SPEED) ?: 10,
strength = item.parseInt(STRENGTH) ?: 10,
dexterity = item.parseInt(DEXTERITY) ?: 10,

View file

@ -13,11 +13,18 @@ import com.pixelized.rplexicon.repository.data.SpellRepository
import com.pixelized.rplexicon.ui.navigation.screens.characterSheetArgument
import com.pixelized.rplexicon.ui.screens.character.composable.character.CharacterSheetHeaderUio
import com.pixelized.rplexicon.ui.screens.character.factory.CharacterSheetHeaderUioFactory
import com.pixelized.rplexicon.utilitary.extentions.local.status
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.zip
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@ -42,11 +49,17 @@ class CharacterSheetViewModel @Inject constructor(
init {
viewModelScope.launch {
launch {
characterRepository.data.collect { sheets ->
_header.value = withContext(Dispatchers.Default) {
headerFactory.convert(model = sheets.getValue(character))
characterRepository.data
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
.collect { sheets ->
_header.value = withContext(Dispatchers.Default) {
val alterations = alterationRepository.getAlterations(character = character)
headerFactory.convert(
model = sheets.getValue(character),
alterations = alterations.status(),
)
}
}
}
}
launch {
update(force = false)

View file

@ -1,18 +1,29 @@
package com.pixelized.rplexicon.ui.screens.character.factory
import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.model.Alteration
import com.pixelized.rplexicon.model.Attack
import com.pixelized.rplexicon.model.CharacterSheet
import com.pixelized.rplexicon.model.Property
import com.pixelized.rplexicon.ui.screens.character.composable.character.CharacterSheetHeaderUio
import com.pixelized.rplexicon.ui.screens.character.composable.character.LabelPointUio
import javax.inject.Inject
class CharacterSheetHeaderUioFactory @Inject constructor() {
fun convert(model: CharacterSheet): CharacterSheetHeaderUio {
fun convert(
model: CharacterSheet,
alterations: Map<Property, List<Alteration.Status>>,
): CharacterSheetHeaderUio {
// compute alteration for the CA.
val armorClassAlteration: Int = alterations[Property.ARMOR_CLASS]?.sumOf {
it.bonus.sumOf { bonus -> bonus.value }
} ?: 0
return CharacterSheetHeaderUio(
armorClass = LabelPointUio(
label = R.string.character_sheet_title_ca,
value = model.armorClass,
value = "${model.armorClass + armorClassAlteration}",
max = null,
),
speed = LabelPointUio(