Simplify alteration status management.
This commit is contained in:
parent
3af7757f30
commit
35fcb23028
6 changed files with 44 additions and 41 deletions
|
|
@ -4,7 +4,6 @@ data class Alteration(
|
|||
val name: String,
|
||||
val source: String,
|
||||
val target: String,
|
||||
val active: Boolean = false,
|
||||
val status: Map<Property, Status>,
|
||||
) {
|
||||
data class Status(
|
||||
|
|
|
|||
|
|
@ -20,18 +20,18 @@ class AlterationParser @Inject constructor(
|
|||
sheet: ValueRange,
|
||||
characterSheets: List<CharacterSheet>,
|
||||
): Map<String, List<Alteration>> = parserScope {
|
||||
val properties = Property.values()
|
||||
val properties = Property.entries.toTypedArray()
|
||||
val alterations = hashMapOf<String, MutableList<Alteration>>()
|
||||
|
||||
sheet.forEachRowIndexed { index, row ->
|
||||
when (index) {
|
||||
0 -> updateStructure(
|
||||
row = row,
|
||||
columns = COLUMNS + characterSheets.map { column(it.name) })
|
||||
columns = COLUMNS + characterSheets.map { column(it.name) },
|
||||
)
|
||||
|
||||
else -> {
|
||||
// Assume that the name is the first column.
|
||||
val name = row.getOrNull(0) as? String
|
||||
val name = row.parse(column = NAME)
|
||||
val source = row.parse(column = SOURCE)
|
||||
val target = row.parse(column = TARGET)
|
||||
val alteration = if (name != null && source != null && target != null) {
|
||||
|
|
@ -39,12 +39,14 @@ class AlterationParser @Inject constructor(
|
|||
name = name,
|
||||
source = source,
|
||||
target = target,
|
||||
active = false,
|
||||
status = properties
|
||||
.mapNotNull { property ->
|
||||
val value = row.parse(column = column(property.key))
|
||||
if (value?.isNotEmpty() == true) {
|
||||
property to parseAlterationStatus(name, value)
|
||||
property to parseAlterationStatus(
|
||||
name = name,
|
||||
value = value
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
|
@ -55,7 +57,7 @@ class AlterationParser @Inject constructor(
|
|||
null
|
||||
}
|
||||
if (alteration != null) {
|
||||
if (alteration.target == OBJECT && alteration.source == OBJECT) {
|
||||
if (alteration.target == EFFECT && alteration.source == EFFECT) {
|
||||
alterations
|
||||
.getOrPut(alteration.name) { mutableListOf() }
|
||||
.add(alteration)
|
||||
|
|
@ -63,14 +65,13 @@ class AlterationParser @Inject constructor(
|
|||
characterSheets
|
||||
.filter { // check if the alteration is applicable to the character
|
||||
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
|
||||
val isActive = row.parseBool(column = column(it.name))
|
||||
.forEach { sheet -> // check the default alteration state for that character
|
||||
alterations
|
||||
.getOrPut(it.name) { mutableListOf() }
|
||||
.add(alteration.copy(active = isActive ?: false))
|
||||
.getOrPut(sheet.name) { mutableListOf() }
|
||||
.add(alteration)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -118,11 +119,12 @@ class AlterationParser @Inject constructor(
|
|||
private const val DISADVANTAGE = "dis"
|
||||
private const val FAIL = "fail"
|
||||
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 SOURCE = column("Source")
|
||||
private val COLUMNS
|
||||
get() = listOf(SOURCE, TARGET) + Property.entries.map { column(it.key) }
|
||||
get() = listOf(NAME, SOURCE, TARGET) + Property.entries.map { column(it.key) }
|
||||
}
|
||||
}
|
||||
|
|
@ -156,6 +156,10 @@ class FirebaseRepository @Inject constructor(
|
|||
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) {
|
||||
val reference = database.getReference(
|
||||
"$PATH_CHARACTERS/$character/${CharacterSheetFire.ALTERATIONS}/$status"
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
|
|||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
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.
|
||||
* @param character the character name
|
||||
|
|
@ -79,7 +66,7 @@ class AlterationRepository @Inject constructor(
|
|||
fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> {
|
||||
val status = hashMapOf<Property, MutableList<Alteration.Status>>()
|
||||
_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 {
|
||||
status.getOrPut(it.key) { mutableListOf() }.add(it.value)
|
||||
}
|
||||
|
|
@ -88,10 +75,9 @@ class AlterationRepository @Inject constructor(
|
|||
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)
|
||||
suspend fun fetchAlterationSheet(sheets: List<CharacterSheet>) {
|
||||
googleRepository.fetch { sheet ->
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import androidx.lifecycle.AndroidViewModel
|
|||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
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.CharacterSheetRepository
|
||||
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
|
||||
|
|
@ -24,6 +25,7 @@ import javax.inject.Inject
|
|||
class AlterationViewModel @Inject constructor(
|
||||
private val characterSheetRepository: CharacterSheetRepository,
|
||||
private val alterationRepository: AlterationRepository,
|
||||
private val firebaseRepository: FirebaseRepository,
|
||||
private val descriptionRepository: DescriptionRepository,
|
||||
private val factory: AlterationFactory,
|
||||
savedStateHandle: SavedStateHandle,
|
||||
|
|
@ -71,8 +73,8 @@ class AlterationViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
fun toggleAlteration(alteration: String) {
|
||||
val value = alterationRepository.getStatus(character = character, alteration = alteration)
|
||||
alterationRepository.setStatus(character = character, alteration = alteration, value.not())
|
||||
val status = firebaseRepository.getStatus(character = character, status = alteration)
|
||||
firebaseRepository.setStatus(character = character, status = alteration, status.not())
|
||||
}
|
||||
|
||||
fun showAlterationDetail(id: String) {
|
||||
|
|
|
|||
|
|
@ -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.DiceThrow
|
||||
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.AlterationRepository
|
||||
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
|
||||
|
|
@ -17,15 +18,20 @@ class AlterationFactory @Inject constructor(
|
|||
private val spellRepository: SpellRepository,
|
||||
private val skillRepository: SkillRepository,
|
||||
private val alterationRepository: AlterationRepository,
|
||||
private val firebaseRepository: FirebaseRepository,
|
||||
) {
|
||||
fun convert(character: String, alterations: List<Alteration>): List<AlterationItemUio> {
|
||||
return alterations.map {
|
||||
val description = descriptionRepository.find(it.name)
|
||||
AlterationItemUio(
|
||||
label = it.name,
|
||||
source = it.source,
|
||||
checked = alterationRepository.getStatus(character, it.name),
|
||||
subLabel = description?.original,
|
||||
subLabel = descriptionRepository.find(
|
||||
name = it.name,
|
||||
)?.original,
|
||||
checked = firebaseRepository.getStatus(
|
||||
character = character,
|
||||
status = it.name,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -134,12 +140,16 @@ class AlterationFactory @Inject constructor(
|
|||
return alterationRepository
|
||||
.getAlterations(character = diceThrow.character, *properties.toTypedArray())
|
||||
.map {
|
||||
val description = descriptionRepository.find(it.name)
|
||||
AlterationItemUio(
|
||||
label = it.name,
|
||||
source = it.source,
|
||||
checked = alterationRepository.getStatus(diceThrow.character, it.name),
|
||||
subLabel = description?.original,
|
||||
checked = firebaseRepository.getStatus(
|
||||
character = diceThrow.character,
|
||||
status = it.name,
|
||||
),
|
||||
subLabel = descriptionRepository.find(
|
||||
name = it.name,
|
||||
)?.original,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue