games-wordmaker: isolate competitive bonus words and hints from casual state
- Commit
3ea72d8e746c3a428c685766799637b8cb610edd- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:44 UTC (28 days ago)
- Parents
05b6fca
Bonus words found on competitive boards were written into the casual DataStore set (BONUS_WORDS_KEY) and bumped the lifetime TOTAL_BONUS_WORDS_KEY, so leaving a competitive round surfaced those words in the casual bonus dialog and inflated the bonus_hunter achievement. The bonus button is composed in competitive mode and the board reveals any cell in revealedHints, so the shared casual revealedHints set also leaked stale hint cells onto competitive boards. Keep a per-round in-memory competitive bonus set that resets on every loadNextCompetitiveLevel, surface it (and an empty hint set) from the bonusWords/revealedHints combines while in COMPETITIVE mode, and make the competitive branch of addBonusWord return the persisted total untouched so unlimited generated boards cannot farm the achievement. Daily still folds into the lifetime total by design; casual is unchanged. Verified with :games:wordmaker:testDevUnitTest, including a new regression test that plants a casual hint, plays a competitive round, and asserts neither the hints nor the competitive bonus words leak across modes and the lifetime total does not move.
Diff
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModel.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModel.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModel.ktindex b4f97f76cd..acb80c0d13 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModel.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModel.kt@@ -24,6 +24,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine+import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext@@ -96,6 +97,15 @@ class WordMakerViewModel(application: Application) : AndroidViewModel(applicatio private val _competitiveCrossword = MutableStateFlow<CrosswordData?>(null) private val _competitiveFoundWords = MutableStateFlow<Set<String>>(emptySet()) + /**+ * Bonus words found in competitive play. Competitive boards are generated on the+ * fly and never persisted, so their bonus words stay in memory: folding them into+ * the saved casual set would show them as already-found when the player returns+ * to their casual level, and the showing flow would likewise surface the casual+ * set mid-competition.+ */+ private val _competitiveBonusWords = MutableStateFlow<Set<String>>(emptySet())+ /** Epoch millis at which the current competitive level's timer expires (0 when inactive). */ private val _competitiveDeadline = MutableStateFlow(0L) val competitiveDeadline: StateFlow<Long> = _competitiveDeadline.asStateFlow()@@ -164,13 +174,23 @@ class WordMakerViewModel(application: Application) : AndroidViewModel(applicatio }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptySet()) val bonusWords: StateFlow<Set<String>> =- combine(gameMode, casualBonusWords, dailyBonusWords) { mode, casual, daily ->- if (mode == GameMode.DAILY) daily else casual+ combine(gameMode, casualBonusWords, _competitiveBonusWords, dailyBonusWords) { mode, casual, competitive, daily ->+ when (mode) {+ GameMode.COMPETITIVE -> competitive+ GameMode.DAILY -> daily+ GameMode.CASUAL -> casual+ } }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptySet()) val revealedHints: StateFlow<Set<Pair<Int, Int>>> = combine(gameMode, casualRevealedHints, dailyRevealedHints) { mode, casual, daily ->- if (mode == GameMode.DAILY) daily else casual+ when (mode) {+ // Hints cannot be earned in competitive play (the button is hidden+ // there), so show none rather than stale cells from the casual board.+ GameMode.COMPETITIVE -> emptySet()+ GameMode.DAILY -> daily+ GameMode.CASUAL -> casual+ } }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptySet()) private val _error = MutableStateFlow<String?>(null)@@ -296,6 +316,7 @@ class WordMakerViewModel(application: Application) : AndroidViewModel(applicatio // Show the loading spinner while the next board is generated. _competitiveCrossword.value = null _competitiveFoundWords.value = emptySet()+ _competitiveBonusWords.value = emptySet() val data = withContext(Dispatchers.Default) { resolveGenerator(ctx).generate() }@@ -363,8 +384,16 @@ class WordMakerViewModel(application: Application) : AndroidViewModel(applicatio } override suspend fun addBonusWord(word: String): Int =- if (gameMode.value == GameMode.DAILY) levelDataStore.addDailyBonusWord(word)- else levelDataStore.addBonusWord(word)+ when (gameMode.value) {+ GameMode.DAILY -> levelDataStore.addDailyBonusWord(word)+ // Competitive boards are unlimited generated layouts, so their bonus words+ // stay in memory and never inflate the lifetime bonus_hunter total.+ GameMode.COMPETITIVE -> {+ _competitiveBonusWords.value = _competitiveBonusWords.value + word+ levelDataStore.totalBonusWords.first()+ }+ GameMode.CASUAL -> levelDataStore.addBonusWord(word)+ } fun setTapToSpell(enabled: Boolean) { viewModelScope.launch { levelDataStore.setTapToSpell(enabled) }
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.ktindex b13211fe1a..8282d4bdf4 100644--- a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/platform/WordMakerViewModelTest.kt@@ -154,6 +154,33 @@ class WordMakerViewModelTest { vm.setGameMode(GameMode.CASUAL) pollUntil { vm.gameMode.value == GameMode.CASUAL } }+ @Test fun competitiveBonusWordsAndHintsStayOutOfCasual() {+ val vm = vm()+ pollUntil { vm.crosswordData.value != null }+ // Plant a casual hint so the competitive board has stale state to (not) show.+ val data = vm.crosswordData.value!!+ vm.revealHint(data, vm.foundWords.value, vm.revealedHints.value)+ pollUntil { vm.revealedHints.value.isNotEmpty() }+ vm.setGameMode(GameMode.COMPETITIVE)+ pollUntil { vm.gameMode.value == GameMode.COMPETITIVE }+ vm.loadNextCompetitiveLevel()+ pollUntil { vm.competitiveActive.value && vm.crosswordData.value != null }+ // Competitive play surfaces neither the casual bonus set nor its hint cells.+ assertTrue(vm.bonusWords.value.isEmpty())+ assertTrue(vm.revealedHints.value.isEmpty())+ val totalBefore = kotlinx.coroutines.runBlocking { vm.levelDataStore.totalBonusWords.first() }+ kotlinx.coroutines.runBlocking { vm.addBonusWord("ZQXCOMPWORD") }+ pollUntil { vm.bonusWords.value.contains("ZQXCOMPWORD") }+ // The lifetime total is unchanged: unlimited generated boards must not farm it.+ assertEquals(totalBefore, kotlinx.coroutines.runBlocking { vm.levelDataStore.totalBonusWords.first() })+ // A fresh competitive level resets the in-memory set ...+ vm.loadNextCompetitiveLevel()+ pollUntil { vm.competitiveActive.value && !vm.bonusWords.value.contains("ZQXCOMPWORD") }+ // ... and returning to casual shows none of the competitive words either.+ vm.setGameMode(GameMode.CASUAL)+ pollUntil { vm.gameMode.value == GameMode.CASUAL }+ assertFalse(vm.bonusWords.value.contains("ZQXCOMPWORD"))+ } @Test fun revealHintRevealsACellAndStartsCooldown() { val vm = vm() pollUntil { vm.crosswordData.value != null }