|
|
|
@@ -34,7 +34,14 @@ internal data class ChannelProgramming(val number: Int, val name: String, val se
|
|
|
|
|
/** Films a series channel may carry, each only in an event that names it. Empty everywhere but Ring X. */
|
|
|
|
|
val channelFilms: List<String> = emptyList(),
|
|
|
|
|
/** Series a film channel may carry, each only in an event that names it, one whole episode a night in order. Empty everywhere but After Dark. */
|
|
|
|
|
val filmSeries: List<String> = emptyList()) {
|
|
|
|
|
val filmSeries: List<String> = emptyList(),
|
|
|
|
|
/**
|
|
|
|
|
* Editorially weighted rotation (see [extendWeighted]), switched on per channel: each show's weight from its tier in
|
|
|
|
|
* the channel's brief. Null keeps equal turns. Only Orbit Three has it (approved 4 November 2026).
|
|
|
|
|
*/
|
|
|
|
|
val tierWeights: Map<String, Double>? = null) {
|
|
|
|
|
/** A show's weight here, by its tier; a show the brief doesn't name counts as supporting. */
|
|
|
|
|
fun weightOf(show: PoolSeries) = tierWeights?.get(seriesKey(show.title)) ?: TIER_SUPPORTING
|
|
|
|
|
/** Whether a show plays its episodes in a mixed order here. */
|
|
|
|
|
fun mixes(show: PoolSeries) = shuffleEpisodes || seriesKey(show.title) in shuffleTitles.mapTo(HashSet(), ::seriesKey)
|
|
|
|
|
/** Changes when the list does, so the episode cache is read again. What's already committed stays. */
|
|
|
|
@@ -89,7 +96,10 @@ internal val SmartChannels = listOf(
|
|
|
|
|
ChannelPresets.filter { it.smartOnly }.map { ChannelProgramming(it.number, it.name, it.searches, seriesCount = if (it.films) it.searches.size else 60, finishTogether = !it.films, exactTitles = true,
|
|
|
|
|
filmRestMs = if (it.films) 7 * 24 * 60 * 60_000L else null,
|
|
|
|
|
suggestionRules = SmartChannelRules[it.number].orEmpty(), dayParts = SmartChannelParts[it.number].orEmpty(), shuffleTitles = SmartChannelMixed[it.number].orEmpty(),
|
|
|
|
|
channelFilms = if (it.number == 401) PpvSunday else emptyList()) }
|
|
|
|
|
channelFilms = if (it.number == 401) PpvSunday else emptyList(), tierWeights = SmartChannelTiers[it.number]) }
|
|
|
|
|
|
|
|
|
|
/** Editorially weighted rotation, per channel, once its tiers are approved. Orbit Three only for now (4 November 2026). */
|
|
|
|
|
private val SmartChannelTiers: Map<Int, Map<String, Double>> get() = mapOf(103 to ThreeTiers)
|
|
|
|
|
|
|
|
|
|
/** Shows that play in a mixed order on channels whose identity is agreed. See ChannelIdentities.kt. */
|
|
|
|
|
private val SmartChannelMixed: Map<Int, Set<String>> get() = mapOf(101 to Orbit1Mixed, 102 to Orbit2Mixed, 103 to ThreeMixed, 104 to GoldMixed, 105 to RiotMixed,
|
|
|
|
@@ -134,7 +144,11 @@ internal data class ScheduleCursor(val lastSeriesId: String? = null, val marks:
|
|
|
|
|
/** When each film last started, for its rest period. */
|
|
|
|
|
val filmShown: Map<String, Long> = emptyMap(),
|
|
|
|
|
/** The last show to air in each block, so a block picks up its rotation where it left off rather than starting again each day. */
|
|
|
|
|
val blockLast: Map<String, String> = emptyMap())
|
|
|
|
|
val blockLast: Map<String, String> = emptyMap(),
|
|
|
|
|
/** Weighted rotation only: each block's tally per show, the minutes it has aired there divided by its tier's weight. */
|
|
|
|
|
val tallies: Map<String, Map<String, Double>> = emptyMap(),
|
|
|
|
|
/** Weighted rotation only: when each show last started its run from the top, so a short series doesn't restart too often. */
|
|
|
|
|
val runStarted: Map<String, Long> = emptyMap())
|
|
|
|
|
|
|
|
|
|
internal data class ScheduleEntry(val seriesId: String, val mark: EpisodeMark, val media: ScheduledMedia, val startMs: Long,
|
|
|
|
|
/** Why the planner chose this, for diagnostics only. Empty for programmes planned before reasons were kept. */
|
|
|
|
@@ -292,6 +306,85 @@ private const val ON_TIME_RUN_IN_MS = 90 * 60_000L
|
|
|
|
|
/** A series on a film channel airs at most once in this long, so a long event window still holds one episode. */
|
|
|
|
|
private const val SERIES_NIGHT_MS = 12 * 60 * 60_000L
|
|
|
|
|
|
|
|
|
|
/** The tiers' weights: a core show gets one and a half times a supporting show's airtime in a block, a surprise half. */
|
|
|
|
|
internal const val TIER_CORE = 3.0
|
|
|
|
|
internal const val TIER_SUPPORTING = 2.0
|
|
|
|
|
internal const val TIER_SURPRISE = 1.0
|
|
|
|
|
/** A series doesn't start its run from the top again within this long of last doing so, while others can play. */
|
|
|
|
|
internal const val RUN_REST_MS = 3 * 24 * 60 * 60_000L
|
|
|
|
|
/** The same file never airs again within this long while another show could play. A minimum; other protections still apply. */
|
|
|
|
|
internal const val SAME_FILE_MS = 24 * 60 * 60_000L
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Editorially weighted rotation (Orbit Three first, 4 November 2026). Each block keeps a tally per show: the minutes it
|
|
|
|
|
* has aired in that block divided by its tier's weight. The eligible show with the lowest tally goes next, so airtime
|
|
|
|
|
* follows the tiers, not episode counts or runtimes, and no eligible show can be passed over for long. A show new to a
|
|
|
|
|
* block starts level with the block's lowest tally, so it neither floods in nor waits. Nobody waits for anyone else to
|
|
|
|
|
* finish: each show follows its own cursor, in order or through its fixed shuffled deck, exactly as before; this only
|
|
|
|
|
* decides how often a show is chosen, never which episode it plays. Eligibility, relaxed in this order only when nothing
|
|
|
|
|
* else is left: not the show that just played; not a file aired within [SAME_FILE_MS]; not a series starting its run again
|
|
|
|
|
* within [RUN_REST_MS] of last starting it; and never a show airing on another channel at that moment.
|
|
|
|
|
*/
|
|
|
|
|
internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, weightOf: (PoolSeries) -> Double,
|
|
|
|
|
dayParts: List<DayPart>, zone: java.time.ZoneId, avoid: ProgrammeClash, shuffle: Boolean, mixed: (PoolSeries) -> Boolean,
|
|
|
|
|
/** Files aired shortly before [fromMs], for the same-file guard across a top-up. */
|
|
|
|
|
recentFiles: Map<String, Long> = emptyMap()): ChannelPlan {
|
|
|
|
|
val marks = cursor.marks.toMutableMap()
|
|
|
|
|
val shown = cursor.filmShown.toMutableMap()
|
|
|
|
|
val tallies = cursor.tallies.mapValues { it.value.toMutableMap() }.toMutableMap()
|
|
|
|
|
val runStarted = cursor.runStarted.toMutableMap()
|
|
|
|
|
val aired = recentFiles.toMutableMap()
|
|
|
|
|
val mixes = HashMap<String, List<PoolEpisode>>()
|
|
|
|
|
val entries = mutableListOf<ScheduleEntry>()
|
|
|
|
|
var last = cursor.lastSeriesId
|
|
|
|
|
var start = fromMs
|
|
|
|
|
fun isMixed(show: PoolSeries) = shuffle || mixed(show)
|
|
|
|
|
fun running(show: PoolSeries) = if (isMixed(show)) mixes.getOrPut(show.id) { shuffledEpisodes(show) } else show.episodes
|
|
|
|
|
fun following(show: PoolSeries): PoolEpisode {
|
|
|
|
|
if (!isMixed(show)) return nextEpisode(show, marks[show.id])
|
|
|
|
|
val mix = running(show)
|
|
|
|
|
return mix[(mix.indexOfFirst { it.mark == marks[show.id] } + 1) % mix.size]
|
|
|
|
|
}
|
|
|
|
|
fun hadItsTurn(available: List<PoolSeries>, at: Long) = available.any { film -> film.isFilm && shown[film.id]?.let { at - it < ONCE_A_NIGHT_MS } == true }
|
|
|
|
|
fun partAt(at: Long): Triple<List<PoolSeries>, String, DayPart?> {
|
|
|
|
|
val first = dayPartFor(pool, dayParts, at, zone)
|
|
|
|
|
val part = first.third ?: return first
|
|
|
|
|
return if (part.once && hadItsTurn(first.first, at)) dayPartFor(pool, dayParts - part, at, zone) else first
|
|
|
|
|
}
|
|
|
|
|
// A show that has left the pool loses its tally, so if it comes back it joins level rather than owed a backlog.
|
|
|
|
|
val ids = pool.mapTo(HashSet()) { it.id }
|
|
|
|
|
tallies.values.forEach { block -> block.keys.retainAll(ids) }
|
|
|
|
|
while (start < untilMs) {
|
|
|
|
|
val (available, block, part) = partAt(start)
|
|
|
|
|
val tally = tallies.getOrPut(block) { HashMap() }
|
|
|
|
|
// A show new to the block, or back in it, joins level with the block's lowest, not at nothing.
|
|
|
|
|
val floor = available.mapNotNull { tally[it.id] }.minOrNull() ?: 0.0
|
|
|
|
|
available.forEach { tally.putIfAbsent(it.id, floor) }
|
|
|
|
|
val clear = available.filter { !avoid(it, start, start + following(it).durationMs) }.ifEmpty { available }
|
|
|
|
|
val rules = listOf<(PoolSeries) -> Boolean>(
|
|
|
|
|
{ it.id != last }, { aired[following(it).id]?.let { at -> start - at < SAME_FILE_MS } != true }, { following(it).mark != running(it).first().mark || runStarted[it.id]?.let { at -> start - at < RUN_REST_MS } != true })
|
|
|
|
|
// Drop the softest rules first, only as far as needed to leave a choice.
|
|
|
|
|
val eligible = (rules.size downTo 0).asSequence().map { kept -> clear.filter { show -> rules.take(kept).all { it(show) } } }.first { it.isNotEmpty() }
|
|
|
|
|
// A show held back by the 24-hour or restart rule is brought up level, so it comes back to its share without a burst.
|
|
|
|
|
val low = eligible.minOf { tally[it.id]!! }
|
|
|
|
|
clear.filter { it.id != last && it !in eligible }.forEach { tally[it.id] = maxOf(tally[it.id]!!, low) }
|
|
|
|
|
val series = eligible.minWith(compareBy<PoolSeries> { tally[it.id]!! }.thenBy { pool.indexOf(it) })
|
|
|
|
|
val episode = following(series)
|
|
|
|
|
val why = "$block; weighted: lowest tally for its tier (${"%.0f".format(tally[series.id])})" + if (eligible.size < clear.size) ", others resting or just played" else ""
|
|
|
|
|
val item = if (series.isFilm) SampleItem(series.title, "Movie", episode.durationMs / 60_000, "", episode.id, episode.durationMs) else scheduledItem(series.title, episode)
|
|
|
|
|
entries += ScheduleEntry(series.id, episode.mark, ScheduledMedia(item, episode.sourceId, episode.durationMs), start, why, if (series in available) part?.key.orEmpty() else "")
|
|
|
|
|
tally[series.id] = tally[series.id]!! + episode.durationMs / 60_000.0 / weightOf(series)
|
|
|
|
|
marks[series.id] = episode.mark
|
|
|
|
|
aired[episode.id] = start
|
|
|
|
|
// The start of a run: its first episode in season order, or the first of its shuffled deck.
|
|
|
|
|
if (episode.mark == running(series).first().mark) runStarted[series.id] = start
|
|
|
|
|
if (series.isFilm) shown[series.id] = start
|
|
|
|
|
last = series.id
|
|
|
|
|
start += episode.durationMs
|
|
|
|
|
}
|
|
|
|
|
return ChannelPlan(entries, ScheduleCursor(last, marks, shown, cursor.blockLast, tallies, runStarted))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Shows take turns, one episode each, back to back from [fromMs] until [untilMs] is covered. */
|
|
|
|
|
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
|
|
|
|
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false,
|
|
|
|
@@ -423,7 +516,7 @@ internal fun airedCursor(base: ScheduleCursor, entries: List<ScheduleEntry>, now
|
|
|
|
|
marks[it.seriesId] = it.mark; last = it.seriesId
|
|
|
|
|
if (it.media.item.type == "Movie") shown[it.seriesId] = it.startMs
|
|
|
|
|
}
|
|
|
|
|
return ScheduleCursor(last, marks, shown, base.blockLast)
|
|
|
|
|
return ScheduleCursor(last, marks, shown, base.blockLast, base.tallies, base.runStarted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -432,13 +525,18 @@ internal fun airedCursor(base: ScheduleCursor, entries: List<ScheduleEntry>, now
|
|
|
|
|
*/
|
|
|
|
|
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
|
|
|
|
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault(), avoid: ProgrammeClash = { _, _, _ -> false }, shuffle: Boolean = false, noThirdInARow: Boolean = true,
|
|
|
|
|
mixed: (PoolSeries) -> Boolean = { false }, blockRotation: Boolean = true): ChannelPlan? {
|
|
|
|
|
mixed: (PoolSeries) -> Boolean = { false }, blockRotation: Boolean = true,
|
|
|
|
|
/** Set for a channel with editorially weighted rotation ([ChannelProgramming.weightOf]); null keeps equal turns. */
|
|
|
|
|
weightOf: ((PoolSeries) -> Double)? = null): 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 (pool.isNullOrEmpty()) return kept
|
|
|
|
|
val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs
|
|
|
|
|
val trailing = kept?.entries?.lastOrNull()?.let { tail -> kept.entries.asReversed().takeWhile { it.seriesId == tail.seriesId }.size } ?: 0
|
|
|
|
|
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone, avoid, shuffle, mixed, trailing, noThirdInARow, blockRotation)
|
|
|
|
|
val added = if (weightOf != null && filmRestMs == null) {
|
|
|
|
|
val recent = kept?.entries.orEmpty().filter { it.startMs >= from - SAME_FILE_MS }.associate { it.media.item.id to it.startMs }
|
|
|
|
|
extendWeighted(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, weightOf, dayParts, zone, avoid, shuffle, mixed, recent)
|
|
|
|
|
} else extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone, avoid, shuffle, mixed, trailing, noThirdInARow, blockRotation)
|
|
|
|
|
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|