From 3d09528e5d6d572cda243aab1d96eb2f999036b3 Mon Sep 17 00:00:00 2001 From: Pauljd Date: Sat, 7 Nov 2026 10:44:49 +0000 Subject: [PATCH] Smart setup: at least three running series in a part of the day that has any (Sakura's late nights leaned on two) Co-Authored-By: Claude Opus 5.5 (1M context) --- .../main/java/tv/orbit/classic/SmartSetup.kt | 20 +++++++++++++++++++ .../orbit/classic/SmartSetupRegressionTest.kt | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt b/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt index 4427e961..d1214a3c 100644 --- a/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt +++ b/android/app/src/main/java/tv/orbit/classic/SmartSetup.kt @@ -73,6 +73,11 @@ internal fun slotFor(row: ShapeRow, shared: Set = emptySet()): Slot { } } +/** At least this many series in a part of the day that has any, where another part can spare them. */ +internal const val MIN_SERIES_PER_PART = 3 +/** A series with at least this many hours counts towards a part of the day's variety. */ +internal const val SUBSTANTIAL_SERIES_HOURS = 10.0 + /** Hours a week each slot fills, all day every day. */ private val WeekHours = mapOf(Slot.Daytime to 11 * 7.0, Slot.Evening to 5 * 7.0, Slot.Late to 8 * 7.0) @@ -130,6 +135,21 @@ internal fun shapeChannel(rows: List): Shape { } } } + // Variety at night (Paul, 7 November 2026: Sakura's late nights leaned on two series): a part of the day with series in it + // should have at least three, where the evening can spare them without running short. + for (to in listOf(Slot.Late, Slot.Daytime)) { + // Counted by series with a run of their own: a six-episode show is gone in two nights. + val seriesThere = rows.count { slots[it.id] == to && it.type == "Series" && hours(it) >= SUBSTANTIAL_SERIES_HOURS } + if (seriesThere == 0 || seriesThere >= MIN_SERIES_PER_PART) continue + val spare = rows.filter { slots[it.id] == Slot.Evening && it.type == "Series" && mayMove(it, Slot.Evening, to) } + // The longest-running first, so the night isn't handed a show that runs out in a couple of evenings. + .sortedWith(compareByDescending { hours(it) }.thenBy { listOf(RatingBand.Teen, RatingBand.Unknown, RatingBand.Family, RatingBand.Kids).indexOf(ratingBand(it.rating)) }) + for (c in spare.take(MIN_SERIES_PER_PART - seriesThere)) { + if (rows.count { slots[it.id] == Slot.Evening && it.type == "Series" } <= MIN_SERIES_PER_PART) break + if (content(Slot.Evening) - hours(c) < target(Slot.Evening)) continue + slots[c.id] = to; notes += "${c.title} moves to ${to.label} for more variety." + } + } // Core shows are series where there are any: a film plays once a week whatever its star. val evening = rows.filter { slots[it.id] == Slot.Evening }.ifEmpty { rows }.let { e -> e.filter { it.type == "Series" }.ifEmpty { e } } val pick = evening.sortedByDescending { it.year ?: 0 }.take(3).map { it.id }.toSet() diff --git a/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt b/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt index fceb8e8d..9521445e 100644 --- a/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/SmartSetupRegressionTest.kt @@ -60,6 +60,11 @@ class SmartSetupRegressionTest { assertTrue(repeats(r.plan) <= 15) val firstRepeat = r.plan.entries.filter { it.startMs < now + week }.groupBy { it.media.item.id }.values.filter { it.size > 1 }.minOfOrNull { it[1].startMs } assertTrue(firstRepeat == null || firstRepeat - now >= 5 * 24 * 3_600_000L) + // Late nights have at least three series (it leaned on two), and no one series takes more than 15 late slots a week. + assertTrue(r.rows.count { r.shape.slots[it.id] == Slot.Late && it.type == "Series" && (it.runMs ?: 0) >= SUBSTANTIAL_SERIES_HOURS * 3_600_000 } >= MIN_SERIES_PER_PART) + val lateBySeries = r.plan.entries.filter { it.startMs < now + week && java.time.Instant.ofEpochMilli(it.startMs).atZone(zone).hour.let { h -> h >= 22 || h < 6 } } + .filter { e -> r.rows.first { it.id == e.seriesId }.type == "Series" }.groupingBy { it.seriesId }.eachCount() + assertTrue(lateBySeries.toString(), lateBySeries.values.max() <= 15) // Every title gets airtime in the week. assertEquals(r.rows.size, r.plan.entries.filter { it.startMs < now + week }.map { it.seriesId }.toSet().size) }