arrows: a daily banked before its streak write still records the day
- Commit
21e07a5b19992e679eab3b332f902db73a31eb54- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:34 UTC (5 days ago)
- Parents
2c6212e
onCleared gated everything a clear records on the lifetime- count bank: recordClearedOnce(key) ?: return. The bank (Arrows settings DataStore) and DailyChallengeStore. recordDayCompleted (shared default DataStore) are separate writes, so a first recording that died between them — bank committed, then process death before the day was written — left a permanently won board whose every replay early-returned: the day's completion, streak, hub report and first_daily/streak progress were lost forever, and dailyDone could never become true on relaunch. The bank now gates only the lifetime count and the boards_10/boards_50 progress. The idempotent effects — unlocks (latched via addStringToSetIfAbsent) and, for dailies, recordDayCompleted plus streak reporting (a same-day re-record yields the same streak) — run on every replayed win. Regression test replays a won daily whose bank was already committed but whose last-completed day lags, and asserts the day records on relaunch (timed out pre-fix). Escape validation, generation across the shipped seed sweep and save-restore separation were audited and cleared. Verified: :games:arrows:testDevUnitTest and :games:arrows:lint BUILD SUCCESSFUL.
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 116235672b..93302ce7fa 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@@ -430,14 +430,24 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application), * replays onto the still-persisted won board. The banked key in [ArrowsDataStore] makes the * count exactly-once across all of those: the check and the bump are one atomic edit, which * an in-memory guard could never be.+ *+ * The bank gates only that count (and the lifetime progress fed by it). Everything else a+ * clear records — the unlocks, and for a daily the day's completion and streak — is+ * idempotent under replay and runs every time: the banked edit and the daily store are+ * separate writes, so a first recording can die between them (the bank commits, the process+ * goes before the day is recorded), and a replay that skipped the rest on the bank's say-so+ * would leave that clear's daily permanently unrecorded even though its board still sits+ * won. Re-running them costs nothing: unlocks latch, and recording the same day again+ * yields the same streak. */ private fun onCleared(game: ArrowsGameState, daily: Boolean, day: Long) { // Daily clears are keyed by day and casual ones by negated level, which cannot collide. val key = if (daily) day else -game.level.toLong() viewModelScope.launch(storeWrites) {- // Null means this board was already banked — a replayed win, not a new clear.- val total = dataStore.recordClearedOnce(key) ?: return@launch+ // Null means this board was already banked — a replayed win, not a new clear — so+ // only the lifetime count and its progress skip; the rest below still runs.+ val total = dataStore.recordClearedOnce(key) val manager = achievementsManager manager.onAchievementUnlocked("first_board") if (game.hearts == STARTING_HEARTS) manager.onAchievementUnlocked("flawless")@@ -446,8 +456,10 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application), // Clearing a board with redirectors shows they are understood; stop explaining. dataStore.markRedirectorHintSeen() }- manager.onProgressUpdated("boards_10", total)- manager.onProgressUpdated("boards_50", total)+ if (total != null) {+ manager.onProgressUpdated("boards_10", total)+ manager.onProgressUpdated("boards_50", total)+ } if (daily) { manager.onAchievementUnlocked("first_daily")
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 7480ad4cf2..2aab9735b7 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@@ -5,8 +5,14 @@ import androidx.lifecycle.ViewModelStore import androidx.test.core.app.ApplicationProvider import androidx.work.Configuration import androidx.work.WorkManager+import com.vayunmathur.games.arrows.data.ArrowsDataStore+import com.vayunmathur.games.arrows.data.DAILY_ARROWS+import com.vayunmathur.games.arrows.data.DAILY_BOARD+import com.vayunmathur.games.arrows.data.DAILY_MIRRORS import com.vayunmathur.games.arrows.data.GameMode+import com.vayunmathur.games.arrows.domain.ArrowsGenerator import com.vayunmathur.games.arrows.domain.ArrowsRules+import com.vayunmathur.library.util.DailyChallengeStore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi@@ -282,6 +288,51 @@ class ArrowsViewModelTest { blocking { second.dataStore.resetBoard(daily = false) } } + @Test+ fun dailyWinBankedBeforeItsStreakWasRecordedStillRecordsTheDailyOnRelaunch() {+ // The aftermath of a process death mid-clear: the lifetime banking edit committed+ // (so the board can never be counted again), but the coroutine died before the+ // daily store recorded the completion. The won board is still persisted, so the+ // next launch replays it — and that replay is the only chance left to record the+ // daily. Banking must gate the count, not the day's completion.+ val dailyStore = DailyChallengeStore(app, ArrowsViewModel.DAILY_KEY_PREFIX)+ val day = dailyStore.todayEpochDay()+ val (cols, rows) = DAILY_BOARD+ val puzzle = assertNotNull(+ ArrowsGenerator.generateSeeded(+ cols = cols,+ rows = rows,+ targetPieces = DAILY_ARROWS,+ mirrorCount = DAILY_MIRRORS,+ seed = 1_300_000_000L + day,+ ),+ "no daily board for today",+ )+ val store = ArrowsDataStore(app)+ blocking {+ // Yesterday was the last recorded completion, and today's board sits won+ // with its clear already banked — exactly the interrupted state above.+ dailyStore.recordDayCompleted(day - 1)+ store.setGameMode(GameMode.DAILY)+ store.ensureDailyDay(day)+ store.saveProgress(puzzle.pieces.map { it.id }.toSet(), 3, daily = true)+ store.recordClearedOnce(day)+ }+ val vm = tracked(ArrowsViewModel(app))+ try {+ await("relaunch loads the won daily board") {+ vm.uiState.value.mode == GameMode.DAILY && vm.uiState.value.game?.isWon == true+ }+ await("replayed win records the daily completion") { vm.uiState.value.dailyDone }+ } finally {+ vm.setGameMode(GameMode.CASUAL)+ await("back to casual") {+ vm.uiState.value.mode == GameMode.CASUAL && vm.uiState.value.game != null+ }+ blocking { store.resetBoard(daily = true) }+ }+ }+ @Test fun dailyFlowAndReminderSettings() { runCatching {