diff --git a/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt b/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt index 6bbb4817..3b1ca939 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt @@ -364,6 +364,7 @@ internal fun extendFilmSchedule(pool: List, cursor: ScheduleCursor, var last = cursor.lastSeriesId var start = fromMs val marks = cursor.marks.toMutableMap() + val blockRest = HashMap() while (start < untilMs) { val (available, block, part) = dayPartFor(pool, dayParts, start, zone) // An event naming only a series plays its next whole episode, once a night. Everywhere else is films only. @@ -380,7 +381,7 @@ internal fun extendFilmSchedule(pool: List, cursor: ScheduleCursor, } val films = available.filter { it.isFilm }.ifEmpty { pool.filter { it.isFilm } }.ifEmpty { available } // Events keep the channel's rest; everyday blocks, when adaptive, rest by how long their own films last. - val rest = if (adaptiveRest && part?.days.isNullOrEmpty()) adaptiveRestMs(films, part, restMs) else restMs + val rest = if (adaptiveRest && part?.days.isNullOrEmpty()) blockRest.getOrPut(part) { sharedAdaptiveRestMs(pool, dayParts, part, restMs) } else restMs val (film, why) = chooseFilm(films, shown, start, rest, avoid) val copy = film.episodes.first() val item = SampleItem(film.title, "Movie", copy.durationMs / 60_000, "", copy.id, copy.durationMs) @@ -405,13 +406,46 @@ internal const val ADAPTIVE_REST_CEILING_MS = 14 * 24 * 60 * 60_000L * pick comes from a real shortlist instead of the same longest-waiting order every cycle. When it's longer, up to * [ADAPTIVE_REST_LONG_SHARE] of it but never less than [restMs], so a big pool airs more of itself. */ -internal fun adaptiveRestMs(films: List, part: DayPart?, restMs: Long): Long { - val hours = part?.let { ((it.toHour - it.fromHour + 24) % 24).let { h -> if (h == 0) 24 else h } } ?: 24 +internal fun adaptiveRestMs(films: List, part: DayPart?, restMs: Long): Long = adaptiveRestMs(films, hoursOf(part), restMs) + +private fun hoursOf(part: DayPart?) = part?.let { ((it.toHour - it.fromHour + 24) % 24).let { h -> if (h == 0) 24 else h } } ?: 24 + +private fun adaptiveRestMs(films: List, hours: Int, restMs: Long): Long { val cycle = films.sumOf { it.episodes.first().durationMs }.toDouble() * 24 / hours val rest = if (cycle * ADAPTIVE_REST_SHORT_SHARE < restMs) cycle * ADAPTIVE_REST_SHORT_SHARE else maxOf(restMs.toDouble(), cycle * ADAPTIVE_REST_LONG_SHARE) return rest.toLong().coerceIn(ADAPTIVE_REST_FLOOR_MS, ADAPTIVE_REST_CEILING_MS) } +/** The films an everyday block may play, as [dayPartFor] chooses them, without its fallbacks. */ +private fun filmsIn(pool: List, part: DayPart): List { + if (part.titles.isEmpty() && part.group.isEmpty()) return pool.filter { it.isFilm } + val titles = part.titles.map(::seriesKey).toSet() + val films = part.titles.map { seriesKey(FilmIdentity.of(it).title) }.toSet() + return pool.filter { it.isFilm && (seriesKey(it.title) in titles || seriesKey(it.title) in films || (part.group.isNotEmpty() && it.group == part.group)) } +} + +/** + * [adaptiveRestMs] for an everyday block that shares most of its films with another everyday block (each holding at least + * half the other's films). A film rests across every block it plays in, so such a block's films are rested by how many + * hours a day all the everyday blocks draw on them, each block in proportion to how much of its pool they make up, and + * never longer than the block alone would rest them. Without this, a short block sharing most of its films with a long one + * keeps a rest worked out from its own few hours and falls back to the longest-waiting order (After Dark's 18:00 to 21:00 + * in Option C). A block with no such partner is rested on its own exactly as before. + */ +internal fun sharedAdaptiveRestMs(pool: List, dayParts: List, part: DayPart?, restMs: Long): Long { + if (part == null) return adaptiveRestMs(pool.filter { it.isFilm }, null, restMs) + val own = filmsIn(pool, part).ifEmpty { pool.filter { it.isFilm } } + val alone = adaptiveRestMs(own, part, restMs) + val ids = own.mapTo(HashSet()) { it.id } + val everyday = dayParts.filter { it.days.isEmpty() }.map { it to filmsIn(pool, it) }.filter { it.second.isNotEmpty() } + val partnered = everyday.any { (other, films) -> other != part && films.count { it.id in ids }.let { shared -> shared * 2 >= own.size && shared * 2 >= films.size } } + if (!partnered) return alone + val demandHours = everyday.sumOf { (other, films) -> hoursOf(other) * films.count { it.id in ids }.toDouble() / films.size } + val cycle = own.sumOf { it.episodes.first().durationMs }.toDouble() * 24 / demandHours + val shared = if (cycle * ADAPTIVE_REST_SHORT_SHARE < restMs) cycle * ADAPTIVE_REST_SHORT_SHARE else maxOf(restMs.toDouble(), cycle * ADAPTIVE_REST_LONG_SHARE) + return minOf(alone, shared.toLong().coerceIn(ADAPTIVE_REST_FLOOR_MS, ADAPTIVE_REST_CEILING_MS)) +} + /** A one-a-night event counts as having had its turn for this long after a programme from it starts. */ private const val ONCE_A_NIGHT_MS = 12 * 60 * 60_000L /** How far ahead an on-time appointment is looked for, and how late after its hour it may start. */ diff --git a/android/app/src/test/java/tv/orbit/classic/AdaptiveFilmRestTest.kt b/android/app/src/test/java/tv/orbit/classic/AdaptiveFilmRestTest.kt index 87ddc70d..903535b3 100644 --- a/android/app/src/test/java/tv/orbit/classic/AdaptiveFilmRestTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/AdaptiveFilmRestTest.kt @@ -40,4 +40,18 @@ class AdaptiveFilmRestTest { plan.entries.filter { "event" in it.why && "picked from" in it.why }.groupBy { it.seriesId }.values.forEach { es -> es.zipWithNext().forEach { (a, b) -> assertTrue(b.startMs - a.startMs >= 7 * day) } } assertEquals(setOf(301, 305), SmartChannels.filter { it.adaptiveFilmRest }.map { it.number }.toSet()) } + + @Test fun blocksSharingMostOfTheirFilmsAreRestedTogether() { + val all = films(130) + val daytime = DayPart(5, 18, all.take(50).map { it.title }); val evening = DayPart(18, 21, all.take(60).map { it.title }); val late = DayPart(21, 5) + // On its own the evening's three hours would rest its films for the 14-day ceiling; sharing 50 of 60 with the day, it can't. + assertEquals(ADAPTIVE_REST_CEILING_MS, adaptiveRestMs(all.take(60), evening, 7 * day)) + val shared = sharedAdaptiveRestMs(all, listOf(daytime, evening, late), evening, 7 * day) + assertTrue(shared < adaptiveRestMs(all.take(50), daytime, 7 * day) + day) + // A block with no such partner keeps its own rest exactly: After Dark's day films within its nights, as built. + assertEquals(adaptiveRestMs(all.take(50), DayPart(5, 21, all.take(50).map { it.title }), 7 * day), + sharedAdaptiveRestMs(all, listOf(DayPart(5, 21, all.take(50).map { it.title }), late), DayPart(5, 21, all.take(50).map { it.title }), 7 * day)) + assertEquals(adaptiveRestMs(all, late, 7 * day), sharedAdaptiveRestMs(all, listOf(daytime, evening, late), late, 7 * day)) + } + } diff --git a/android/app/src/test/java/tv/orbit/classic/AfterDarkDaytimeReplayTest.kt b/android/app/src/test/java/tv/orbit/classic/AfterDarkDaytimeReplayTest.kt index 510007f5..80e8e4c3 100644 --- a/android/app/src/test/java/tv/orbit/classic/AfterDarkDaytimeReplayTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/AfterDarkDaytimeReplayTest.kt @@ -89,7 +89,7 @@ class AfterDarkDaytimeReplayTest { } val rests = parts.filter { it.days.isEmpty() }.map { part -> val (available, _, _) = dayPartFor(series, listOf(part), java.time.LocalDate.of(2026, 11, 16).atTime(part.fromHour, 30).atZone(zone).toInstant().toEpochMilli(), zone) - "${part.fromHour}-${part.toHour} ${available.count { it.isFilm }} films, rest ${"%.1f".format(adaptiveRestMs(available.filter { it.isFilm }, part, base.filmRestMs!!) / day.toDouble())} d" + "${part.fromHour}-${part.toHour} ${available.count { it.isFilm }} films, rest ${"%.1f".format(sharedAdaptiveRestMs(series, parts, part, base.filmRestMs!!) / day.toDouble())} d" } out.appendLine("== $label") out.appendLine(" blocks: ${rests.joinToString("; ")}") @@ -97,6 +97,7 @@ class AfterDarkDaytimeReplayTest { out.appendLine(line("Late 21:00 to 05:00", nightEntries)) out.appendLine(" Whole channel: ${new.map { it.media.item.id }.distinct().size} different films; any film again within 3 days ${allGaps.count { it < 3 }}, within 7 days ${allGaps.count { it < 7 }}") out.appendLine(" On another channel at the same time: ${new.count { e -> others(series.first { it.id == e.seriesId }, e.startMs, e.endMs) }}; gaps ${new.zipWithNext().count { (a, b) -> a.endMs != b.startMs }}") + out.appendLine(" Fixed-order fallbacks by hour: " + new.filter { "no film past its rest" in it.why }.groupBy { hour(it) }.toSortedMap().entries.joinToString { "${it.key}:00 ${it.value.size}" }.ifEmpty { "none" }) out.appendLine(" Most-screened by day: " + dayEntries.groupBy { it.media.item.title }.entries.sortedByDescending { it.value.size }.take(5).joinToString { "${it.key} ${it.value.size}" }) out.appendLine() } diff --git a/docs/replays/after-dark-option-c-2026-11-04.md b/docs/replays/after-dark-option-c-2026-11-04.md new file mode 100644 index 00000000..a68ab63f --- /dev/null +++ b/docs/replays/after-dark-option-c-2026-11-04.md @@ -0,0 +1,38 @@ +# After Dark Option C: the fallbacks fixed + +This is a verified proposal from 4 November 2026, asked for by Paul at 09:50. Option C itself isn't enabled. The rest fix it needs is in the code, but it changes nothing on today's channels: a four-week replay of all 30 channels gives identical fingerprints with and without it. + +## The cause + +Option C splits After Dark's day into 05:00 to 18:00, with Option B's 53 films, and 18:00 to 21:00, with those 53 plus 12 grown-up cult titles. Each everyday block worked out its own rest from its own hours. + +- **The evening block's rest was far too long.** Its 65 films over just 3 hours a day came out at the 14-day ceiling. +- **The daytime's rest assumed only daytime used its films.** Its 53 films over 13 hours gave 6.2 days, but the evening and the late block draw on the same films too. + +A film rests across every block it plays in, so the shared films were never rested in time. The daytime fell back to the fixed longest-waiting order: 36 to 67 times in four weeks, depending on the data copy. + +## The fix (`sharedAdaptiveRestMs`) + +When two everyday blocks each hold at least half of the other's films, they're rested together. A block's films rest according to how many hours a day all the everyday blocks draw on them, each block counted in proportion to how much of its own pool those films make up. The result is never longer than the block alone would rest them. + +A block without such a partner is rested exactly as before. That covers After Dark today: its day films are only 40% of its night pool. The rule only changes anything for a layout like Option C. + +## Verified: four weeks on the real pool + +Box copies were taken at 10:05, and bookings avoid every other channel's plan (docs/replays/after-dark-option-c-2026-11-04.txt). + +| | Option B (installable as 0.99.8) | Option C before the fix | **Option C with the fix** | +|---|---|---|---| +| Films by day (05:00 to 21:00) | 53 | 65 | **63** | +| Daytime repeat gap, median / shortest | 5.5 / 5.0 d | 6.3 / 6.1 d | **5.6 / 4.3 d** | +| Fixed-order fallbacks | 0 | 31 to 67 | **0** | +| Late films / late rest | 73 / 14 d | 68 / 14 d | **73 / 14 d** | +| Any film again within 7 days, whole channel | 202 | 179 | **161** | +| Clashes, gaps | 0, 0 | 0, 0 | **0, 0** | + +With the fix, Option C keeps B's median daytime gap while adding the 10 evening titles that air, with no fallbacks and fewer weekly repeats across the channel. The shortest daytime gap falls from 5.0 to 4.3 days: daytime films rest 4.3 days and evening ones 4.9, because the evening and late blocks now share the daytime films' load. + +## To approve + +- **The 12 evening titles (18:00 to 21:00 only):** Barbarella, Brazil, Withnail & I, The Big Lebowski, Heathers, The Rocky Horror Picture Show, Army of Darkness, Death Race 2000, Penda's Fen, Robin Redbreast, Beauty and the Beast (1979), The Outcasts. +- **If approved,** Option C would be its own small release with the usual checks. The intense and NC-17 films stay late-night only. diff --git a/docs/replays/after-dark-option-c-2026-11-04.txt b/docs/replays/after-dark-option-c-2026-11-04.txt new file mode 100644 index 00000000..78b9bc73 --- /dev/null +++ b/docs/replays/after-dark-option-c-2026-11-04.txt @@ -0,0 +1,30 @@ +After Dark daytime options: four weeks of new bookings after the committed plan, from box copies. Read-only. +Films named but not in the pool: none + +== A. Before Option B: 05:00 to 21:00 from the day list and day suggestions + blocks: 21-5 131 films, rest 14.0 d; 5-21 34 films, rest 3.3 d + Daytime 05:00 to 21:00: 257 screenings, 34 different films; repeat gap median 3.7 d, shortest 3.3 d; fixed-order fallbacks 0 + Late 21:00 to 05:00: 107 screenings, 84 different films; repeat gap median 17.1 d, shortest 14.1 d; fixed-order fallbacks 0 + Whole channel: 127 different films; any film again within 3 days 0, within 7 days 223 + On another channel at the same time: 0; gaps 0 + Fixed-order fallbacks by hour: none + Most-screened by day: The League of Gentlemen Are Behind You 8, Mulholland Drive 8, They Live 8, The Frighteners 8, Halloween 8 + +== B. As built (Option B): 19 lighter cult films also allowed 05:00 to 21:00 + blocks: 21-5 131 films, rest 14.0 d; 5-21 53 films, rest 5.0 d + Daytime 05:00 to 21:00: 262 screenings, 53 different films; repeat gap median 5.5 d, shortest 5.0 d; fixed-order fallbacks 0 + Late 21:00 to 05:00: 107 screenings, 73 different films; repeat gap median 15.8 d, shortest 14.0 d; fixed-order fallbacks 0 + Whole channel: 135 different films; any film again within 3 days 0, within 7 days 202 + On another channel at the same time: 0; gaps 0 + Fixed-order fallbacks by hour: none + Most-screened by day: It Came from Outer Space 6, Night of the Comet 6, Phantom of the Paradise 6, Count Dracula 6, The Day of the Triffids 6 + +== C. Graded: B by day, then 18:00 to 21:00 also 12 cult comedies and British folk-horror television + blocks: 21-5 131 films, rest 14.0 d; 5-18 53 films, rest 4.3 d; 18-21 65 films, rest 4.9 d + Daytime 05:00 to 21:00: 260 screenings, 63 different films; repeat gap median 5.6 d, shortest 4.3 d; fixed-order fallbacks 0 + Late 21:00 to 05:00: 107 screenings, 73 different films; repeat gap median 15.1 d, shortest 14.0 d; fixed-order fallbacks 0 + Whole channel: 135 different films; any film again within 3 days 0, within 7 days 161 + On another channel at the same time: 0; gaps 0 + Fixed-order fallbacks by hour: none + Most-screened by day: Them! 6, Mars Attacks! 6, The Vanishing 6, The Adventures of Buckaroo Banzai Across the 8th Dimension 5, The Blob 5 +