Simplify alteration status management.

This commit is contained in:
Thomas Andres Gomez 2023-12-22 09:53:24 +01:00
parent 3af7757f30
commit 35fcb23028
6 changed files with 44 additions and 41 deletions

View file

@ -4,7 +4,6 @@ data class Alteration(
val name: String, val name: String,
val source: String, val source: String,
val target: String, val target: String,
val active: Boolean = false,
val status: Map<Property, Status>, val status: Map<Property, Status>,
) { ) {
data class Status( data class Status(

View file

@ -20,18 +20,18 @@ class AlterationParser @Inject constructor(
sheet: ValueRange, sheet: ValueRange,
characterSheets: List<CharacterSheet>, characterSheets: List<CharacterSheet>,
): Map<String, List<Alteration>> = parserScope { ): Map<String, List<Alteration>> = parserScope {
val properties = Property.values() val properties = Property.entries.toTypedArray()
val alterations = hashMapOf<String, MutableList<Alteration>>() val alterations = hashMapOf<String, MutableList<Alteration>>()
sheet.forEachRowIndexed { index, row -> sheet.forEachRowIndexed { index, row ->
when (index) { when (index) {
0 -> updateStructure( 0 -> updateStructure(
row = row, row = row,
columns = COLUMNS + characterSheets.map { column(it.name) }) columns = COLUMNS + characterSheets.map { column(it.name) },
)
else -> { else -> {
// Assume that the name is the first column. val name = row.parse(column = NAME)
val name = row.getOrNull(0) as? String
val source = row.parse(column = SOURCE) val source = row.parse(column = SOURCE)
val target = row.parse(column = TARGET) val target = row.parse(column = TARGET)
val alteration = if (name != null && source != null && target != null) { val alteration = if (name != null && source != null && target != null) {
@ -39,12 +39,14 @@ class AlterationParser @Inject constructor(
name = name, name = name,
source = source, source = source,
target = target, target = target,
active = false,
status = properties status = properties
.mapNotNull { property -> .mapNotNull { property ->
val value = row.parse(column = column(property.key)) val value = row.parse(column = column(property.key))
if (value?.isNotEmpty() == true) { if (value?.isNotEmpty() == true) {
property to parseAlterationStatus(name, value) property to parseAlterationStatus(
name = name,
value = value
)
} else { } else {
null null
} }
@ -55,7 +57,7 @@ class AlterationParser @Inject constructor(
null null
} }
if (alteration != null) { if (alteration != null) {
if (alteration.target == OBJECT && alteration.source == OBJECT) { if (alteration.target == EFFECT && alteration.source == EFFECT) {
alterations alterations
.getOrPut(alteration.name) { mutableListOf() } .getOrPut(alteration.name) { mutableListOf() }
.add(alteration) .add(alteration)
@ -63,14 +65,13 @@ class AlterationParser @Inject constructor(
characterSheets characterSheets
.filter { // check if the alteration is applicable to the character .filter { // check if the alteration is applicable to the character
alteration.target.let { target -> alteration.target.let { target ->
target == ALL || it.characterClass.any { it.value == target } || target == it.race || target == it.name target == ALL || target == it.name || target == it.race || it.characterClass.any { it.value == target }
} }
} }
.forEach { // check the default alteration state for that character .forEach { sheet -> // check the default alteration state for that character
val isActive = row.parseBool(column = column(it.name))
alterations alterations
.getOrPut(it.name) { mutableListOf() } .getOrPut(sheet.name) { mutableListOf() }
.add(alteration.copy(active = isActive ?: false)) .add(alteration)
} }
} }
} }
@ -118,11 +119,12 @@ class AlterationParser @Inject constructor(
private const val DISADVANTAGE = "dis" private const val DISADVANTAGE = "dis"
private const val FAIL = "fail" private const val FAIL = "fail"
private const val CRITICAL = "crit" private const val CRITICAL = "crit"
private const val OBJECT = "Objet" private const val EFFECT = "Effet"
private val NAME = column("Altération")
private val TARGET = column("Cible") private val TARGET = column("Cible")
private val SOURCE = column("Source") private val SOURCE = column("Source")
private val COLUMNS private val COLUMNS
get() = listOf(SOURCE, TARGET) + Property.entries.map { column(it.key) } get() = listOf(NAME, SOURCE, TARGET) + Property.entries.map { column(it.key) }
} }
} }

View file

@ -156,6 +156,10 @@ class FirebaseRepository @Inject constructor(
reference.setValue(value) reference.setValue(value)
} }
fun getStatus(character: String, status: String): Boolean {
return this.status.value[character + status] ?: false
}
fun setStatus(character: String, status: String, value: Boolean) { fun setStatus(character: String, status: String, value: Boolean) {
val reference = database.getReference( val reference = database.getReference(
"$PATH_CHARACTERS/$character/${CharacterSheetFire.ALTERATIONS}/$status" "$PATH_CHARACTERS/$character/${CharacterSheetFire.ALTERATIONS}/$status"

View file

@ -12,7 +12,6 @@ import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -59,18 +58,6 @@ class AlterationRepository @Inject constructor(
} }
} }
/**
* check the activation state of a given alteration for given character.
* @param character the character name
* @param alteration the alteration name
* @return true if the alteration is activated otherwise false
*/
fun getStatus(character: String, alteration: String): Boolean {
return fireRepository.status.value[character + alteration]
?: _assignedAlterations.value[character]?.firstOrNull { it.name == alteration }?.active
?: false
}
/** /**
* get a map of [Property] and [Alteration.Status] for a given player if the alteration is active. * get a map of [Property] and [Alteration.Status] for a given player if the alteration is active.
* @param character the character name * @param character the character name
@ -79,7 +66,7 @@ class AlterationRepository @Inject constructor(
fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> { fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> {
val status = hashMapOf<Property, MutableList<Alteration.Status>>() val status = hashMapOf<Property, MutableList<Alteration.Status>>()
_assignedAlterations.value[character]?.forEach { alteration -> _assignedAlterations.value[character]?.forEach { alteration ->
if (fireRepository.status.value[character + alteration.name] ?: alteration.active) { if (fireRepository.status.value[character + alteration.name] == true) {
alteration.status.forEach { alteration.status.forEach {
status.getOrPut(it.key) { mutableListOf() }.add(it.value) status.getOrPut(it.key) { mutableListOf() }.add(it.value)
} }
@ -88,10 +75,9 @@ class AlterationRepository @Inject constructor(
return status return status
} }
fun setStatus(character: String, alteration: String, value: Boolean?) { /**
fireRepository.setStatus(character = character, status = alteration, value = value ?: false) * Update the alteration sheet.
} */
@Throws(IncompatibleSheetStructure::class, Exception::class) @Throws(IncompatibleSheetStructure::class, Exception::class)
suspend fun fetchAlterationSheet(sheets: List<CharacterSheet>) { suspend fun fetchAlterationSheet(sheets: List<CharacterSheet>) {
googleRepository.fetch { sheet -> googleRepository.fetch { sheet ->

View file

@ -7,6 +7,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.pixelized.rplexicon.R import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
import com.pixelized.rplexicon.data.repository.character.AlterationRepository import com.pixelized.rplexicon.data.repository.character.AlterationRepository
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
@ -24,6 +25,7 @@ import javax.inject.Inject
class AlterationViewModel @Inject constructor( class AlterationViewModel @Inject constructor(
private val characterSheetRepository: CharacterSheetRepository, private val characterSheetRepository: CharacterSheetRepository,
private val alterationRepository: AlterationRepository, private val alterationRepository: AlterationRepository,
private val firebaseRepository: FirebaseRepository,
private val descriptionRepository: DescriptionRepository, private val descriptionRepository: DescriptionRepository,
private val factory: AlterationFactory, private val factory: AlterationFactory,
savedStateHandle: SavedStateHandle, savedStateHandle: SavedStateHandle,
@ -71,8 +73,8 @@ class AlterationViewModel @Inject constructor(
} }
fun toggleAlteration(alteration: String) { fun toggleAlteration(alteration: String) {
val value = alterationRepository.getStatus(character = character, alteration = alteration) val status = firebaseRepository.getStatus(character = character, status = alteration)
alterationRepository.setStatus(character = character, alteration = alteration, value.not()) firebaseRepository.setStatus(character = character, status = alteration, status.not())
} }
fun showAlterationDetail(id: String) { fun showAlterationDetail(id: String) {

View file

@ -3,6 +3,7 @@ package com.pixelized.rplexicon.ui.screens.rolls.factory
import com.pixelized.rplexicon.data.model.Alteration import com.pixelized.rplexicon.data.model.Alteration
import com.pixelized.rplexicon.data.model.DiceThrow import com.pixelized.rplexicon.data.model.DiceThrow
import com.pixelized.rplexicon.data.model.Property import com.pixelized.rplexicon.data.model.Property
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
import com.pixelized.rplexicon.data.repository.character.ActionRepository import com.pixelized.rplexicon.data.repository.character.ActionRepository
import com.pixelized.rplexicon.data.repository.character.AlterationRepository import com.pixelized.rplexicon.data.repository.character.AlterationRepository
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
@ -17,15 +18,20 @@ class AlterationFactory @Inject constructor(
private val spellRepository: SpellRepository, private val spellRepository: SpellRepository,
private val skillRepository: SkillRepository, private val skillRepository: SkillRepository,
private val alterationRepository: AlterationRepository, private val alterationRepository: AlterationRepository,
private val firebaseRepository: FirebaseRepository,
) { ) {
fun convert(character: String, alterations: List<Alteration>): List<AlterationItemUio> { fun convert(character: String, alterations: List<Alteration>): List<AlterationItemUio> {
return alterations.map { return alterations.map {
val description = descriptionRepository.find(it.name)
AlterationItemUio( AlterationItemUio(
label = it.name, label = it.name,
source = it.source, source = it.source,
checked = alterationRepository.getStatus(character, it.name), subLabel = descriptionRepository.find(
subLabel = description?.original, name = it.name,
)?.original,
checked = firebaseRepository.getStatus(
character = character,
status = it.name,
),
) )
} }
} }
@ -134,12 +140,16 @@ class AlterationFactory @Inject constructor(
return alterationRepository return alterationRepository
.getAlterations(character = diceThrow.character, *properties.toTypedArray()) .getAlterations(character = diceThrow.character, *properties.toTypedArray())
.map { .map {
val description = descriptionRepository.find(it.name)
AlterationItemUio( AlterationItemUio(
label = it.name, label = it.name,
source = it.source, source = it.source,
checked = alterationRepository.getStatus(diceThrow.character, it.name), checked = firebaseRepository.getStatus(
subLabel = description?.original, character = diceThrow.character,
status = it.name,
),
subLabel = descriptionRepository.find(
name = it.name,
)?.original,
) )
} }
} }