Smart setup: the evening rotation from the plan's own time, balanced by running time (0.110.15-smartsetup, versionCode 298)
The weekly evening and late rotation read the real clock (rotationWeek() in programmingOf), so plans depended on the day they were made, and SmartSetupRegressionTest failed from Monday 9 November. And the weekly sets were split by count, so one of Orbit Sakura's weeks was ten short films: 30 repeats from day 2.5 (from 16 November), against 15 from day 6. Shaped channels now hand the planner the whole rotating list; topUpPlan resolves it (resolveRotations) from its own time, balancing the sets by the pool's running times (rotationSet), deterministically. Every Sakura week is now 15-17 repeats, none before day 6. Committed plans untouched. New tests plan every rotation week for both fixture channels; the regression limit is 18 a week (original run: 34). Docs: docs/smartsetup-rotation-fix.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,7 +29,15 @@ internal data class DayPart(val fromHour: Int, val toHour: Int, val titles: List
|
||||
/** An appointment's own allowance for starting late, instead of [ON_TIME_GRACE_MS]. */
|
||||
val graceMs: Long? = null,
|
||||
/** On a film channel, a series event's episodes a night (a strand), and whether they go round a mixed deck. */
|
||||
val run: Int = 1, val shuffled: Boolean = false) {
|
||||
val run: Int = 1, val shuffled: Boolean = false,
|
||||
/**
|
||||
* A weekly rotation (smart setup's shaped evenings and late nights, 9 November 2026): these of [titles] take turns, at
|
||||
* most [rotateCap] a week, the rest always play. The planner picks the week's set itself ([resolveRotations]) from the plan's
|
||||
* own time and the shows' real running times, so plans never depend on the clock and no week runs thin.
|
||||
*/
|
||||
val rotating: List<String>? = null, val rotateCap: Int = 0,
|
||||
/** How many hours a week the rotation's own slot runs, so a long show counts only for what a week can hold. */
|
||||
val rotateWeekHours: Int = 0) {
|
||||
fun covers(hour: Int) = if (fromHour <= toHour) hour in fromHour until toHour else hour >= fromHour || hour < toHour
|
||||
/** An event that runs past midnight belongs to the night it started, so its small hours count from the day before. */
|
||||
fun covers(hour: Int, day: java.time.DayOfWeek) = covers(hour) && (days.isEmpty() || (if (fromHour > toHour && hour < toHour) day.minus(1) else day) in days)
|
||||
@@ -1047,6 +1055,7 @@ internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List
|
||||
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 (pool.isNullOrEmpty()) return kept
|
||||
@Suppress("NAME_SHADOWING") val dayParts = resolveRotations(dayParts, pool, nowMs, zone)
|
||||
val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs
|
||||
val untilMs = minOf(nowMs + SCHEDULE_AHEAD_MS, seasonEndMs ?: Long.MAX_VALUE)
|
||||
if (from >= untilMs) return kept ?: ChannelPlan(emptyList(), existing?.tail ?: aired)
|
||||
@@ -1058,6 +1067,49 @@ internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List
|
||||
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
|
||||
}
|
||||
|
||||
/**
|
||||
* A weekly rotation's set for the week of [nowMs] (Mondays turn it over): the [DayPart.rotating] titles split into weeks
|
||||
* ([DayPart.rotateCap] or fewer each) balanced by running time, then this week's taken. Longest first into the lightest week,
|
||||
* each counting at most the hours its slot runs in a week, so a week of short films isn't left beside a week of long series
|
||||
* (Orbit Sakura, 9 November 2026: one week had 14 hours of evening shows, the next 116). Deterministic: the same titles, pool
|
||||
* and week always give the same set.
|
||||
*/
|
||||
internal fun resolveRotations(parts: List<DayPart>, pool: List<PoolSeries>, nowMs: Long, zone: java.time.ZoneId): List<DayPart> {
|
||||
if (parts.none { it.rotating != null }) return parts
|
||||
val week = rotationWeek(nowMs, zone)
|
||||
val runtime = HashMap<String, Long>()
|
||||
for (s in pool) {
|
||||
val run = s.episodes.sumOf { it.durationMs }
|
||||
val key = seriesKey(s.title)
|
||||
runtime[key] = (runtime[key] ?: 0L) + run
|
||||
if (s.isFilm) { val f = seriesKey(FilmIdentity.of(s.title).title); if (f != key) runtime[f] = (runtime[f] ?: 0L) + run }
|
||||
}
|
||||
return parts.map { part ->
|
||||
val rotating = part.rotating ?: return@map part
|
||||
val set = rotationSet(rotating, part.rotateCap, week, part.rotateWeekHours * 3_600_000L) { runtime[seriesKey(it)] ?: runtime[seriesKey(FilmIdentity.of(it).title)] ?: 0L }
|
||||
val fixed = part.titles.filter { it !in rotating }
|
||||
part.copy(titles = fixed + set, rotating = null)
|
||||
}
|
||||
}
|
||||
|
||||
/** The week's set of [titles] as [resolveRotations] chooses it. All of them when they fit in one week. */
|
||||
internal fun rotationSet(titles: List<String>, cap: Int, week: Long, weekMs: Long, runtimeOf: (String) -> Long): List<String> {
|
||||
if (titles.size <= cap) return titles
|
||||
val pages = (titles.size + cap - 1) / cap
|
||||
val per = (titles.size + pages - 1) / pages
|
||||
val value = titles.associateWith { minOf(runtimeOf(it), if (weekMs > 0) weekMs else Long.MAX_VALUE) }
|
||||
val order = titles.sortedWith(compareByDescending<String> { value.getValue(it) }.thenBy { seriesKey(it) })
|
||||
val load = LongArray(pages); val count = IntArray(pages)
|
||||
val pageOf = HashMap<String, Int>()
|
||||
for (t in order) {
|
||||
var best = -1
|
||||
for (p in 0 until pages) if (count[p] < per && (best < 0 || load[p] < load[best])) best = p
|
||||
pageOf[t] = best; load[best] += value.getValue(t); count[best]++
|
||||
}
|
||||
val k = Math.floorMod(week, pages.toLong()).toInt()
|
||||
return titles.filter { pageOf[it] == k }.sortedBy { seriesKey(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
* A channel that reads committed programmes, and falls back to its existing loop outside them.
|
||||
* The plan is swapped inside one object, so a top-up never looks like a new channel and never interrupts playback.
|
||||
|
||||
@@ -105,13 +105,12 @@ internal object CustomChannels {
|
||||
/** Roughly how long a first plan takes, from what it has to read: each series' episodes, a quick look for each film. */
|
||||
fun planMinutes(series: Int, films: Int) = ((series * 9 + films) / 60.0).let { kotlin.math.ceil(it).toInt() }.coerceAtLeast(1)
|
||||
|
||||
@Volatile private var cachedWeek = -1L
|
||||
@Volatile private var cached: Pair<List<CustomChannel>, List<ChannelProgramming>> = emptyList<CustomChannel>() to emptyList()
|
||||
/** Every channel of your own as the planner sees it. */
|
||||
val programmings: List<ChannelProgramming> get() {
|
||||
val now = list; val week = rotationWeek()
|
||||
// Remade when the list changes, and each week for the evening's rotation.
|
||||
if (cached.first !== now || cachedWeek != week) { cached = now to now.map(::programmingOf); cachedWeek = week }
|
||||
val now = list
|
||||
// Remade when the list changes. The week's evening rotation is chosen by the planner from each plan's own time.
|
||||
if (cached.first !== now) cached = now to now.map(::programmingOf)
|
||||
return cached.second
|
||||
}
|
||||
}
|
||||
@@ -127,7 +126,7 @@ internal fun programmingOf(channel: CustomChannel): ChannelProgramming = Channel
|
||||
// You chose these titles yourself, so they play in the sound they have: Sakura's anime is mostly Japanese with subtitles,
|
||||
// and asking for English sound left nearly all of it out (7 November 2026).
|
||||
englishAudio = false,
|
||||
dayParts = when (channel.style) { PlayStyle.Blocks -> timetableParts(channel.shows); PlayStyle.Shaped -> shapedParts(channel.slots, eveningPage = rotationWeek()); else -> emptyList() },
|
||||
dayParts = when (channel.style) { PlayStyle.Blocks -> timetableParts(channel.shows); PlayStyle.Shaped -> shapedParts(channel.slots); else -> emptyList() },
|
||||
storyGrouping = channel.style == PlayStyle.Standard || channel.style == PlayStyle.Shaped,
|
||||
shorts = if (channel.style == PlayStyle.Standard || channel.style == PlayStyle.Shaped) YoursShorts else null,
|
||||
tierWeights = if (channel.style == PlayStyle.Shaped && channel.core.isNotEmpty()) shapedWeights(channel.core) else null,
|
||||
|
||||
@@ -230,18 +230,33 @@ internal fun weekCheck(series: Int, films: Int): String? = when {
|
||||
* never the late ones, so a late title stays late (Paul, 7 November 2026: the creator's choices and the profile's rules win).
|
||||
* With nothing laid out, everything plays all day.
|
||||
*/
|
||||
internal fun shapedParts(slots: Map<String, Slot>, eveningPage: Long = 0): List<DayPart> {
|
||||
internal fun shapedParts(slots: Map<String, Slot>, eveningPage: Long? = null): List<DayPart> {
|
||||
if (slots.isEmpty()) return emptyList()
|
||||
val bySlot = Slot.entries.associateWith { s -> slots.filterValues { it == s }.keys.toList() }.toMutableMap()
|
||||
// Evenings are appointments (Paul, 7 November 2026: Sakura's evening had 85 shows, each on once a week): at most
|
||||
// EVENING_ROTATION shows a week, so each comes back most nights, and a different set the next week until all have had theirs.
|
||||
bySlot[Slot.Evening] = eveningRotation(bySlot.getValue(Slot.Evening), eveningPage)
|
||||
// Late nights the same way (Paul, 7 November 2026: Sakura's late had 52 shows), with a few more as the hours are longer.
|
||||
bySlot[Slot.Late] = eveningRotation(bySlot.getValue(Slot.Late), eveningPage, LATE_ROTATION)
|
||||
// With no [eveningPage] (as the app plans), the week's sets are left to the planner, which balances them by running time
|
||||
// and takes the week from the plan's own time ([resolveRotations]). With one, the old split by count (tests).
|
||||
val evening = bySlot.getValue(Slot.Evening); val late = bySlot.getValue(Slot.Late)
|
||||
val eveningTurns = evening.takeIf { eveningPage == null && it.size > EVENING_ROTATION }
|
||||
val lateTurns = late.takeIf { eveningPage == null && it.size > LATE_ROTATION }
|
||||
if (eveningPage != null) {
|
||||
bySlot[Slot.Evening] = eveningRotation(evening, eveningPage)
|
||||
bySlot[Slot.Late] = eveningRotation(late, eveningPage, LATE_ROTATION)
|
||||
}
|
||||
val gentle = bySlot.getValue(Slot.Daytime) + bySlot.getValue(Slot.Evening)
|
||||
fun hours(s: Slot) = (if (s.fromHour <= s.toHour) s.toHour - s.fromHour else 24 - s.fromHour + s.toHour) * 7
|
||||
return Slot.entries.mapNotNull { s ->
|
||||
val titles = bySlot.getValue(s).ifEmpty { if (s == Slot.Late) gentle + bySlot.getValue(Slot.Late) else gentle }
|
||||
titles.takeIf { it.isNotEmpty() }?.let { DayPart(s.fromHour, s.toHour, it) }
|
||||
val own = bySlot.getValue(s)
|
||||
val titles = own.ifEmpty { if (s == Slot.Late) gentle + bySlot.getValue(Slot.Late) else gentle }
|
||||
if (titles.isEmpty()) return@mapNotNull null
|
||||
// A part carrying a rotating list takes its turns: its own slot's, or the evening's when it borrows the evening.
|
||||
when {
|
||||
s == Slot.Late && lateTurns != null -> DayPart(s.fromHour, s.toHour, titles, rotating = lateTurns, rotateCap = LATE_ROTATION, rotateWeekHours = hours(Slot.Late))
|
||||
eveningTurns != null && titles.any { it in eveningTurns } -> DayPart(s.fromHour, s.toHour, titles, rotating = eveningTurns, rotateCap = EVENING_ROTATION, rotateWeekHours = hours(Slot.Evening))
|
||||
else -> DayPart(s.fromHour, s.toHour, titles)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +277,7 @@ internal fun eveningRotation(titles: List<String>, page: Long, cap: Int = EVENIN
|
||||
}
|
||||
|
||||
/** Which week it is for the evening's rotation: Mondays turn it over. */
|
||||
internal fun rotationWeek(nowMs: Long = System.currentTimeMillis(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): Long =
|
||||
internal fun rotationWeek(nowMs: Long, zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): Long =
|
||||
java.time.Instant.ofEpochMilli(nowMs).atZone(zone).toLocalDate().with(java.time.DayOfWeek.MONDAY).toEpochDay() / 7
|
||||
|
||||
/** "About 42 hours", for the week check: how long the picks run before anything repeats. */
|
||||
|
||||
Reference in New Issue
Block a user