diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 750ec0d8..c0adeccf 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -3,7 +3,7 @@ plugins { id("org.jetbrains.kotlin.plugin.compose") } /** The release's build number: versionCode is this x 10 plus the edition's channel. */ -val ORBIT_BUILD = 86 +val ORBIT_BUILD = 87 android { namespace = "tv.orbit.classic" compileSdk = 36 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 475800a8..0a5e8060 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt @@ -456,6 +456,27 @@ private const val CHANNEL_POOL_VERSION = 5 internal const val SCHEDULE_AHEAD_MS = 7 * 24 * 60 * 60_000L internal const val SCHEDULE_TOP_UP_MS = 6 * 24 * 60 * 60_000L +/** + * A channel still on its first read of its list books only this far ahead (Paul, 1 December 2026: Horror TV went on air with 9 + * of its 68 shows and booked a whole week from them). Each round adds a little more from whatever has been read by then, so + * the full list is on air within hours, not a week later; once the list is read, the week ahead is booked as usual. + */ +internal const val FILLING_AHEAD_MS = 6 * 60 * 60_000L +internal const val FILLING_TOP_UP_MS = 4 * 60 * 60_000L +/** While filling, programmes starting within this of now are kept; anything later booked before is planned again. */ +internal const val FILLING_KEEP_MS = 60 * 60_000L + +/** + * A plan booked further ahead than a filling channel books ([FILLING_AHEAD_MS]), as a channel's first plan was before 1 December + * 2026: programmes starting more than [FILLING_KEEP_MS] from now are dropped (none of them has aired), and the place is taken + * from what's kept, so planning carries on in order with no repeats. + */ +internal fun trimmedForFilling(plan: ChannelPlan, aired: ScheduleCursor, nowMs: Long): ChannelPlan { + val cut = nowMs + FILLING_KEEP_MS + if (plan.entries.none { it.startMs > cut }) return plan + val kept = plan.entries.filter { it.startMs <= cut } + return ChannelPlan(kept, airedCursor(aired, kept, Long.MAX_VALUE)) +} /** Two days for Catch Up ([CATCH_UP_MS]) and an hour over, so the oldest programme in it is never cut in half. */ internal const val SCHEDULE_KEEP_PAST_MS = 49 * 60 * 60_000L @@ -1330,14 +1351,16 @@ internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List /** Nothing is booked to start from here on: a seasonal channel's end ([seasonEndMs]). */ seasonEndMs: Long? = null, /** Film channels only: [NETWORK_FILM_REST_MS]. */ - network: NetworkRest = NoNetworkRest): ChannelPlan? { + network: NetworkRest = NoNetworkRest, + /** How far ahead to book, and when to add more: shorter for a channel still on its first read ([FILLING_AHEAD_MS]). */ + aheadMs: Long = SCHEDULE_AHEAD_MS, topUpMs: Long = SCHEDULE_TOP_UP_MS): ChannelPlan? { val kept = existing?.let { plan -> plan.copy(entries = plan.entries.dropWhile { it.endMs < nowMs - SCHEDULE_KEEP_PAST_MS }) } - if (kept != null && kept.endMs >= nowMs + SCHEDULE_TOP_UP_MS) return kept + if (kept != null && kept.endMs >= nowMs + topUpMs) return kept if (pool.isNullOrEmpty()) return kept @Suppress("NAME_SHADOWING") val dayParts = resolveRotations(dayParts, pool, nowMs, zone) require((weightOf == null && filmRestMs == null) || dayParts.none { it.offAir || it.seasonCap > 0 }) { "Off-air blocks and season rotations are for plain series channels." } val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs - val untilMs = minOf(nowMs + SCHEDULE_AHEAD_MS, seasonEndMs ?: Long.MAX_VALUE) + val untilMs = minOf(nowMs + aheadMs, seasonEndMs ?: Long.MAX_VALUE) if (from >= untilMs) return kept ?: ChannelPlan(emptyList(), existing?.tail ?: aired) val trailing = kept?.entries?.lastOrNull()?.let { tail -> kept.entries.asReversed().takeWhile { it.seriesId == tail.seriesId }.size } ?: 0 val added = if (weightOf != null && filmRestMs == null) { diff --git a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt index c32f4e2c..f4e71613 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelScheduler.kt @@ -623,15 +623,19 @@ internal class ChannelScheduler(private val store: ScheduleStore, val network: NetworkRest = if (programming.filmRestMs == null) NoNetworkRest else { film, at -> restingElsewhere(film, at) } // Under the plan lock, so a programme being re-timed for its real length (PlanEdits) is never written over. val (existing, plan) = synchronized(PlanEdits.lock) { - val existing = store.loadPlan(userId, number) - base = placeToPlanFrom(existing, progress, now) + base = placeToPlanFrom(store.loadPlan(userId, number), progress, now) + // Still on its first read: a few hours at a time, so the shows read next join within hours, not a week later. + val filling = pool?.signature?.isEmpty() == true + val existing = store.loadPlan(userId, number)?.let { if (filling) trimmedForFilling(it, progress?.aired ?: ScheduleCursor(), now) else it } + if (filling) Log.i("OrbitSchedule", "channel=$number filling plannedUntil=${existing?.endMs?.let { (it - now) / 60_000 }}min") val plan = topUpPlan(existing, base, pool?.series?.let { withOpenApprovals(withoutOddRuntimes(withoutFailed(withinRuns(it, programming.episodeRuns), store.loadFailed(userId)), number, store.runtimeReleases().load()), choices) }?.let { if (programming.mixedTurns) mixedTurnOrder(it, number) else it }, now, programming.filmRestMs, programming.finishTogether, programming.dayParts, // SYNDICATE, a series channel that books films every day, also rests a film another channel shows within five days // (25 November 2026), as the film channels do: a preference, given up only when nothing else fits, like a clash. avoid = { show, startMs, endMs -> others(show, startMs, endMs) || ((number == SYNDICATE_NUMBER || number == WONDER_TV_NUMBER || number == HEROES_LIVE_NUMBER) && show.isFilm && restingElsewhere(show, startMs)) }, shuffle = programming.shuffleEpisodes, mixed = programming::mixes, weightOf = programming.tierWeights?.let { programming::weightOf }, adaptiveRest = programming.adaptiveFilmRest, fileRestMs = programming.fileRestMs, stories = programming.storyGrouping, shorts = programming.shorts, marathon = programming.marathon, - seasonEndMs = seasonEndMs(number, now), network = network) ?: throw EmbyCheckException("${programming.name} has no episodes to schedule.") + seasonEndMs = seasonEndMs(number, now), network = network, + aheadMs = if (filling) FILLING_AHEAD_MS else SCHEDULE_AHEAD_MS, topUpMs = if (filling) FILLING_TOP_UP_MS else SCHEDULE_TOP_UP_MS) ?: throw EmbyCheckException("${programming.name} has no episodes to schedule.") // Franchise films booked side by side go in release order, among the new bookings only (Continuity.kt). val ordered = if (plan === existing) plan else runCatching { val from = plan.entries.indexOfFirst { it.startMs >= (existing?.endMs ?: Long.MIN_VALUE) }.takeIf { it >= 0 } ?: plan.entries.size diff --git a/android/app/src/test/java/tv/orbit/classic/FirstReadFillingTest.kt b/android/app/src/test/java/tv/orbit/classic/FirstReadFillingTest.kt new file mode 100644 index 00000000..d508845d --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/FirstReadFillingTest.kt @@ -0,0 +1,67 @@ +package tv.orbit.classic + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.time.LocalDate +import java.time.ZoneId + +/** + * A new channel on its first read (Paul, 1 December 2026: "so it doesn't just run a small pool for myself or new users"): + * it books only hours ahead while its list is read, so shows read later join within hours; a week booked from a few shows + * before this change is cut back to the next hour and planned again, in order, with nothing repeated. + */ +class FirstReadFillingTest { + private val zone = ZoneId.systemDefault() + private val minute = 60_000L + private val hour = 60 * minute + private fun serial(name: String, n: Int, minutes: Int = 44) = PoolSeries("s-$name", name, (1..n).map { e -> PoolEpisode("$name-$e", "m", EpisodeMark(1, e), "$name / S1 E$e", minutes * minute) }) + private val all = (1..12).map { serial("Show$it", 30) } + private fun start() = LocalDate.of(2026, 12, 1).atTime(1, 9).atZone(zone).toInstant().toEpochMilli() + private fun inOrder(entries: List, pool: List) = entries.groupBy { it.seriesId }.all { (id, l) -> + val eps = pool.first { it.id == id }.episodes.map { it.mark } + l.zipWithNext().all { (a, b) -> eps[(eps.indexOf(a.mark) + 1) % eps.size] == b.mark } + } + + @Test fun whileFillingItBooksHoursNotAWeekAndLaterShowsJoinSoon() { + var plan: ChannelPlan? = null; var aired = ScheduleCursor(); val seen = mutableMapOf() + // Rounds every hour; the list is read three shows a round, as the background read does a fair share at a time. + for (round in 0 until 8) { + val now = start() + round * hour + val read = all.take(minOf(all.size, 3 + 3 * round)) + plan?.let { aired = airedCursor(aired, it.entries, now) } + plan = topUpPlan(plan, aired, read, now, aheadMs = FILLING_AHEAD_MS, topUpMs = FILLING_TOP_UP_MS)!! + assertTrue("never more than six hours ahead", plan.endMs <= now + FILLING_AHEAD_MS + hour) + plan.entries.forEach { e -> seen.putIfAbsent(e.startMs, e)?.let { assertEquals("bookings kept", it, e) } } + } + val booked = seen.values.sortedBy { it.startMs } + // Shows read in the fourth round are on air within a few hours of being read, not a week later. + val firstOfShow10 = booked.first { it.seriesId == "s-Show10" }.startMs + assertTrue(firstOfShow10 < start() + 12 * hour) + assertTrue(inOrder(booked, all)) + } + + @Test fun aWeekBookedFromAFewShowsIsCutBackAndPlannedAgainInOrder() { + val few = all.take(3) + val week = topUpPlan(null, ScheduleCursor(), few, start())!! + assertTrue(week.endMs > start() + 6 * 24 * hour) + val now = start() + 75 * minute + val aired = airedCursor(ScheduleCursor(), week.entries, now) + val trimmed = trimmedForFilling(week, aired, now) + assertTrue(trimmed.entries.all { it.startMs <= now + FILLING_KEEP_MS }) + assertEquals("what's on now and next is kept", week.entries.takeWhile { it.startMs <= now + FILLING_KEEP_MS }, trimmed.entries) + val more = topUpPlan(trimmed, aired, all, now, aheadMs = FILLING_AHEAD_MS, topUpMs = FILLING_TOP_UP_MS)!! + assertEquals(trimmed.entries, more.entries.take(trimmed.entries.size)) + assertTrue("shows read since join", more.entries.any { it.seriesId !in few.map { s -> s.id } }) + assertTrue("nothing repeated or skipped", inOrder(more.entries, all)) + // Once the whole list is read, it books the week again. + val full = topUpPlan(more, aired, all, now)!! + assertTrue(full.endMs > now + 6 * 24 * hour) + assertTrue(inOrder(full.entries, all)) + } + + @Test fun aPlanWithinTheFillingWindowIsLeftAlone() { + val plan = topUpPlan(null, ScheduleCursor(), all, start(), aheadMs = FILLING_AHEAD_MS)!! + assertTrue(trimmedForFilling(plan, ScheduleCursor(), start() + 6 * hour) === plan) + } +} diff --git a/docs/CHANGELOG-build87.md b/docs/CHANGELOG-build87.md new file mode 100644 index 00000000..21acb8a8 --- /dev/null +++ b/docs/CHANGELOG-build87.md @@ -0,0 +1,14 @@ +# Build 87 (version code 870): change log + +Build 86 plus the change below, on `horror-tv`. Nothing here changes a channel's home, ratings rules or dayparts. + +## A new channel no longer books a week from its first few shows (Paul, 1 December 2026) + +- A channel goes on air after reading only part of its list (Horror TV read 9 of its 68 shows, Wonder TV 7, Heroes Live 10), + and it used to book its whole week ahead from those. Now, while a channel's first read is still going, it books only six + hours ahead and adds more each round from whatever has been read by then, so the rest of its list is on air within hours. + Once the list is read, it books the week ahead as before. This is the same for every household and every new channel. +- A week already booked from a first read (Horror TV, Wonder TV and Heroes Live downstairs) is cut back to what starts + within the next hour, and planned again from there in order: nothing that has aired is repeated, and nothing is skipped. + Channels that have finished reading their list are untouched. +- Tests: FirstReadFillingTest.