Modern Apps git

← Modern-Apps.git

games: taps on the previous board during a mode switch no longer hit the new mode

Commit
da05708bc75858eb8831a690ab320c514417b347
Author
Vayun Mathur <[email protected]>
When
2026-12-01 13:44 UTC (4 days ago)
Parents
5a2b176
load() flips mode/generating in uiState but retains the old
game while the new board generates, and the board stays
tappable. tapArrow/restartLevel derived their persistence
flag from state.mode with no check that the board on screen
belongs to that mode: a blocked tap in the window cost the
incoming daily board a heart before the player ever saw it,
clearing taps wrote the old board's removed-ids into the
new mode's progress, and restart wiped the incoming mode's
saved progress instead of the shown board's.

tapArrow, restartLevel and nextLevel now ignore actions
while game.mode != state.mode; same-mode reloads are
unaffected. Regression test drives the exact stale window
(mid-switch restart + blocked tap) and asserts both boards'
state. Rules, generation (600 levels + 3,000 daily seeds)
and records were audited and cleared. Verified:
:games:arrows:testDevUnitTest 143 tests and lint pass.

Diff

games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.ktindex 93302ce7fa..ff2d1cc041 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt@@ -288,6 +288,11 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),     override fun tapArrow(pieceId: Int) {         val state = _uiState.value         val game = state.game ?: return+        // While the next board generates, [load] has already flipped the mode but keeps the+        // previous board on screen. That board is display-only: judging its taps now would+        // persist them under the incoming mode, so an arrow tapped on the old board could+        // spend a heart (or clear arrows) on a board the player has never seen.+        if (game.mode != state.mode) return         if (game.isOver || state.moves.any { it.pieceId == pieceId }) return         val piece = game.remaining.firstOrNull { it.id == pieceId } ?: return @@ -361,6 +366,7 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),         val state = _uiState.value         if (state.mode == GameMode.DAILY) return         val game = state.game ?: return+        if (game.mode != state.mode) return         if (!game.isWon) return         lastBlockedId = -1         _uiState.value = state.copy(moves = emptyList(), showHeartHint = false)@@ -369,6 +375,10 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),      override fun restartLevel() {         val state = _uiState.value+        // Same stale-board window as [tapArrow]: restarting the board still on screen while+        // the incoming mode generates would wipe the incoming mode's saved progress instead.+        val shown = state.game+        if (shown != null && shown.mode != state.mode) return         val daily = state.mode == GameMode.DAILY         lastBlockedId = -1         // Reset on screen now rather than waiting for the store to echo it, for the same reason
games/arrows/src/test/java/com/vayunmathur/games/arrows/platform/ArrowsViewModelTest.kt
diff --git a/games/arrows/src/test/java/com/vayunmathur/games/arrows/platform/ArrowsViewModelTest.kt b/games/arrows/src/test/java/com/vayunmathur/games/arrows/platform/ArrowsViewModelTest.ktindex 2aab9735b7..ec74562e48 100644--- a/games/arrows/src/test/java/com/vayunmathur/games/arrows/platform/ArrowsViewModelTest.kt+++ b/games/arrows/src/test/java/com/vayunmathur/games/arrows/platform/ArrowsViewModelTest.kt@@ -288,6 +288,80 @@ class ArrowsViewModelTest {         blocking { second.dataStore.resetBoard(daily = false) }     } +    @Test+    fun actionsOnTheStaleBoardWhileTheNewModeLoadsAreIgnored() {+        val vm = loadedViewModel()+        try {+            if (vm.uiState.value.mode != GameMode.CASUAL) {+                vm.setGameMode(GameMode.CASUAL)+                await("casual loads") {+                    vm.uiState.value.game?.mode == GameMode.CASUAL && !vm.uiState.value.generating+                }+            }+            // Some casual progress first, so a stray restart or tap has something to+            // lose on this board and something to transfer to the incoming one.+            vm.restartLevel()+            await("casual board fresh") {+                vm.uiState.value.game!!.removed.isEmpty() && vm.uiState.value.game!!.hearts == 3+            }+            val cleared = ArrowsRules.clearableNow(vm.uiState.value.game!!).first()+            vm.tapArrow(cleared.id)+            val progressed = vm.uiState.value.game!!+            assertEquals(setOf(cleared.id), progressed.removed)+            val blocked = assertNotNull(+                progressed.remaining.firstOrNull { ArrowsRules.isBlocked(progressed, it) },+            )++            vm.setGameMode(GameMode.DAILY)+            // load() flips the mode as soon as the new session starts but keeps the+            // previous board on screen while the daily board generates. Spin until+            // that stale window is visible, then act on the stale board.+            var staleShown = false+            val deadline = System.nanoTime() + 5_000_000_000L+            while (System.nanoTime() < deadline) {+                val s = vm.uiState.value+                if (s.mode == GameMode.DAILY && s.generating && s.game?.mode == GameMode.CASUAL) {+                    staleShown = true+                    break+                }+                if (s.mode == GameMode.DAILY && s.game?.mode == GameMode.DAILY && !s.generating) break+                Thread.sleep(1)+            }+            assertTrue(staleShown, "the stale casual board was never shown during the daily load")+            vm.restartLevel()+            vm.tapArrow(blocked.id)++            // Neither action touched the stale board...+            assertEquals(setOf(cleared.id), vm.uiState.value.game!!.removed)+            assertEquals(3, vm.uiState.value.game!!.hearts)+            // ...and neither leaked into the daily board that replaces it: the restart+            // must not wipe its saved progress and the blocked tap must not spend its heart.+            await("daily loads") {+                vm.uiState.value.game?.mode == GameMode.DAILY && !vm.uiState.value.generating+            }+            val daily = vm.uiState.value.game!!+            assertEquals(3, daily.hearts, "a tap on the stale casual board must not spend a daily heart")+            assertTrue(daily.removed.isEmpty(), "a tap on the stale casual board must not clear daily arrows")++            // Back on the casual board, the progress made before the switch is still there:+            // the restart issued against the stale board did not reset this board either.+            vm.setGameMode(GameMode.CASUAL)+            await("casual reloads") {+                vm.uiState.value.game?.mode == GameMode.CASUAL && !vm.uiState.value.generating+            }+            assertEquals(setOf(cleared.id), vm.uiState.value.game!!.removed)+            assertEquals(3, vm.uiState.value.game!!.hearts)+        } finally {+            vm.setGameMode(GameMode.CASUAL)+            await("back to casual") {+                vm.uiState.value.game?.mode == GameMode.CASUAL && !vm.uiState.value.generating+            }+            vm.restartLevel()+            await("restart persisted") { blocking { vm.dataStore.hearts.first() } == 3 }+            blocking { vm.dataStore.resetBoard(daily = true) }+        }+    }+     @Test     fun dailyWinBankedBeforeItsStreakWasRecordedStillRecordsTheDailyOnRelaunch() {         // The aftermath of a process death mid-clear: the lifetime banking edit committed