Modern Apps git

← Modern-Apps.git

games/arrows: cleared-board banking survives mode interleaving

Commit
a0688c06dbac24a06a7c28c00767146ca439f8ed
Author
Vayun Mathur <[email protected]>
When
2026-11-30 22:30 UTC (5 days ago)
Parents
9c7e39b
The lifetime boards-cleared counter remembered only the last
banked key, so alternating between two simultaneously-won
boards (win casual level N, win today's daily, come back)
re-banked each on every switch โ€” boardsCleared grew without
bound and fed the boards_10/boards_50 achievements. The
contract says exactly-once per board; the guard held one key.

Banking is now a persisted string-set of banked keys (checked
in the same atomic edit). The legacy single long still counts
as banked (upgrade) and is still written (downgrade keeps the
guard for the latest clear). New regression test exercises
casual -> daily -> replay both. Rules, generation, input and
daily rollover were audited and cleared. Verified:
:games:arrows:testDevUnitTest + lint BUILD SUCCESSFUL.

Diff

games/arrows/src/main/java/com/vayunmathur/games/arrows/data/ArrowsDataStore.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/data/ArrowsDataStore.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/data/ArrowsDataStore.ktindex bcad81cd0e..8d8454e1ee 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/data/ArrowsDataStore.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/data/ArrowsDataStore.kt@@ -106,12 +106,20 @@ class ArrowsDataStore(private val context: Context) {      * The check and the bump share one edit, so a won board replaying through a fresh ViewModel      * after process death โ€” or two collectors firing at once โ€” cannot count it twice. Returns the      * new total, or null if [key] was already counted.+     *+     * Every banked key is kept, not just the latest: a casual board and today's daily can both+     * sit won at once, and switching back to either replays its clear through the progress+     * collector, so remembering only the most recent key would count the other one again.+     * [BANKED_KEY] is the pre-set store's single key; it still counts as banked and is kept+     * updated so a downgrade keeps its guard for the latest clear.      */     suspend fun recordClearedOnce(key: Long): Int? {         var total: Int? = null         context.dataStore.edit { prefs ->-            if (prefs[BANKED_KEY] == key) return@edit+            val banked = prefs[BANKED_KEYS_KEY] ?: emptySet()+            if (key.toString() in banked || prefs[BANKED_KEY] == key) return@edit             val next = (prefs[CLEARED_KEY] ?: 0) + 1+            prefs[BANKED_KEYS_KEY] = banked + key.toString()             prefs[BANKED_KEY] = key             prefs[CLEARED_KEY] = next             total = next@@ -136,7 +144,9 @@ class ArrowsDataStore(private val context: Context) {      private companion object {         val LEVEL_KEY = intPreferencesKey("current_level")-        /** The clear key already banked; guards the lifetime counter across process death. */+        /** Every clear key already banked; guards the lifetime counter across process death. */+        val BANKED_KEYS_KEY = stringSetPreferencesKey("banked_clears")+        /** The latest banked clear key, kept for stores written before [BANKED_KEYS_KEY]. */         val BANKED_KEY = longPreferencesKey("last_banked_clear")         val REMOVED_KEY = stringSetPreferencesKey("removed_arrows")         val HEARTS_KEY = intPreferencesKey("hearts")
games/arrows/src/test/java/com/vayunmathur/games/arrows/data/ArrowsDataStoreTest.kt
diff --git a/games/arrows/src/test/java/com/vayunmathur/games/arrows/data/ArrowsDataStoreTest.kt b/games/arrows/src/test/java/com/vayunmathur/games/arrows/data/ArrowsDataStoreTest.ktindex 5c7b3ac31b..0f5fdfdfde 100644--- a/games/arrows/src/test/java/com/vayunmathur/games/arrows/data/ArrowsDataStoreTest.kt+++ b/games/arrows/src/test/java/com/vayunmathur/games/arrows/data/ArrowsDataStoreTest.kt@@ -107,6 +107,28 @@ class ArrowsDataStoreTest {         assertEquals(after1!! + 1, after2)     } +    @Test+    fun recordClearedOnceDoesNotRecountAfterAnotherBoardIntervenes() = runBlocking {+        val s = store()+        // A casual clear (negative key) and a daily clear (day key) can both sit won at+        // once; switching modes replays the other's clear, so banking must survive the+        // interleaving A -> B -> A rather than only remembering the latest key.+        val casualKey = -777101L+        val dailyKey = 777102L+        val casualTotal = s.recordClearedOnce(casualKey)+        val dailyTotal = s.recordClearedOnce(dailyKey)+        assertEquals(casualTotal!! + 1, dailyTotal)+        assertNull(+            s.recordClearedOnce(casualKey),+            "replaying the casual clear after a daily clear must not count twice",+        )+        assertNull(+            s.recordClearedOnce(dailyKey),+            "replaying the daily clear after a casual clear must not count twice",+        )+        assertEquals(dailyTotal, s.boardsCleared.first())+    }+     @Test     fun ensureDailyDayRollsOverOnlyOnNewDay() = runBlocking {         val s = store()