diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 8cfcf51e..151babc1 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -9,8 +9,8 @@ android { applicationId = "tv.orbit.classic" minSdk = 26 targetSdk = 36 - versionCode = 292 - versionName = "0.110.9-combined" + versionCode = 293 + versionName = "0.110.10-household" } buildFeatures { compose = true } // The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so diff --git a/android/app/src/main/java/tv/orbit/classic/InstallSnapshot.kt b/android/app/src/main/java/tv/orbit/classic/InstallSnapshot.kt index 7c0ef7b7..fd81b29e 100644 --- a/android/app/src/main/java/tv/orbit/classic/InstallSnapshot.kt +++ b/android/app/src/main/java/tv/orbit/classic/InstallSnapshot.kt @@ -34,6 +34,13 @@ class InstallSnapshotReceiver : BroadcastReceiver() { try { InstallRestore.apply(context.filesDir.parentFile!!, folder, intent.getStringExtra("confirm"), System.currentTimeMillis()) } finally { if (intent.getStringExtra("confirm") == "RESTORE") folder.deleteRecursively() } } + } else if (intent.getStringExtra("do") == "hive-household") { + // BumbleHive's Household save from another TV, pushed into install-restore/ (see HiveLegacy.adoptCopy). + val incoming = File(context.getExternalFilesDir(null), "install-restore/hive-household.json") + try { + tv.orbit.classic.arcade.hive.HiveLegacy.adoptCopy(File(context.filesDir, "arcade"), incoming, intent.getStringExtra("sha256"), + intent.getStringExtra("confirm"), intent.getStringExtra("from"), System.currentTimeMillis()) + } finally { incoming.delete() } } else InstallSnapshot.handle(context, intent.getStringExtra("do").orEmpty(), intent.getStringExtra("version")) } resultData = result.fold({ it }, { "failed" }) diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeHome.kt b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeHome.kt index acaf7c98..cbc6f3d2 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeHome.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeHome.kt @@ -103,7 +103,10 @@ internal fun OrbitArcade(modifier: Modifier, onPlaying: (Boolean) -> Unit, onExi DisposableEffect(Unit) { onPlaying(true); onDispose { onPlaying(false) } } fun update(change: (ArcadeProfileData) -> ArcadeProfileData) { data = repo.store.update(profile.id, change) } - fun start(game: ArcadeGame) { playing = game; session = sessionStart(data.game(game.id)) } + fun start(game: ArcadeGame) { + if (game.id == ArcadeGames.Sky.id) carrySkydokuCount(data, household).let { if (it !== data) update { d -> carrySkydokuCount(d, household) } } + playing = game; session = sessionStart(data.game(game.id)) + } fun launch(game: ArcadeGame) { if (!game.launchable) { note = "${game.title} is coming soon to the Arcade."; return } if (game !in visible) return diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeStore.kt b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeStore.kt index bc992eed..52ada3ad 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeStore.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/ArcadeStore.kt @@ -247,6 +247,24 @@ internal object HouseholdRecords { }.getOrDefault(emptyMap()) } +// ---------- Skydoku's count from before profiles (8 November 2026) ---------- + +/** Marks, in Skydoku's kept values, that this profile has had the TV's old count carried in (so it happens once). */ +internal const val SKY_CARRIED = "householdCarried" + +/** + * Paul, 8 November 2026: Skydoku's solved count looked lost after the Arcade went per profile, because each profile starts + * from 0 and the TV's old count (orbit_settings skydoku_solved, kept read-only in Household Records) only shows there. The + * first time a profile opens Skydoku, its count becomes the better of its own and the TV's old one. Never lower, once per + * profile, and the Household Record itself is never changed. Nothing happens without a Household count above 0. + */ +internal fun carrySkydokuCount(data: ArcadeProfileData, household: Map): ArcadeProfileData { + val old = household["skydoku_solved"]?.number ?: return data + val g = data.game("Sky") + if (old <= 0 || g.kept[SKY_CARRIED] != null) return data + return data.withGame("Sky") { it.copy(count = maxOf(it.count, old), kept = it.kept + (SKY_CARRIED to old.toString())) } +} + // ---------- Gameplay choices (Stage D, frame 05's Gameplay card) ---------- /** How hard a game pushes. Kids start on Gentle; everyone else on Normal, until they choose. */ diff --git a/android/app/src/main/java/tv/orbit/classic/arcade/hive/HiveLegacy.kt b/android/app/src/main/java/tv/orbit/classic/arcade/hive/HiveLegacy.kt index b3e9c5a9..f7a99042 100644 --- a/android/app/src/main/java/tv/orbit/classic/arcade/hive/HiveLegacy.kt +++ b/android/app/src/main/java/tv/orbit/classic/arcade/hive/HiveLegacy.kt @@ -77,6 +77,43 @@ internal object HiveLegacy { Snapshot(o.optBoolean("present"), if (o.isNull("value")) null else o.optString("value"), o.optLong("copiedAt")) }.getOrNull() + /** + * Giving another TV the Household save (8 November 2026: upstairs never had a WebView save; its storage was created by + * the import, so its snapshot says "nothing there"). A copy of another TV's snapshot, pushed by adb with its SHA-256, + * becomes this TV's snapshot, so each profile is asked "Carry on from this TV's hive?" and chooses for themselves. + * + * Refused unless [confirm] is "HOUSEHOLD"; unless the copy's checksum matches; unless it's a read-only snapshot of + * schema 1 holding a save game.js could read with progress in it; and whenever this TV already has a snapshot that holds + * a save, or one that can't be read. Only a missing snapshot, or one that says "nothing there", is replaced (that one is + * kept beside it). The save's text is kept exactly; where it came from is recorded. Profiles' Arcade files are never + * touched. Returns what happened. + */ + fun adoptCopy(dir: File, incoming: File, sha256: String?, confirm: String?, from: String?, nowMs: Long): String { + if (confirm != "HOUSEHOLD") return "refused: needs confirm HOUSEHOLD" + if (!incoming.isFile) return "refused: no copy pushed" + val bytes = incoming.readBytes() + val sum = java.security.MessageDigest.getInstance("SHA-256").digest(bytes).joinToString("") { "%02x".format(it) } + if (sha256 == null || !sum.equals(sha256.trim(), ignoreCase = true)) return "refused: checksum doesn't match" + val o = runCatching { JSONObject(String(bytes, Charsets.UTF_8)) }.getOrNull() ?: return "refused: not a snapshot" + if (o.optInt("schema") != 1 || !o.optBoolean("readOnly") || o.optString("source") != "localStorage/$KEY") return "refused: not a snapshot" + if (!o.optBoolean("present") || o.isNull("value")) return "refused: the copy holds no save" + val value = o.optString("value") + val save = LegacyHiveSave.parse(value) ?: return "refused: the copy's save can't be read" + if (!save.hasProgress) return "refused: the copy's save has no progress" + val target = file(dir) + if (target.exists()) { + val here = load(dir) ?: return "refused: this TV's snapshot can't be read" + if (here.present) return "refused: this TV already has a save" + } + val label = from?.takeIf { Regex("""[A-Za-z0-9 :._-]{1,80}""").matches(it) } ?: "another TV" + val json = JSONObject().put("schema", 1).put("readOnly", true).put("source", "localStorage/$KEY").put("origin", ORIGIN_URL) + .put("copiedAt", nowMs).put("present", true).put("value", value).put("profiles", JSONObject()) + .put("copiedFrom", label).put("copiedFromAt", o.optLong("copiedAt")) + dir.mkdirs() + if (target.exists()) runCatching { target.copyTo(File(dir, "$FILE.empty-$nowMs"), overwrite = true) } + return runCatching { writeSafely(target, json.toString()); "copied: ${save.progress.unlocked} unlocked, ${save.progress.totalStars()} stars" }.getOrElse { "failed: ${it.message}" } + } + /** * The import, given a [read] that asks the old storage and calls back with evaluateJavascript's answer (null on any * failure). Runs the read only when there's no snapshot yet; [done] gets the snapshot, or null when the read failed. diff --git a/android/app/src/test/java/tv/orbit/classic/arcade/hive/HouseholdCopyTest.kt b/android/app/src/test/java/tv/orbit/classic/arcade/hive/HouseholdCopyTest.kt new file mode 100644 index 00000000..1afa8ecc --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/arcade/hive/HouseholdCopyTest.kt @@ -0,0 +1,130 @@ +package tv.orbit.classic.arcade.hive + +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import tv.orbit.classic.arcade.ArcadeProfileData +import tv.orbit.classic.arcade.GameRecord +import tv.orbit.classic.arcade.HouseholdRecord +import tv.orbit.classic.arcade.ResetAction +import tv.orbit.classic.arcade.SKY_CARRIED +import tv.orbit.classic.arcade.applyReset +import tv.orbit.classic.arcade.carrySkydokuCount +import java.io.File +import java.security.MessageDigest + +/** 0.110.10: giving upstairs the downstairs Household save, and Skydoku's old count carried into each profile. */ +class HouseholdCopyTest { + @get:Rule val tmp = TemporaryFolder() + + private val downstairsSave = JSONObject().put("unlocked", 48).put("bombs", 2) + .put("stars", JSONObject().also { o -> for (n in 1..47) o.put("$n", if (n <= 2) 3 else 2) }).toString() + + private fun snapshotText(present: Boolean = true, value: String? = downstairsSave, readOnly: Boolean = true, schema: Int = 1) = + JSONObject().put("schema", schema).put("readOnly", readOnly).put("source", "localStorage/orbithive-save-v1").put("origin", HiveLegacy.ORIGIN_URL) + .put("copiedAt", 1794176482158L).put("present", present).put("value", value ?: JSONObject.NULL).put("profiles", JSONObject()).toString() + + private fun sha(f: File) = MessageDigest.getInstance("SHA-256").digest(f.readBytes()).joinToString("") { "%02x".format(it) } + + /** An upstairs arcade folder whose import found nothing, and a pushed copy of downstairs' snapshot. */ + private fun setup(here: String? = snapshotText(present = false, value = null), copy: String = snapshotText()): Pair { + val dir = tmp.newFolder() + if (here != null) HiveLegacy.file(dir).writeText(here) + File(dir, "grown.json").writeText("{\"profile\":\"untouched\"}") + val incoming = tmp.newFile().also { it.writeText(copy) } + return dir to incoming + } + + @Test fun anEmptySnapshotTakesTheCopyExactlyAndKeepsTheOldOneBeside() { + val (dir, incoming) = setup() + val result = HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "HOUSEHOLD", "downstairs 63586700000184484", 5L) + assertEquals("copied: 48 unlocked, 96 stars", result) + val snap = HiveLegacy.load(dir)!! + assertTrue(snap.present); assertEquals(downstairsSave, snap.value) + assertEquals("downstairs 63586700000184484", JSONObject(HiveLegacy.file(dir).readText()).getString("copiedFrom")) + assertTrue(File(dir, "${HiveLegacy.FILE}.empty-5").isFile) + // Profiles' own files are never touched, and each profile is now offered it. + assertEquals("{\"profile\":\"untouched\"}", File(dir, "grown.json").readText()) + assertTrue(offerHousehold(HiveSaveData(), snap.save)) + } + + @Test fun noSnapshotYetAlsoTakesTheCopy() { + val (dir, incoming) = setup(here = null) + assertTrue(HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "HOUSEHOLD", null, 5L).startsWith("copied")) + assertEquals("another TV", JSONObject(HiveLegacy.file(dir).readText()).getString("copiedFrom")) + } + + @Test fun refusesWithoutConfirmOrTheRightChecksum() { + val (dir, incoming) = setup() + val before = HiveLegacy.file(dir).readBytes() + assertEquals("refused: needs confirm HOUSEHOLD", HiveLegacy.adoptCopy(dir, incoming, sha(incoming), null, null, 5L)) + assertEquals("refused: needs confirm HOUSEHOLD", HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "RESTORE", null, 5L)) + assertEquals("refused: checksum doesn't match", HiveLegacy.adoptCopy(dir, incoming, "0".repeat(64), "HOUSEHOLD", null, 5L)) + assertEquals("refused: checksum doesn't match", HiveLegacy.adoptCopy(dir, incoming, null, "HOUSEHOLD", null, 5L)) + assertEquals("refused: no copy pushed", HiveLegacy.adoptCopy(dir, File(dir, "nothing.json"), "x", "HOUSEHOLD", null, 5L)) + assertArrayEquals(before, HiveLegacy.file(dir).readBytes()) + } + + @Test fun refusesACopyThatIsntAGoodSave() { + val bad = listOf( + "not json" to "refused: not a snapshot", + snapshotText(readOnly = false) to "refused: not a snapshot", + snapshotText(schema = 2) to "refused: not a snapshot", + snapshotText(present = false, value = null) to "refused: the copy holds no save", + snapshotText(value = "{broken") to "refused: the copy's save can't be read", + snapshotText(value = "{\"unlocked\":1}") to "refused: the copy's save has no progress", + ) + for ((copy, why) in bad) { + val (dir, incoming) = setup(copy = copy) + val before = HiveLegacy.file(dir).readBytes() + assertEquals(why, HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "HOUSEHOLD", null, 5L)) + assertArrayEquals(before, HiveLegacy.file(dir).readBytes()) + } + } + + @Test fun neverReplacesARealSaveOrAnUnreadableSnapshot() { + for ((here, why) in listOf(snapshotText(value = "{\"unlocked\":3}") to "refused: this TV already has a save", "{broken" to "refused: this TV's snapshot can't be read")) { + val (dir, incoming) = setup(here = here) + assertEquals(why, HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "HOUSEHOLD", null, 5L)) + assertEquals(here, HiveLegacy.file(dir).readText()) + } + } + + @Test fun anOddSourceLabelIsNotWritten() { + val (dir, incoming) = setup() + HiveLegacy.adoptCopy(dir, incoming, sha(incoming), "HOUSEHOLD", "x\"; rm -rf /", 5L) + assertEquals("another TV", JSONObject(HiveLegacy.file(dir).readText()).getString("copiedFrom")) + } + + // ---------- Skydoku ---------- + + private val household = mapOf("skydoku_solved" to HouseholdRecord("skydoku_solved", 44, null, "orbit_settings/skydoku_solved", 1L)) + + @Test fun skydokusOldCountIsCarriedInOnceKeepingTheBest() { + val fresh = ArcadeProfileData("kid") + val carried = carrySkydokuCount(fresh, household) + assertEquals(44, carried.game("Sky").count); assertEquals("44", carried.game("Sky").kept[SKY_CARRIED]) + // Once only: solving more counts on from there, and it isn't carried again. + val more = carried.withGame("Sky") { it.copy(count = 45) } + assertSame(more, carrySkydokuCount(more, household)) + // A profile already past the old count keeps its own. + val ahead = ArcadeProfileData("p").withGame("Sky") { GameRecord(count = 60) } + assertEquals(60, carrySkydokuCount(ahead, household).game("Sky").count) + // One with a few of its own goes up to the old count, never down. + val few = ArcadeProfileData("p").withGame("Sky") { GameRecord(count = 1, level = mapOf("daily" to "2026-11-08")) } + val up = carrySkydokuCount(few, household).game("Sky") + assertEquals(44, up.count); assertEquals(mapOf("daily" to "2026-11-08"), up.level) + } + + @Test fun skydokuWithNoOldCountIsLeftAloneAndAClearIsntUndone() { + val p = ArcadeProfileData("p") + assertSame(p, carrySkydokuCount(p, emptyMap())) + assertSame(p, carrySkydokuCount(p, mapOf("skydoku_solved" to HouseholdRecord("skydoku_solved", 0, null, "x", 1L)))) + // Cleared on purpose after the carry: it stays cleared. + val cleared = applyReset(carrySkydokuCount(p, household), "Sky", ResetAction.ClearHighScores, 9L) + assertEquals(0, cleared.game("Sky").count) + assertEquals(0, carrySkydokuCount(cleared, household).game("Sky").count) + } +}