|
|
|
@@ -13,7 +13,21 @@ internal data class DayPart(val fromHour: Int, val toHour: Int, val titles: List
|
|
|
|
|
/** On a channel with weighted rotation: false keeps this block on equal turns, as Tots' Wind Down and Night (4 November 2026). */
|
|
|
|
|
val weighted: Boolean = true,
|
|
|
|
|
/** On a channel with weighted rotation: tier weights for this block only, over the channel's, as ToonZ's teatime trio. */
|
|
|
|
|
val weights: Map<String, Double> = emptyMap()) {
|
|
|
|
|
val weights: Map<String, Double> = emptyMap(),
|
|
|
|
|
/** An event that starts on this date: before it, the event gives way to the usual block. */
|
|
|
|
|
val from: java.time.LocalDate? = null,
|
|
|
|
|
/**
|
|
|
|
|
* A premiere (Stage D1, approved by Paul on 4 November 2026): a series event that only ever shows episodes not yet aired on
|
|
|
|
|
* Orbit, in order, [premiereDouble] a night while at least [premiereBuffer] are waiting, otherwise one, and nothing once
|
|
|
|
|
* they run out, so the slot goes back to the usual block rather than repeating one as new. Weighted channels only.
|
|
|
|
|
*/
|
|
|
|
|
val premiere: Boolean = false,
|
|
|
|
|
val premiereDouble: Int = 2,
|
|
|
|
|
val premiereBuffer: Int = 4,
|
|
|
|
|
/** Set to true for a block only on the channel's premiere nights, false for one only on its other nights. Null is every night. */
|
|
|
|
|
val premiereNights: Boolean? = null,
|
|
|
|
|
/** An appointment's own allowance for starting late, instead of [ON_TIME_GRACE_MS]. */
|
|
|
|
|
val graceMs: Long? = null) {
|
|
|
|
|
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)
|
|
|
|
@@ -168,7 +182,7 @@ internal val SmartChannels = listOf(
|
|
|
|
|
SuggestionRule("day", "Daytime", "Movie", listOf("Thriller", "Mystery"), 1975..2005, 6.5f, setOf("NC-17", "X"), 30, NotCultCinema + "Science Fiction", 70, 7.9f),
|
|
|
|
|
SuggestionRule("late", "Late night", "Movie", listOf("Horror"), 1970..2010, 6f, emptySet(), 90, NotCultCinema, 70))),
|
|
|
|
|
ChannelProgramming(VORTEX_NUMBER, "Vortex", VortexShows, seriesCount = 40, finishTogether = true, exactTitles = true, englishAudio = false, tierWeights = VortexTiers, catalogueScaleHours = CATALOGUE_SCALE_HOURS,
|
|
|
|
|
dayParts = VortexDayParts,
|
|
|
|
|
dayParts = VortexDayParts, withdrawn = setOf(BEERUS_OTHER_COPY),
|
|
|
|
|
suggestionRules = listOf(
|
|
|
|
|
SuggestionRule("movie", "Movie Night", "Movie", listOf("Animation"), 1985..2025, 5f, setOf("NC-17", "X"), 30, setOf("Documentary"), 30, titleContains = "Dragon Ball"),
|
|
|
|
|
SuggestionRule("movie", "Movie Night", "Movie", listOf("Animation", "Action"), 1985..2015, 7f, setOf("NC-17", "X"), 30, setOf("Documentary", "Family"), 60),
|
|
|
|
@@ -551,19 +565,19 @@ internal fun nextAppointment(at: Long, zone: java.time.ZoneId, lookaheadHours: I
|
|
|
|
|
*/
|
|
|
|
|
internal fun <T> fitBeforeAppointment(appointment: Long, start: Long, chosen: T, candidates: List<T>, runInMs: Long = ON_TIME_RUN_IN_MS,
|
|
|
|
|
/** How many more programmes may follow before the appointment: 1 for series, 2 for films, which are long. */
|
|
|
|
|
followers: Int = 1, ends: (T) -> Long): T? {
|
|
|
|
|
val latest = appointment + ON_TIME_GRACE_MS
|
|
|
|
|
followers: Int = 1, graceMs: Long = ON_TIME_GRACE_MS, ends: (T) -> Long): T? {
|
|
|
|
|
val latest = appointment + graceMs
|
|
|
|
|
if (ends(chosen) <= appointment - runInMs) return null
|
|
|
|
|
val remaining = appointment - start
|
|
|
|
|
fun length(x: T) = ends(x) - start
|
|
|
|
|
/** Whether [first] can open a run of up to [followers] more different candidates that ends at the appointment or in its grace. */
|
|
|
|
|
fun opensRun(first: T): Boolean {
|
|
|
|
|
val total = length(first)
|
|
|
|
|
if (total in remaining..remaining + ON_TIME_GRACE_MS) return true
|
|
|
|
|
if (total in remaining..remaining + graceMs) return true
|
|
|
|
|
if (total > remaining) return false
|
|
|
|
|
val rest = candidates.filter { it != first }.map(::length)
|
|
|
|
|
if (followers >= 1 && rest.any { total + it in remaining..remaining + ON_TIME_GRACE_MS }) return true
|
|
|
|
|
if (followers >= 2) rest.forEachIndexed { i, a -> if (total + a < remaining) for (j in i + 1 until rest.size) if (total + a + rest[j] in remaining..remaining + ON_TIME_GRACE_MS) return true }
|
|
|
|
|
if (followers >= 1 && rest.any { total + it in remaining..remaining + graceMs }) return true
|
|
|
|
|
if (followers >= 2) rest.forEachIndexed { i, a -> if (total + a < remaining) for (j in i + 1 until rest.size) if (total + a + rest[j] in remaining..remaining + graceMs) return true }
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if (opensRun(chosen)) return null
|
|
|
|
@@ -645,10 +659,55 @@ internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
/** How long [show]'s next booking runs: the whole story when it opens one. */
|
|
|
|
|
fun lengthOf(show: PoolSeries): Long = following(show).let { episode -> storiesOf(show)?.takeIf { it.partOf(episode) == 0 }?.of(episode)?.durationMs ?: episode.durationMs }
|
|
|
|
|
fun hadItsTurn(available: List<PoolSeries>, at: Long) = available.any { film -> film.isFilm && shown[film.id]?.let { at - it < ONCE_A_NIGHT_MS } == true }
|
|
|
|
|
// A premiere event: its show, and the files already booked for it in the day before this stretch, so a night split by a top-up carries on.
|
|
|
|
|
val premierePart = dayParts.firstOrNull { it.premiere }
|
|
|
|
|
val premiereShows = premierePart?.let { p -> pool.filter { show -> p.titles.any { seriesKey(it) == seriesKey(show.title) } } }.orEmpty()
|
|
|
|
|
val premiereFiles = premiereShows.flatMapTo(HashSet()) { show -> show.episodes.map { it.id } }
|
|
|
|
|
/** The evening a time belongs to: the small hours count with the day before. */
|
|
|
|
|
fun nightOf(at: Long) = java.time.Instant.ofEpochMilli(at).atZone(zone).let { if (it.hour < 6) it.toLocalDate().minusDays(1) else it.toLocalDate() }
|
|
|
|
|
fun unaired(show: PoolSeries) = marks[show.id]?.let { mark -> show.episodes.count { it.mark > mark } } ?: show.episodes.size
|
|
|
|
|
/** Premiere episodes started on [night], booked in this stretch or just before it. */
|
|
|
|
|
fun premieresOn(night: java.time.LocalDate) = (aired.filterKeys { it in premiereFiles }.values + entries.filter { it.media.item.id in premiereFiles }.map { it.startMs })
|
|
|
|
|
.distinct().count { nightOf(it) == night }
|
|
|
|
|
fun premiereCap(show: PoolSeries, night: java.time.LocalDate): Int =
|
|
|
|
|
if (unaired(show) + premieresOn(night) >= premierePart!!.premiereBuffer) premierePart.premiereDouble else 1
|
|
|
|
|
/** Whether the premiere runs on the night of [at]: its day, on or after its start date, with a new episode to show or one already shown. */
|
|
|
|
|
fun premiereNight(at: Long): Boolean {
|
|
|
|
|
val p = premierePart ?: return false
|
|
|
|
|
val night = nightOf(at)
|
|
|
|
|
return (p.days.isEmpty() || night.dayOfWeek in p.days) && (p.from == null || !night.isBefore(p.from)) &&
|
|
|
|
|
premiereShows.any { unaired(it) > 0 || premieresOn(night) > 0 }
|
|
|
|
|
}
|
|
|
|
|
fun premiereStart(at: Long) = nightOf(at).atTime(premierePart!!.fromHour, 0).atZone(zone).toInstant().toEpochMilli()
|
|
|
|
|
/** Whether [part] may be used at [at] by the premiere rules, and with which of its shows. Null means it gives way. */
|
|
|
|
|
fun premiereFilter(part: DayPart, shows: List<PoolSeries>, at: Long): List<PoolSeries>? {
|
|
|
|
|
if (part.from != null && nightOf(at).isBefore(part.from)) return null
|
|
|
|
|
if (part.premiereNights != null && part.premiereNights != premiereNight(at)) return null
|
|
|
|
|
if (part.premiere) return shows.filter { unaired(it) > 0 && premieresOn(nightOf(at)) < premiereCap(it, nightOf(at)) }.ifEmpty { null }
|
|
|
|
|
// Before a premiere, only a film that ends in time for it: nothing is cut to make room.
|
|
|
|
|
if (part.premiereNights == true && premierePart != null) {
|
|
|
|
|
val latest = premiereStart(at) + (premierePart.graceMs ?: ON_TIME_GRACE_MS)
|
|
|
|
|
return shows.filter { !it.isFilm || at + it.episodes.first().durationMs <= latest }.ifEmpty { null }
|
|
|
|
|
}
|
|
|
|
|
return shows
|
|
|
|
|
}
|
|
|
|
|
fun partAt(at: Long): Triple<List<PoolSeries>, String, DayPart?> {
|
|
|
|
|
// A premiere night's second episode follows its first straight away, whatever the hour.
|
|
|
|
|
if (premierePart != null) premiereShows.firstOrNull { it.id == last && unaired(it) > 0 && premieresOn(nightOf(at)) in 1 until premiereCap(it, nightOf(at)) &&
|
|
|
|
|
entries.lastOrNull()?.media?.item?.id.let { id -> id == null || id in premiereFiles } }
|
|
|
|
|
?.let { return Triple(listOf(it), "event " + premierePart.group.ifEmpty { "${premierePart.fromHour}-${premierePart.toHour}" }, premierePart) }
|
|
|
|
|
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
|
|
|
|
|
val spent = part.once && hadItsTurn(first.first, at)
|
|
|
|
|
var found = if (spent) dayPartFor(pool, dayParts - part, at, zone) else first
|
|
|
|
|
if (premierePart == null && dayParts.none { it.from != null || it.premiereNights != null }) return found
|
|
|
|
|
// Premiere rules: a part that gives way leaves the time to the next one that covers it.
|
|
|
|
|
var parts = if (spent) dayParts - part else dayParts
|
|
|
|
|
while (true) {
|
|
|
|
|
val candidate = found.third ?: return found
|
|
|
|
|
val allowed = premiereFilter(candidate, found.first, at) ?: run { parts = parts - candidate; found = dayPartFor(pool, parts, at, zone); null } ?: continue
|
|
|
|
|
return Triple(allowed, found.second, candidate)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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 }
|
|
|
|
@@ -687,7 +746,8 @@ internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
// Before an appointment, the eligible show that leaves it on time, then any clear one, in tally order.
|
|
|
|
|
val appointment = if (part?.onTime == true) null else nextAppointment(start, zone, partAt = ::partAt)
|
|
|
|
|
val fitted = if (continuing != null) null else appointment?.let { at -> fun ends(show: PoolSeries) = start + lengthOf(show)
|
|
|
|
|
fitBeforeAppointment(at, start, preferred, eligible.sortedWith(byTally), ends = ::ends) ?: fitBeforeAppointment(at, start, preferred, clear.sortedWith(byTally), ends = ::ends) }
|
|
|
|
|
val grace = partAt(at).third?.graceMs ?: ON_TIME_GRACE_MS
|
|
|
|
|
fitBeforeAppointment(at, start, preferred, eligible.sortedWith(byTally), graceMs = grace, ends = ::ends) ?: fitBeforeAppointment(at, start, preferred, clear.sortedWith(byTally), graceMs = grace, ends = ::ends) }
|
|
|
|
|
val series = continuing ?: fitted ?: preferred
|
|
|
|
|
val episode = following(series)
|
|
|
|
|
val story = storiesOf(series)?.let { it.of(episode)?.let { found -> found.parts.size to it.partOf(episode) + 1 } }
|
|
|
|
@@ -697,7 +757,8 @@ internal fun extendWeighted(pool: List<PoolSeries>, cursor: ScheduleCursor, from
|
|
|
|
|
series.id == last && unclashed.size < available.size -> "; again, as the others here air elsewhere then"
|
|
|
|
|
else -> ""
|
|
|
|
|
} + (if (fitted != null) "; chosen to finish before the appointment at ${java.time.Instant.ofEpochMilli(appointment!!).atZone(zone).toLocalTime()}" else "") +
|
|
|
|
|
(if (story != null) "; story: part 1 of ${story.first}, booked whole" else "")
|
|
|
|
|
(if (story != null) "; story: part 1 of ${story.first}, booked whole" else "") +
|
|
|
|
|
(if (part?.premiere == true && series in available) "; premiere: first airing on Orbit" 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 "")
|
|
|
|
|
val weight = part?.takeIf { series in available }?.weights?.get(seriesKey(series.title)) ?: weightOf(series)
|
|
|
|
@@ -947,7 +1008,8 @@ internal class ScheduledChannel(val loop: SampleChannel, plan: ChannelPlan) : Sa
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal val PRESENTATION_KEY = Regex("[a-z0-9]{1,20}\\.[a-z0-9]{1,20}")
|
|
|
|
|
// Two or three parts, such as "riot.laughs" or "vortex.afterburn.premiere" (Stage D1).
|
|
|
|
|
internal val PRESENTATION_KEY = Regex("[a-z0-9]{1,20}(\\.[a-z0-9]{1,20}){1,2}")
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A booked programme's presentation: the key saved with it when it was booked. A programme booked before keys were kept
|
|
|
|
|