-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fmasa/talent-effects
Talent Effects
- Loading branch information
Showing
12 changed files
with
306 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/character/effects/EffectFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cz.frantisekmasa.wfrp_master.common.character.effects | ||
|
||
class EffectFactory { | ||
fun getEffects(item: EffectSource): List<CharacterEffect> { | ||
return when (item) { | ||
is EffectSource.Trait -> { | ||
val name = item.trait.evaluatedName.trim() | ||
|
||
listOfNotNull( | ||
SizeChange.fromTraitNameOrNull(name), | ||
CharacteristicChange.fromTraitNameOrNull(name), | ||
SwarmWoundsModification.fromTraitNameOrNull(name), | ||
) | ||
} | ||
is EffectSource.Talent -> { | ||
val name = item.talent.name.trim() | ||
|
||
listOfNotNull( | ||
HardyWoundsModification.fromTalentOrNull(name, item.talent.taken.toUInt()), | ||
CharacteristicChange.fromTalentNameOrNull(name), | ||
) | ||
} | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
.../commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/character/effects/EffectManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cz.frantisekmasa.wfrp_master.common.character.effects | ||
|
||
import cz.frantisekmasa.wfrp_master.common.core.domain.character.Character | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.character.CharacterRepository | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.identifiers.CharacterId | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.talents.TalentRepository | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.traits.TraitRepository | ||
import cz.frantisekmasa.wfrp_master.common.core.shared.IO | ||
import cz.frantisekmasa.wfrp_master.common.firebase.firestore.Firestore | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.first | ||
|
||
class EffectManager( | ||
private val characters: CharacterRepository, | ||
private val traits: TraitRepository, | ||
private val talents: TalentRepository, | ||
private val effectFactory: EffectFactory, | ||
private val firestore: Firestore, | ||
) { | ||
|
||
suspend fun saveEffectSource( | ||
characterId: CharacterId, | ||
source: EffectSource, | ||
): Unit = coroutineScope { | ||
val characterDeferred = async(Dispatchers.IO) { characters.get(characterId) } | ||
|
||
val effectSources = effectSources(characterId) | ||
val previousSourceVersion = effectSources.firstOrNull { it.id == source.id } | ||
val otherEffects = effectSources | ||
.filter { it.id != source.id } | ||
.flatMap { effectFactory.getEffects(it) } | ||
|
||
val character = characterDeferred.await() | ||
var updatedCharacter = if (previousSourceVersion != null) | ||
character.revert(effectFactory.getEffects(previousSourceVersion), otherEffects) | ||
else character | ||
|
||
val newEffects = effectFactory.getEffects(source) | ||
updatedCharacter = updatedCharacter.apply(newEffects, otherEffects) | ||
|
||
firestore.runTransaction { transaction -> | ||
if (updatedCharacter != character) { | ||
characters.save(transaction, characterId.partyId, updatedCharacter) | ||
} | ||
|
||
when (source) { | ||
is EffectSource.Trait -> { | ||
traits.save(transaction, characterId, source.trait) | ||
} | ||
is EffectSource.Talent -> { | ||
talents.save(transaction, characterId, source.talent) | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun removeEffectSource( | ||
characterId: CharacterId, | ||
source: EffectSource, | ||
): Unit = coroutineScope { | ||
val characterDeferred = async(Dispatchers.IO) { characters.get(characterId) } | ||
|
||
val effectSources = effectSources(characterId) | ||
val otherEffects = effectSources | ||
.filter { it.id != source.id } | ||
.flatMap { effectFactory.getEffects(it) } | ||
|
||
val character = characterDeferred.await() | ||
val updatedCharacter = character.revert(effectFactory.getEffects(source), otherEffects) | ||
|
||
firestore.runTransaction { transaction -> | ||
if (updatedCharacter != character) { | ||
characters.save(transaction, characterId.partyId, updatedCharacter) | ||
} | ||
|
||
when (source) { | ||
is EffectSource.Trait -> { | ||
traits.remove(transaction, characterId, source.trait.id) | ||
} | ||
is EffectSource.Talent -> { | ||
talents.remove(transaction, characterId, source.talent.id) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun effectSources(characterId: CharacterId): List<EffectSource> = coroutineScope { | ||
val traits = async(Dispatchers.IO) { traits.findAllForCharacter(characterId).first() } | ||
val talents = async(Dispatchers.IO) { talents.findAllForCharacter(characterId).first() } | ||
|
||
traits.await().map { EffectSource.Trait(it) } + | ||
talents.await().map { EffectSource.Talent(it) } | ||
} | ||
|
||
private fun Character.apply( | ||
effects: List<CharacterEffect>, | ||
otherEffects: List<CharacterEffect>, | ||
): Character { | ||
return effects.fold(this) { character, effect -> effect.apply(character, otherEffects) } | ||
} | ||
|
||
private fun Character.revert( | ||
effects: List<CharacterEffect>, | ||
otherEffects: List<CharacterEffect>, | ||
): Character { | ||
return effects.fold(this) { character, effect -> effect.revert(character, otherEffects) } | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...c/commonMain/kotlin/cz/frantisekmasa/wfrp_master/common/character/effects/EffectSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cz.frantisekmasa.wfrp_master.common.character.effects | ||
|
||
import com.benasher44.uuid.Uuid | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.talents.Talent as CharacterTalent | ||
import cz.frantisekmasa.wfrp_master.common.core.domain.traits.Trait as CharacterTrait | ||
|
||
sealed interface EffectSource { | ||
val id: Uuid | ||
|
||
data class Trait(val trait: CharacterTrait) : EffectSource { | ||
override val id: Uuid get() = trait.id | ||
} | ||
|
||
data class Talent(val talent: CharacterTalent) : EffectSource { | ||
override val id: Uuid get() = talent.id | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...n/kotlin/cz/frantisekmasa/wfrp_master/common/character/effects/HardyWoundsModification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cz.frantisekmasa.wfrp_master.common.character.effects | ||
|
||
import cz.frantisekmasa.wfrp_master.common.core.domain.character.Character | ||
|
||
class HardyWoundsModification(private val timesTaken: UInt): CharacterEffect { | ||
override fun apply(character: Character, otherEffects: List<CharacterEffect>): Character { | ||
val modifiers = character.woundsModifiers | ||
|
||
return character.modifyWounds( | ||
modifiers.copy( | ||
extraToughnessBonusMultiplier = modifiers.extraToughnessBonusMultiplier + timesTaken, | ||
) | ||
) | ||
} | ||
|
||
override fun revert(character: Character, otherEffects: List<CharacterEffect>): Character { | ||
val modifiers = character.woundsModifiers | ||
|
||
return character.modifyWounds( | ||
modifiers.copy( | ||
extraToughnessBonusMultiplier = (modifiers.extraToughnessBonusMultiplier - timesTaken) | ||
.coerceAtLeast(0.toUInt()), | ||
) | ||
) | ||
} | ||
|
||
companion object { | ||
fun fromTalentOrNull(name: String, timesTaken: UInt): HardyWoundsModification? { | ||
if (name.equals("hardy", ignoreCase = true)) { | ||
return HardyWoundsModification(timesTaken) | ||
} | ||
|
||
return null | ||
} | ||
} | ||
|
||
} |
14 changes: 0 additions & 14 deletions
14
...onMain/kotlin/cz/frantisekmasa/wfrp_master/common/character/effects/TraitEffectFactory.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.