games/wordmaker: a competitive level now ends with exactly one outcome
- Commit
aed56046b03cfd236e47d97d0046a7bfca4113ae- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 20:07 UTC (5 days ago)
- Parents
ffcf686
If the final word landed inside the timer's last wake window, both terminal effects fired: the win effect and the deadline effect each launched a scoring coroutine, and because the level was only marked over after a suspending DataStore edit inside each, both score deltas applied (+10 win and -5 timeout on Easy) and the lobby result was whichever coroutine landed last. The first terminal report now claims the level synchronously (check-and-clear before any suspension), mirroring the guard setGameMode already had; the second report sees an inactive level and does nothing. New regression test covers both orders and failed pre-fix. Dictionary consistency, generators, win semantics, persistence and letter input were audited and cleared. Verified: :games:wordmaker:testDevUnitTest passes (one rotating pre-existing Compose flake in full-suite runs, green in isolation on both sides of this change); lint passes.
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 0fdcb5f65e..65b5b5cd4a 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@@ -373,23 +373,36 @@ class WordMakerViewModel(application: Application) : AndroidViewModel(applicatio viewModelScope.launch { levelDataStore.setDifficulty(difficulty) } } - /** Player solved the competitive level in time: award points and return to the lobby. */+ /**+ * Player solved the competitive level in time: award points and return to the lobby.+ *+ * A level ends exactly once. The win is reported from the board's+ * win effect and the timeout from its timer effect, and in the timer's+ * last wake window both can fire for the same level (the final word was+ * credited, but the deadline passed before the win recomposed). The+ * first report therefore claims the level synchronously, before the+ * score edit suspends, exactly as [setGameMode] resets this state+ * synchronously; the second report then sees an inactive level and+ * does nothing instead of also applying its delta.+ */ fun onCompetitiveWin() {+ if (!_competitiveActive.value) return+ _competitiveActive.value = false+ _competitiveDeadline.value = 0L viewModelScope.launch { levelDataStore.addToCompetitiveScore(difficulty.value.winDelta) _competitiveResult.value = CompetitiveResult(won = true, delta = difficulty.value.winDelta)- _competitiveDeadline.value = 0L- _competitiveActive.value = false } } - /** Timer ran out: deduct points and return to the lobby. */+ /** Timer ran out: deduct points and return to the lobby. First terminal report wins; see [onCompetitiveWin]. */ override fun onCompetitiveTimeout() {+ if (!_competitiveActive.value) return+ _competitiveActive.value = false+ _competitiveDeadline.value = 0L viewModelScope.launch { levelDataStore.addToCompetitiveScore(-difficulty.value.lossDelta) _competitiveResult.value = CompetitiveResult(won = false, delta = -difficulty.value.lossDelta)- _competitiveDeadline.value = 0L- _competitiveActive.value = false } }
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 c1de4cc043..2eb48bb844 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@@ -175,6 +175,40 @@ class WordMakerViewModelTest { vm.setGameMode(GameMode.CASUAL) pollUntil { vm.gameMode.value == GameMode.CASUAL } }+ @Test fun competitiveLevelEndsWithExactlyOneOutcome() {+ val vm = vm()+ vm.setGameMode(GameMode.COMPETITIVE)+ pollUntil { vm.gameMode.value == GameMode.COMPETITIVE }+ vm.setDifficulty(Difficulty.EASY)+ pollUntil { vm.difficulty.value == Difficulty.EASY }+ // Win reported first, timeout second (the final word was credited in+ // the timer's last wake window): only the win may be scored.+ vm.loadNextCompetitiveLevel()+ pollUntil { vm.competitiveActive.value && vm.crosswordData.value != null }+ val score0 = vm.competitiveScore.value+ vm.onCompetitiveWin()+ vm.onCompetitiveTimeout()+ pollUntil { vm.competitiveResult.value?.won == true }+ assertEquals(Difficulty.EASY.winDelta, vm.competitiveResult.value!!.delta)+ pollUntil { vm.competitiveScore.value == score0 + Difficulty.EASY.winDelta }+ assertEquals(0L, vm.competitiveDeadline.value)+ assertFalse(vm.competitiveActive.value)+ // And the mirror image on the next level: timeout first, win second.+ vm.loadNextCompetitiveLevel()+ pollUntil { vm.competitiveActive.value && vm.crosswordData.value != null }+ val score1 = vm.competitiveScore.value+ vm.onCompetitiveTimeout()+ vm.onCompetitiveWin()+ pollUntil { vm.competitiveResult.value?.won == false }+ assertEquals(-Difficulty.EASY.lossDelta, vm.competitiveResult.value!!.delta)+ pollUntil {+ vm.competitiveScore.value ==+ (score1 - Difficulty.EASY.lossDelta).coerceAtLeast(0)+ }+ vm.setDifficulty(Difficulty.MEDIUM)+ vm.setGameMode(GameMode.CASUAL)+ pollUntil { vm.gameMode.value == GameMode.CASUAL }+ } @Test fun competitiveBonusWordsAndHintsStayOutOfCasual() { val vm = vm() pollUntil { vm.crosswordData.value != null }