|
|
|
@@ -3,7 +3,11 @@ package tv.orbit.classic
|
|
|
|
|
/** Local hours, wrapping past midnight, and the titles a channel may use then. No titles means everything in its pool. */
|
|
|
|
|
internal data class DayPart(val fromHour: Int, val toHour: Int, val titles: List<String> = emptyList(), val group: String = "",
|
|
|
|
|
/** Set for an event: only these days, starting from its [fromHour] on each. List it before the everyday blocks. */
|
|
|
|
|
val days: Set<java.time.DayOfWeek> = emptySet()) {
|
|
|
|
|
val days: Set<java.time.DayOfWeek> = emptySet(),
|
|
|
|
|
/** An appointment: the programmes before it are chosen, where one fits, to finish by its hour, so it starts on time. */
|
|
|
|
|
val onTime: Boolean = false,
|
|
|
|
|
/** One start a night: once a programme from this event has started, the rest of its window goes to the usual block. */
|
|
|
|
|
val once: Boolean = false) {
|
|
|
|
|
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)
|
|
|
|
@@ -25,12 +29,14 @@ internal data class ChannelProgramming(val number: Int, val name: String, val se
|
|
|
|
|
val shuffleEpisodes: Boolean = false,
|
|
|
|
|
/** Shows played in a mixed order on a channel that otherwise runs in season order, such as Paradox's standalone adventures. */
|
|
|
|
|
val shuffleTitles: Set<String> = emptySet(),
|
|
|
|
|
/** 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()) {
|
|
|
|
|
/** 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. */
|
|
|
|
|
val signature get() = "$CHANNEL_POOL_VERSION#${searches.joinToString("|")}#$seriesCount" + if (filmSeries.isEmpty()) "" else "#series:${filmSeries.joinToString("|")}"
|
|
|
|
|
val signature get() = "$CHANNEL_POOL_VERSION#${searches.joinToString("|")}#$seriesCount" + if (filmSeries.isEmpty()) "" else "#series:${filmSeries.joinToString("|")}" + if (channelFilms.isEmpty()) "" else "#films:${channelFilms.joinToString("|")}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Expect the unexpected: standalone adventures by day in a mixed order, cult drama in order in prime time, and the strange stuff late. */
|
|
|
|
@@ -79,7 +85,8 @@ internal val SmartChannels = listOf(
|
|
|
|
|
ChannelProgramming(106, "Paradox", ParadoxShows, seriesCount = 20, finishTogether = true, exactTitles = true, dayParts = ParadoxDayParts, shuffleTitles = ParadoxStandalones.toSet())) +
|
|
|
|
|
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()) }
|
|
|
|
|
suggestionRules = SmartChannelRules[it.number].orEmpty(), dayParts = SmartChannelParts[it.number].orEmpty(), shuffleTitles = SmartChannelMixed[it.number].orEmpty(),
|
|
|
|
|
channelFilms = if (it.number == 401) PpvSunday else emptyList()) }
|
|
|
|
|
|
|
|
|
|
/** 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,
|
|
|
|
@@ -159,20 +166,24 @@ internal fun dayPartPool(pool: List<PoolSeries>, parts: List<DayPart>, startMs:
|
|
|
|
|
dayPartChoice(pool, parts, startMs, zone).first
|
|
|
|
|
|
|
|
|
|
/** [dayPartPool], with a short name for the block it came from, for the reasons kept with each programme. */
|
|
|
|
|
internal fun dayPartChoice(pool: List<PoolSeries>, parts: List<DayPart>, startMs: Long, zone: java.time.ZoneId): Pair<List<PoolSeries>, String> {
|
|
|
|
|
internal fun dayPartChoice(pool: List<PoolSeries>, parts: List<DayPart>, startMs: Long, zone: java.time.ZoneId): Pair<List<PoolSeries>, String> =
|
|
|
|
|
dayPartFor(pool, parts, startMs, zone).let { it.first to it.second }
|
|
|
|
|
|
|
|
|
|
/** [dayPartChoice], with the part it came from, or null for the whole pool. */
|
|
|
|
|
internal fun dayPartFor(pool: List<PoolSeries>, parts: List<DayPart>, startMs: Long, zone: java.time.ZoneId): Triple<List<PoolSeries>, String, DayPart?> {
|
|
|
|
|
val local = java.time.Instant.ofEpochMilli(startMs).atZone(zone)
|
|
|
|
|
// An event with nothing to show gives way to the ordinary block for that time.
|
|
|
|
|
for (part in parts.filter { it.covers(local.hour, local.dayOfWeek) }) {
|
|
|
|
|
val name = (if (part.days.isNotEmpty()) "event " else "block ") + part.group.ifEmpty { "${part.fromHour}-${part.toHour}" }
|
|
|
|
|
if (part.titles.isEmpty() && part.group.isEmpty()) return pool to name
|
|
|
|
|
if (part.titles.isEmpty() && part.group.isEmpty()) return Triple(pool, name, part)
|
|
|
|
|
val titles = part.titles.map(::seriesKey).toSet()
|
|
|
|
|
// A film is named with its year, as "Halloween (1978)"; the pool already holds that exact film under its plain title.
|
|
|
|
|
val films = part.titles.map { seriesKey(FilmIdentity.of(it).title) }.toSet()
|
|
|
|
|
val allowed = pool.filter { seriesKey(it.title) in titles || (it.isFilm && seriesKey(it.title) in films) || (part.group.isNotEmpty() && it.group == part.group) }
|
|
|
|
|
if (allowed.isNotEmpty()) return allowed to name
|
|
|
|
|
if (allowed.isNotEmpty()) return Triple(allowed, name, part)
|
|
|
|
|
}
|
|
|
|
|
// A film saved for an event doesn't appear in everyday programming.
|
|
|
|
|
return pool.filter { !it.isFilm || pool.all(PoolSeries::isFilm) }.ifEmpty { pool } to (if (parts.isEmpty()) "whole pool" else "no block, whole pool")
|
|
|
|
|
return Triple(pool.filter { !it.isFilm || pool.all(PoolSeries::isFilm) }.ifEmpty { pool }, if (parts.isEmpty()) "whole pool" else "no block, whole pool", null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A few names and how many more, so a reason stays short. */
|
|
|
|
@@ -262,6 +273,16 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
|
|
|
|
|
return ChannelPlan(entries, ScheduleCursor(last, marks, shown))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 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. */
|
|
|
|
|
private const val ON_TIME_LOOKAHEAD_HOURS = 4
|
|
|
|
|
private const val ON_TIME_GRACE_MS = 20 * 60_000L
|
|
|
|
|
/** An appointment's window is its first hour; a programme must end inside it for the appointment to happen at all. */
|
|
|
|
|
private const val ON_TIME_LAST_CALL_MS = 60 * 60_000L
|
|
|
|
|
/** How long before an appointment the programmes leading into it may be adjusted. */
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
@@ -286,6 +307,25 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
// How many times in a row the last show has played, counted from the start of this extension.
|
|
|
|
|
var run = if (cursor.lastSeriesId != null) runBefore else 0
|
|
|
|
|
val blockLast = cursor.blockLast.toMutableMap()
|
|
|
|
|
// When each film last started, kept so a one-a-night event can tell it has had its turn.
|
|
|
|
|
val shown = cursor.filmShown.toMutableMap()
|
|
|
|
|
fun hadItsTurn(available: List<PoolSeries>, at: Long) = available.any { film -> film.isFilm && shown[film.id]?.let { at - it < ONCE_A_NIGHT_MS } == true }
|
|
|
|
|
/** The part for [at], passing over a one-a-night event that has already started a programme tonight. */
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
/** The next on-time appointment within a few hours, if there is one and it hasn't had its turn tonight. */
|
|
|
|
|
fun appointmentAfter(at: Long): Long? {
|
|
|
|
|
val hour = 60 * 60_000L
|
|
|
|
|
val local = java.time.Instant.ofEpochMilli(at).atZone(zone)
|
|
|
|
|
val nextHour = local.truncatedTo(java.time.temporal.ChronoUnit.HOURS).plusHours(1).toInstant().toEpochMilli()
|
|
|
|
|
return (0 until ON_TIME_LOOKAHEAD_HOURS).map { nextHour + it * hour }.firstOrNull { boundary ->
|
|
|
|
|
val (films, _, part) = partAt(boundary)
|
|
|
|
|
part?.onTime == true && films.isNotEmpty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
@@ -294,7 +334,7 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
return mix[(mix.indexOfFirst { it.mark == marks[show.id] } + 1) % mix.size]
|
|
|
|
|
}
|
|
|
|
|
while (start < untilMs) {
|
|
|
|
|
val (available, block) = dayPartChoice(pool, dayParts, start, zone)
|
|
|
|
|
val (available, block, part) = partAt(start)
|
|
|
|
|
// Shows only wait for others in the same block, or a daytime show could stand idle for an evening one.
|
|
|
|
|
val restarted = finishTogether && available.all { marks[it.id] == running(it).last().mark }
|
|
|
|
|
if (restarted) available.forEach { marks.remove(it.id) }
|
|
|
|
@@ -310,7 +350,27 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
else -> true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
val chosen = picked ?: nextInTurn(pool, after, allowed)
|
|
|
|
|
var chosen = picked ?: nextInTurn(pool, after, allowed)
|
|
|
|
|
// Before an appointment, a show in turn that finishes in time goes first, so the appointment starts near its hour. Nothing is cut.
|
|
|
|
|
val appointment = if (part?.onTime == true) null else appointmentAfter(start)
|
|
|
|
|
var fitted = false
|
|
|
|
|
val latest = (appointment ?: 0L) + ON_TIME_GRACE_MS
|
|
|
|
|
fun ends(show: PoolSeries) = start + following(show).durationMs
|
|
|
|
|
val candidates = if (appointment == null) emptyList() else turnOrder(pool, after).filter { show -> allowed(show) && !avoid(show, start, ends(show)) }
|
|
|
|
|
/** Whether [show] leaves the appointment on time: ending in its first minutes, or leaving a gap another show closes. */
|
|
|
|
|
fun onTimeAfter(show: PoolSeries) = ends(show) in appointment!!..latest ||
|
|
|
|
|
(ends(show) < appointment && candidates.any { other -> other.id != show.id && ends(show) + following(other).durationMs in appointment..latest })
|
|
|
|
|
// Only the last stretch before the appointment is adjusted; earlier programmes keep their ordinary turns.
|
|
|
|
|
if (appointment != null && ends(chosen) > appointment - ON_TIME_RUN_IN_MS && !onTimeAfter(chosen)) {
|
|
|
|
|
val fits = candidates.filter { ends(it) <= latest }
|
|
|
|
|
// Best is a show ending right at the appointment; next, one that leaves a gap another show can close; then anything that fits.
|
|
|
|
|
val choice = fits.firstOrNull { ends(it) >= appointment }
|
|
|
|
|
?: fits.firstOrNull { onTimeAfter(it) }
|
|
|
|
|
?: fits.firstOrNull().takeIf { ends(chosen) > latest }
|
|
|
|
|
// Failing that, at least something that ends while the appointment's window is still open, so it isn't missed.
|
|
|
|
|
?: candidates.firstOrNull { ends(it) < appointment + ON_TIME_LAST_CALL_MS }.takeIf { ends(chosen) >= appointment + ON_TIME_LAST_CALL_MS }
|
|
|
|
|
if (choice != null) { chosen = choice; fitted = true }
|
|
|
|
|
}
|
|
|
|
|
// Never a third in a row while another show in the block is only waiting for the round to finish. That one starts its next round early.
|
|
|
|
|
val early = if (!noThirdInARow || !finishTogether || chosen.id != last || run < 2) null else turnOrder(pool, after).firstOrNull { show ->
|
|
|
|
|
show.id != chosen.id && show in available && marks[show.id] == running(show).last().mark && !avoid(show, start, start + running(show).first().durationMs)
|
|
|
|
@@ -326,6 +386,7 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
else -> "; fallback: every show in turn airs elsewhere then"
|
|
|
|
|
})
|
|
|
|
|
if (isMixed(series)) append(", episode from its shuffled order")
|
|
|
|
|
if (fitted) append("; chosen to finish before the appointment at ${java.time.Instant.ofEpochMilli(appointment!!).atZone(zone).toLocalTime()}")
|
|
|
|
|
if (restarted) append("; every show had finished, so all started again")
|
|
|
|
|
if (waiting.isNotEmpty()) append("; deferred until the rest finish: ${someOf(waiting)}")
|
|
|
|
|
if (elsewhere.isNotEmpty()) append("; deferred, airing elsewhere: ${someOf(elsewhere)}")
|
|
|
|
@@ -337,9 +398,10 @@ internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
run = if (series.id == last) run + 1 else 1
|
|
|
|
|
last = series.id
|
|
|
|
|
blockLast[block] = series.id
|
|
|
|
|
if (series.isFilm) shown[series.id] = start
|
|
|
|
|
start += episode.durationMs
|
|
|
|
|
}
|
|
|
|
|
return ChannelPlan(entries, ScheduleCursor(last, marks, blockLast = blockLast))
|
|
|
|
|
return ChannelPlan(entries, ScheduleCursor(last, marks, shown, blockLast))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal fun scheduledItem(seriesTitle: String, episode: PoolEpisode) = SampleItem(
|
|
|
|
|