Modern Apps git

← Modern-Apps.git

games-pipes: reload daily board when the day rolls over

Commit
25843a68f0cdd79564abafe8ec141cfa98185291
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:47 UTC (28 days ago)
Parents
05b6fca
PipesViewModel.loadLevel treated a same-pack/same-index call as an
idempotent no-op, keying only on the indices and a non-null levelData.
That is wrong for the daily pack, which refreshDaily() regenerates at
midnight: a DailyGame route kept open across the rollover recomposes
with a new levels list and re-fires loadLevel, but the early return
kept yesterday's LevelData on screen. The stale board shows isReady
with yesterday's puzzle, and a win is recorded under yesterday's level
id — which refreshDaily() has already pruned from the daily stats via
retainOnly — so the win silently evaporates and the best-score chip
looks up the new day's id against the old board.

The fix resolves the target LevelData first and only takes the early
return when it equals the currently loaded board. Same-day reloads
still resolve to equal instances and stay no-ops, preserving the
idempotency the navigation tests rely on. The change is app-side in
games/pipes only; no shared library or sdk code is touched.

Adds a dailyRolloverReloadsSameIndex regression test that loads a
daily level, draws partial progress, swaps in the next day's pack, and
asserts loadLevel for the same index delivers the fresh board with
empty paths and zero moves. It fails against the old code (stale
yesterday id) and passes with the fix.

Verified with :games:pipes:testDevUnitTest (all green, including the
new test).

Diff

games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.ktindex d434966f58..8242ffb9fe 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt@@ -171,12 +171,16 @@ class PipesViewModel(application: Application) : AndroidViewModel(application),     }      fun loadLevel(packIndex: Int, levelIndex: Int) {+        val levelData = levelsFor(packIndex)?.getOrNull(levelIndex) ?: return         val current = _uiState.value+        // Same-index reloads are a no-op only while they resolve to the same board. The+        // daily pack is regenerated at midnight, so a DailyGame route kept open across the+        // rollover must pick up the new day's board instead of keeping yesterday's (whose+        // IDs the stats store has already pruned, silently losing the win).         if (current.packIndex == packIndex &&             current.levelIndex == levelIndex &&-            current.levelData != null+            current.levelData == levelData         ) return-        val levelData = levelsFor(packIndex)?.getOrNull(levelIndex) ?: return         _uiState.value = PipesUiState(             packIndex = packIndex,             levelIndex = levelIndex,
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.ktindex 622cf633d4..7fb9e43718 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt@@ -7,6 +7,7 @@ import com.vayunmathur.games.pipes.data.LevelData import com.vayunmathur.games.pipes.data.LevelPack import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.resetMain@@ -269,6 +270,34 @@ class PipesViewModelTest {         Unit     } +    @Test fun dailyRolloverReloadsSameIndex() = runBlocking {+        // A DailyGame route kept open across midnight: refreshDaily() swaps in the new+        // day's pack, and the re-fired loadLevel must pick up the new board instead of+        // keeping yesterday's via the same-index early return.+        val vm = vm()+        vm.refreshDaily()+        withTimeout(10_000) { vm.dailyPack.first { it != null } }+        vm.loadLevel(PipesViewModel.DAILY_PACK_INDEX, 0)+        val yesterday = vm.uiState.value.levelData!!+        val ep = yesterday.endpoints[0]+        val next = (yesterday.adjacency[ep.cells[0]] ?: emptyList())+            .first { n -> yesterday.endpoints.all { n !in it.cells } }+        draw(vm, listOf(ep.cells[0], next))+        assertEquals(1, vm.getCurrentMoves())+        val rolledOver =+            com.vayunmathur.games.pipes.domain.DailyLevels.packFor(vm.dailyDay.value + 1)+        assertNotNull(rolledOver)+        val field = PipesViewModel::class.java.getDeclaredField("_dailyPack")+        field.isAccessible = true+        @Suppress("UNCHECKED_CAST")+        (field.get(vm) as MutableStateFlow<LevelPack?>).value = rolledOver+        vm.loadLevel(PipesViewModel.DAILY_PACK_INDEX, 0)+        assertEquals(rolledOver.levels[0], vm.uiState.value.levelData)+        assertTrue(vm.uiState.value.gameState.paths.isEmpty())+        assertEquals(0, vm.getCurrentMoves())+        Unit+    }+     @Test fun reachingPairedEndpointEndsTheStroke() {         val packIndex = LevelPack.PACKS.indexOfFirst { it.name == "Bridges" }         val vm = vm(); vm.loadLevel(packIndex, 0)